Skip to content

Commit 82ab4eb

Browse files
committed
Allow elements asserting
1 parent 50c34bd commit 82ab4eb

File tree

3 files changed

+334
-13
lines changed

3 files changed

+334
-13
lines changed

README.md

Lines changed: 136 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,48 @@ Publish the config file to control default settings:
1818
php artisan vendor:publish --tag=visual-assert-config
1919
```
2020

21-
2221
## Usage
2322

2423
The Dusk Browser class now has access to some new methods:
2524

2625
### assertScreenshot()
2726

28-
This method will take a screenshot of the current page and compare it to a reference image (generated the first time the test is run).
27+
This method will take a screenshot of the current page and compare it to a reference image (generated the first time the test is run).
2928

3029
If the images are different, the test will fail and save the image diff so you can inspect the differences.
3130

3231
```php
33-
$browser->assertScreenshot(string $name, float|null $threshold = null, int|null $metric = null)
32+
$browser->assertScreenshot(string $name, float|null $threshold = null, int|null $metric = null, int|null $width = null, int|null $height = null)
3433
```
3534

36-
Example:
35+
Example:
3736

3837
```php
3938
$this->browse(function (Browser $browser) {
40-
$browser->visit('/')
41-
->assertScreenshot('home');
42-
});
39+
$browser->visit('/')
40+
->assertScreenshot('home');
41+
});
42+
```
43+
44+
### assertElementScreenshot()
45+
46+
Take a screenshot of a specific element and compare it to a reference image. Perfect for testing individual components without worrying about the rest of the page.
47+
48+
```php
49+
$browser->assertElementScreenshot(string $selector, string $name, float|null $threshold = null, int|null $metric = null)
50+
```
51+
52+
Example:
53+
54+
```php
55+
$this->browse(function (Browser $browser) {
56+
$browser->visit('/login')
57+
// Test that the login form hasn't changed
58+
->assertElementScreenshot('#login-form', 'login-form')
59+
60+
// Test header with custom threshold
61+
->assertElementScreenshot('header', 'header', 0.02);
62+
});
4363
```
4464

4565
### assertResponsiveScreenshots()
@@ -54,9 +74,115 @@ Example:
5474

5575
```php
5676
$this->browse(function (Browser $browser) {
57-
$browser->visit('/')
58-
->assertResponsiveScreenshots('home');
59-
});
77+
$browser->visit('/')
78+
->assertResponsiveScreenshots('home');
79+
});
80+
```
81+
82+
### assertResponsiveElementScreenshots()
83+
84+
Test an element at different responsive breakpoints.
85+
86+
```php
87+
$browser->assertResponsiveElementScreenshots(string $selector, string $name, float|null $threshold = null, int|null $metric = null)
88+
```
89+
90+
Example:
91+
92+
```php
93+
$this->browse(function (Browser $browser) {
94+
$browser->visit('/products')
95+
// Test product grid at different screen sizes
96+
->assertResponsiveElementScreenshots('.product-grid', 'product-grid');
97+
});
98+
```
99+
100+
### assertElementsScreenshots()
101+
102+
Test multiple elements at once by providing an array of selectors and names.
103+
104+
```php
105+
$browser->assertElementsScreenshots(array $elements, float|null $threshold = null, int|null $metric = null)
106+
```
107+
108+
Example:
109+
110+
```php
111+
$this->browse(function (Browser $browser) {
112+
$browser->visit('/dashboard')
113+
->assertElementsScreenshots([
114+
'#sidebar' => 'dashboard-sidebar',
115+
'.user-profile' => 'user-profile-card',
116+
'#main-content' => 'main-content-area',
117+
]);
118+
});
119+
```
120+
121+
### assertScreenshotWithoutFixed()
122+
123+
Take a screenshot with fixed elements (like sticky headers, chat widgets, cookie banners) automatically hidden.
124+
125+
```php
126+
$browser->assertScreenshotWithoutFixed(string $name, array|null $selectorsToHide = null, float|null $threshold = null, int|null $metric = null, int|null $width = null, int|null $height = null)
127+
```
128+
129+
Example:
130+
131+
```php
132+
$this->browse(function (Browser $browser) {
133+
$browser->visit('/')
134+
// Uses default selectors from config
135+
->assertScreenshotWithoutFixed('homepage-clean')
136+
137+
// Hide specific elements
138+
->assertScreenshotWithoutFixed('homepage-custom', [
139+
'.chat-widget',
140+
'#cookie-banner',
141+
'.sticky-nav'
142+
]);
143+
});
144+
```
145+
146+
### assertElementScreenshotWithoutFixed()
147+
148+
Take an element screenshot with fixed elements hidden.
149+
150+
```php
151+
$browser->assertElementScreenshotWithoutFixed(string $selector, string $name, array|null $selectorsToHide = null, float|null $threshold = null, int|null $metric = null)
152+
```
153+
154+
Example:
155+
156+
```php
157+
$this->browse(function (Browser $browser) {
158+
$browser->visit('/products')
159+
->assertElementScreenshotWithoutFixed(
160+
'.product-grid',
161+
'products-no-overlay',
162+
['.promo-popup', '.flash-sale-banner']
163+
);
164+
});
165+
```
166+
167+
### withoutFixedElements()
168+
169+
Temporarily hide fixed elements for multiple operations.
170+
171+
```php
172+
$browser->withoutFixedElements(callable $callback, array|null $selectors = null)
173+
```
174+
175+
Example:
176+
177+
```php
178+
$this->browse(function (Browser $browser) {
179+
$browser->visit('/dashboard')
180+
->withoutFixedElements(function ($browser) {
181+
return $browser
182+
->assertScreenshot('dashboard-clean')
183+
->assertElementScreenshot('#charts', 'charts-clean');
184+
}, ['.fixed']);
185+
});
60186
```
61187

62188
## Updating reference images

config/visual-assert.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@
1313
'default_metric' => \Imagick::METRIC_MEANSQUAREERROR,
1414

1515
'skip_if_different_window_size' => false,
16+
17+
/**
18+
* Skip assertion if element sizes don't match (for element screenshots)
19+
* If true, the test will pass without comparison when element dimensions differ
20+
* If false, the test will fail when element dimensions differ
21+
*/
22+
'skip_if_different_element_size' => false,
23+
24+
/**
25+
* CSS selectors for fixed elements to hide during screenshots
26+
*/
27+
'fixed_elements_to_hide' => [
28+
'.fixed',
29+
'.sticky',
30+
],
1631
];

0 commit comments

Comments
 (0)