@@ -18,28 +18,48 @@ Publish the config file to control default settings:
1818php artisan vendor:publish --tag=visual-assert-config
1919```
2020
21-
2221## Usage
2322
2423The 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
3029If 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
0 commit comments