@@ -69,8 +69,8 @@ static private void rafraf(Page page) {
6969 // Do a double raf since single raf does not
7070 // actually guarantee a new animation frame.
7171 page .evaluate ("() => new Promise(x => {\n " +
72- " requestAnimationFrame(() => requestAnimationFrame(x));\n " +
73- " })" );
72+ " requestAnimationFrame(() => requestAnimationFrame(x));\n " +
73+ " })" );
7474 }
7575
7676 @ Test
@@ -136,7 +136,7 @@ void shouldNotCaptureInfiniteWebAnimations() {
136136 }
137137
138138 @ Test
139- void maskShouldWork () {
139+ void maskShouldWorkForPage () {
140140 page .setViewportSize (500 , 500 );
141141 page .navigate (server .PREFIX + "/grid.html" );
142142 byte [] screenshot = page .screenshot (new Page .ScreenshotOptions ()
@@ -146,6 +146,17 @@ void maskShouldWork() {
146146 assertThrows (AssertionFailedError .class , () -> assertArrayEquals (screenshot , originalScreenshot ));
147147 }
148148
149+ @ Test
150+ void maskShouldWorkForLocator () {
151+ page .navigate (server .PREFIX + "/grid.html" );
152+ Locator locatorToScreenshot = page .locator ("div" ).first ();
153+ byte [] screenshot = locatorToScreenshot .screenshot (new Locator .ScreenshotOptions ()
154+ .setMask (asList (page .locator ("img" ))));
155+ // TODO: toMatchSnapshot is not present in java, so we only checks that masked screenshot is different.
156+ byte [] originalScreenshot = locatorToScreenshot .screenshot ();
157+ assertThrows (AssertionFailedError .class , () -> assertArrayEquals (screenshot , originalScreenshot ));
158+ }
159+
149160 @ Test
150161 void shouldWorkWithDeviceScaleFactorAndClip () {
151162 try (BrowserContext context = browser .newContext (new Browser .NewContextOptions ()
@@ -186,16 +197,16 @@ void shouldWorkWithDeviceScaleFactorAndScaleDevice() {
186197 @ Test
187198 void shouldNotCaptureBlinkingCaretByDefault () {
188199 page .setContent ("<!-- Refer to stylesheet from other origin. Accessing this\n " +
189- " stylesheet rules will throw.\n " +
190- " -->\n " +
191- " <link rel=stylesheet href=\" " + server .CROSS_PROCESS_PREFIX + "/injectedstyle.css\" >\n " +
192- " <!-- make life harder: define caret color in stylesheet -->\n " +
193- " <style>\n " +
194- " div {\n " +
195- " caret-color: #000 !important;\n " +
196- " }\n " +
197- " </style>\n " +
198- " <div contenteditable=\" true\" ></div>\n " );
200+ " stylesheet rules will throw.\n " +
201+ " -->\n " +
202+ " <link rel=stylesheet href=\" " + server .CROSS_PROCESS_PREFIX + "/injectedstyle.css\" >\n " +
203+ " <!-- make life harder: define caret color in stylesheet -->\n " +
204+ " <style>\n " +
205+ " div {\n " +
206+ " caret-color: #000 !important;\n " +
207+ " }\n " +
208+ " </style>\n " +
209+ " <div contenteditable=\" true\" ></div>\n " );
199210 Locator div = page .locator ("div" );
200211 div .type ("foo bar" );
201212 byte [] screenshot = div .screenshot ();
@@ -210,19 +221,19 @@ void shouldNotCaptureBlinkingCaretByDefault() {
210221 }
211222
212223 @ Test
213- @ DisabledIf (value = "com.microsoft.playwright.TestBase#isFirefox" , disabledReason = "fixme" )
224+ @ DisabledIf (value = "com.microsoft.playwright.TestBase#isFirefox" , disabledReason = "fixme" )
214225 void shouldCaptureBlinkingCaretIfExplicitlyAskedFor () {
215226 page .setContent (" <!-- Refer to stylesheet from other origin. Accessing this\n " +
216- " stylesheet rules will throw.\n " +
217- " -->\n " +
218- " <link rel=stylesheet href=\" " + server .CROSS_PROCESS_PREFIX + "/injectedstyle.css'}\" >\n " +
219- " <!-- make life harder: define caret color in stylesheet -->\n " +
220- " <style>\n " +
221- " div {\n " +
222- " caret-color: #000 !important;\n " +
223- " }\n " +
224- " </style>\n " +
225- " <div contenteditable=\" true\" ></div>\n " );
227+ " stylesheet rules will throw.\n " +
228+ " -->\n " +
229+ " <link rel=stylesheet href=\" " + server .CROSS_PROCESS_PREFIX + "/injectedstyle.css'}\" >\n " +
230+ " <!-- make life harder: define caret color in stylesheet -->\n " +
231+ " <style>\n " +
232+ " div {\n " +
233+ " caret-color: #000 !important;\n " +
234+ " }\n " +
235+ " </style>\n " +
236+ " <div contenteditable=\" true\" ></div>\n " );
226237 Locator div = page .locator ("div" );
227238 div .type ("foo bar" );
228239 byte [] screenshot = div .screenshot ();
@@ -265,32 +276,32 @@ static boolean isScreenshotTestDisabled() {
265276 }
266277
267278 @ Test
268- @ DisabledIf (value = "com.microsoft.playwright.TestPageScreenshot#isScreenshotTestDisabled" , disabledReason = "array lengths differ" )
279+ @ DisabledIf (value = "com.microsoft.playwright.TestPageScreenshot#isScreenshotTestDisabled" , disabledReason = "array lengths differ" )
269280 void shouldHideElementsBasedOnAttr () throws IOException {
270281 page .setViewportSize (500 , 500 );
271282 page .navigate (server .PREFIX + "/grid.html" );
272283 page .locator ("div" ).nth (5 ).evaluate ("element => {\n " +
273- " element.setAttribute('data-test-screenshot', 'hide');\n " +
274- "}" );
284+ " element.setAttribute('data-test-screenshot', 'hide');\n " +
285+ "}" );
275286 byte [] actual = page .screenshot (new Page .ScreenshotOptions ().setStyle ("[data-test-screenshot=\" hide\" ] {\n " +
276- " visibility: hidden;\n " +
277- " }" ));
287+ " visibility: hidden;\n " +
288+ " }" ));
278289 assertArrayEquals (expectedScreenshot ("hide-should-work" ), actual , "Screenshots should match" );
279290 Object visibility = page .locator ("div" ).nth (5 ).evaluate ("element => element.style.visibility" );
280291 assertEquals ("" , visibility );
281292 }
282293
283294 @ Test
284- @ DisabledIf (value = "com.microsoft.playwright.TestPageScreenshot#isScreenshotTestDisabled" , disabledReason = "array lengths differ" )
295+ @ DisabledIf (value = "com.microsoft.playwright.TestPageScreenshot#isScreenshotTestDisabled" , disabledReason = "array lengths differ" )
285296 void shouldRemoveElementsBasedOnAttr () throws IOException {
286297 page .setViewportSize (500 , 500 );
287298 page .navigate (server .PREFIX + "/grid.html" );
288299 page .locator ("div" ).nth (5 ).evaluate ("element => {\n " +
289- " element.setAttribute('data-test-screenshot', 'remove');\n " +
290- " }" );
300+ " element.setAttribute('data-test-screenshot', 'remove');\n " +
301+ " }" );
291302 byte [] actual = page .screenshot (new Page .ScreenshotOptions ().setStyle ("[data-test-screenshot=\" remove\" ] {\n " +
292- " display: none;\n " +
293- " }" ));
303+ " display: none;\n " +
304+ " }" ));
294305 assertArrayEquals (expectedScreenshot ("remove-should-work" ), actual , "Screenshots should match" );
295306 Object display = page .locator ("div" ).nth (5 ).evaluate ("element => element.style.display" );
296307 assertEquals ("" , display );
0 commit comments