11package com .serenitydojo .playwright ;
22
33import com .microsoft .playwright .*;
4+ import com .microsoft .playwright .assertions .LocatorAssertions ;
45import com .microsoft .playwright .assertions .PlaywrightAssertions ;
6+ import com .microsoft .playwright .junit .UsePlaywright ;
57import com .microsoft .playwright .options .AriaRole ;
68import com .microsoft .playwright .options .LoadState ;
79import com .microsoft .playwright .options .SelectOption ;
1517
1618import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
1719
18- @ Execution (ExecutionMode .SAME_THREAD )
20+ @ UsePlaywright
21+ @ Disabled
1922public class PlaywrightLocatorsTest {
2023
2124 protected static Playwright playwright ;
@@ -55,20 +58,20 @@ static void tearDown() {
5558 class LocatingElementsUsingCSS {
5659
5760 @ BeforeEach
58- void openContactPage () {
61+ void openContactPage (Page page ) {
5962 page .navigate ("https://practicesoftwaretesting.com/contact" );
6063 }
6164
6265 @ DisplayName ("By id" )
6366 @ Test
64- void locateTheFirstNameFieldByID () {
67+ void locateTheFirstNameFieldByID (Page page ) {
6568 page .locator ("#first_name" ).fill ("Sarah-Jane" );
6669 assertThat (page .locator ("#first_name" )).hasValue ("Sarah-Jane" );
6770 }
6871
6972 @ DisplayName ("By CSS class" )
7073 @ Test
71- void locateTheSendButtonByCssClass () {
74+ void locateTheSendButtonByCssClass (Page page ) {
7275 page .locator ("#first_name" ).fill ("Sarah-Jane" );
7376 page .locator (".btnSubmit" ).click ();
7477 List <String > alertMessages = page .locator (".alert" ).allTextContents ();
@@ -78,7 +81,7 @@ void locateTheSendButtonByCssClass() {
7881
7982 @ DisplayName ("By attribute" )
8083 @ Test
81- void locateTheSendButtonByAttribute () {
84+ void locateTheSendButtonByAttribute (Page page ) {
8285 page .locator ("input[placeholder='Your last name *']" ).fill ("Smith" );
8386 assertThat (page .locator ("#last_name" )).hasValue ("Smith" );
8487 }
@@ -89,14 +92,14 @@ void locateTheSendButtonByAttribute() {
8992 class LocatingElementsByTextUsingCSS {
9093
9194 @ BeforeEach
92- void openContactPage () {
95+ void openContactPage (Page page ) {
9396 page .navigate ("https://practicesoftwaretesting.com/contact" );
9497 }
9598
9699 // :has-text matches any element containing specified text somewhere inside.
97100 @ DisplayName ("Using :has-text" )
98101 @ Test
99- void locateTheSendButtonByText () {
102+ void locateTheSendButtonByText (Page page ) {
100103 page .locator ("#first_name" ).fill ("Sarah-Jane" );
101104 page .locator ("#last_name" ).fill ("Smith" );
102105 page .locator ("input:has-text('Send')" ).click ();
@@ -105,7 +108,7 @@ void locateTheSendButtonByText() {
105108 // :text matches the smallest element containing specified text.
106109 @ DisplayName ("Using :text" )
107110 @ Test
108- void locateAProductItemByText () {
111+ void locateAProductItemByText (Page page ) {
109112 page .locator (".navbar :text('Home')" ).click ();
110113 page .locator (".card :text('Bolt')" ).click ();
111114 assertThat (page .locator ("[data-test=product-name]" )).hasText ("Bolt Cutters" );
@@ -114,7 +117,7 @@ void locateAProductItemByText() {
114117 // Exact matches
115118 @ DisplayName ("Using :text-is" )
116119 @ Test
117- void locateAProductItemByTextIs () {
120+ void locateAProductItemByTextIs (Page page ) {
118121 page .locator (".navbar :text('Home')" ).click ();
119122 page .locator (".card :text-is('Bolt Cutters')" ).click ();
120123 assertThat (page .locator ("[data-test=product-name]" )).hasText ("Bolt Cutters" );
@@ -123,7 +126,7 @@ void locateAProductItemByTextIs() {
123126 // matching with regular expressions
124127 @ DisplayName ("Using :text-matches" )
125128 @ Test
126- void locateAProductItemByTextMatches () {
129+ void locateAProductItemByTextMatches (Page page ) {
127130 page .locator (".navbar :text('Home')" ).click ();
128131 page .locator (".card :text-matches('Bolt \\ \\ w+')" ).click ();
129132 assertThat (page .locator ("[data-test=product-name]" )).hasText ("Bolt Cutters" );
@@ -134,20 +137,20 @@ void locateAProductItemByTextMatches() {
134137 @ Nested
135138 class LocatingVisibleElements {
136139 @ BeforeEach
137- void openContactPage () {
138- openPage ();
140+ void openContactPage (Page page ) {
141+ openPage (page );
139142 }
140143
141144 @ DisplayName ("Finding visible and invisible elements" )
142145 @ Test
143- void locateVisibleAndInvisibleItems () {
146+ void locateVisibleAndInvisibleItems (Page page ) {
144147 int dropdownItems = page .locator (".dropdown-item" ).count ();
145148 Assertions .assertTrue (dropdownItems > 0 );
146149 }
147150
148151 @ DisplayName ("Finding only visible elements" )
149152 @ Test
150- void locateVisibleItems () {
153+ void locateVisibleItems (Page page ) {
151154 int dropdownItems = page .locator (".dropdown-item:visible" ).count ();
152155 Assertions .assertTrue (dropdownItems == 0 );
153156 }
@@ -159,7 +162,7 @@ class LocatingElementsByRole {
159162
160163 @ DisplayName ("Using the BUTTON role" )
161164 @ Test
162- void byButton () {
165+ void byButton (Page page ) {
163166 page .navigate ("https://practicesoftwaretesting.com/contact" );
164167
165168
@@ -173,8 +176,8 @@ void byButton() {
173176
174177 @ DisplayName ("Using the HEADING role" )
175178 @ Test
176- void byHeaderRole () {
177- openPage ();
179+ void byHeaderRole (Page page ) {
180+ openPage (page );
178181
179182 page .locator ("#search-query" ).fill ("Pliers" );
180183
@@ -191,25 +194,25 @@ void byHeaderRole() {
191194
192195 @ DisplayName ("Using the HEADING role and level" )
193196 @ Test
194- void byHeaderRoleLevel () {
195- openPage ();
196-
197- List <String > level4Headings
198- = page .getByRole (AriaRole .HEADING ,
199- new Page .GetByRoleOptions ()
200- .setName ("Pliers" )
201- .setLevel (5 ))
202- .allTextContents ();
197+ void byHeaderRoleLevel (Page page ) {
198+ openPage (page );
199+
200+ Locator headings = page .getByRole (
201+ AriaRole .HEADING ,
202+ new Page .GetByRoleOptions ().setLevel (5 )
203+ );
204+ assertThat (headings .first ()).isVisible ();
205+
206+ List <String > level4Headings = headings .allTextContents ();
203207
204208 org .assertj .core .api .Assertions .assertThat (level4Headings ).isNotEmpty ();
205209 }
206210
207211 @ DisplayName ("Identifying checkboxes" )
208212 @ Test
209- void byCheckboxes () {
210- playwright .selectors ().setTestIdAttribute ("data-test" );
213+ void byCheckboxes (Page page ) {
211214
212- openPage ();
215+ openPage (page );
213216 page .getByLabel ("Hammer" ).click ();
214217 page .getByLabel ("Chisels" ).click ();
215218 page .getByLabel ("Wrench" ).click ();
@@ -241,7 +244,7 @@ class LocatingElementsByPlaceholdersAndLabels {
241244
242245 @ DisplayName ("Using a label" )
243246 @ Test
244- void byLabel () {
247+ void byLabel (Page page ) {
245248 page .navigate ("https://practicesoftwaretesting.com/contact" );
246249
247250 page .getByLabel ("First name" ).fill ("Obi-Wan" );
@@ -254,7 +257,7 @@ void byLabel() {
254257
255258 @ DisplayName ("Using a placeholder text" )
256259 @ Test
257- void byPlaceholder () {
260+ void byPlaceholder (Page page ) {
258261 page .navigate ("https://practicesoftwaretesting.com/contact" );
259262
260263 page .getByPlaceholder ("Your first name" ).fill ("Obi-Wan" );
@@ -272,29 +275,29 @@ void byPlaceholder() {
272275 class LocatingElementsByText {
273276
274277 @ BeforeEach
275- void openTheCatalogPage () {
276- openPage ();
278+ void openTheCatalogPage (Page page ) {
279+ openPage (page );
277280 }
278281
279282 @ DisplayName ("Locating an element by text contents" )
280283 @ Test
281- void byText () {
284+ void byText (Page page ) {
282285 page .getByText ("Bolt Cutters" ).click ();
283286
284287 PlaywrightAssertions .assertThat (page .getByText ("MightyCraft Hardware" )).isVisible ();
285288 }
286289
287290 @ DisplayName ("Using alt text" )
288291 @ Test
289- void byAltText () {
292+ void byAltText (Page page ) {
290293 page .getByAltText ("Combination Pliers" ).click ();
291294
292295 PlaywrightAssertions .assertThat (page .getByText ("ForgeFlex Tools" )).isVisible ();
293296 }
294297
295298 @ DisplayName ("Using title" )
296299 @ Test
297- void byTitle () {
300+ void byTitle (Page page ) {
298301 page .getByAltText ("Combination Pliers" ).click ();
299302
300303 page .getByTitle ("Practice Software Testing - Toolshop" ).click ();
@@ -305,17 +308,10 @@ void byTitle() {
305308 @ Nested
306309 class LocatingElementsByTestID {
307310
308- @ BeforeAll
309- static void setTestId () {
310- playwright .selectors ().setTestIdAttribute ("data-test" );
311- }
312-
313311 @ DisplayName ("Using a custom data-test field" )
314312 @ Test
315- void byTestId () {
316- openPage ();
317-
318- playwright .selectors ().setTestIdAttribute ("data-test" );
313+ void byTestId (Page page ) {
314+ openPage (page );
319315
320316 page .getByTestId ("search-query" ).fill ("Pliers" );
321317 page .getByTestId ("search-submit" ).click ();
@@ -334,8 +330,8 @@ static void setTestId() {
334330
335331 @ DisplayName ("Using roles" )
336332 @ Test
337- void locatingAMenuItemUsingRoles () {
338- openPage ();
333+ void locatingAMenuItemUsingRoles (Page page ) {
334+ openPage (page );
339335
340336 page .getByRole (AriaRole .MENUBAR , new Page .GetByRoleOptions ().setName ("Main Menu" ))
341337 .getByRole (AriaRole .MENUITEM , new Locator .GetByRoleOptions ().setName ("Home" ))
@@ -344,8 +340,8 @@ void locatingAMenuItemUsingRoles() {
344340
345341 @ DisplayName ("Using roles with other strategies" )
346342 @ Test
347- void locatingAMenuItemUsingRolesAndOtherStrategies () {
348- openPage ();
343+ void locatingAMenuItemUsingRolesAndOtherStrategies (Page page ) {
344+ openPage (page );
349345
350346 page .getByRole (AriaRole .MENUBAR , new Page .GetByRoleOptions ().setName ("Main Menu" ))
351347 .getByText ("Home" )
@@ -354,8 +350,8 @@ void locatingAMenuItemUsingRolesAndOtherStrategies() {
354350
355351 @ DisplayName ("filtering locators by text" )
356352 @ Test
357- void filteringMenuItems () {
358- openPage ();
353+ void filteringMenuItems (Page page ) {
354+ openPage (page );
359355
360356 page .getByRole (AriaRole .MENUBAR , new Page .GetByRoleOptions ().setName ("Main Menu" ))
361357 .getByText ("Categories" )
@@ -376,21 +372,22 @@ void filteringMenuItems() {
376372
377373 @ DisplayName ("filtering locators by locator" )
378374 @ Test
379- void filteringMenuItemsByLocator () {
380- openPage (); ;
375+ void filteringMenuItemsByLocator (Page page ) {
376+ openPage (page ) ;
381377
382- List < String > allProducts = page .locator (".card" )
378+ Locator allProducts = page .locator (".card" )
383379 .filter (new Locator .FilterOptions ().setHas (page .getByText ("Out of stock" )))
384- .getByTestId ("product-name" )
385- .allTextContents ();
380+ .getByTestId ("product-name" );
381+
382+ assertThat (allProducts .first ()).isVisible ();
383+ List <String > allProductNames = allProducts .allTextContents ();
386384
387- org .assertj .core .api .Assertions .assertThat (allProducts ).hasSize (1 )
385+ org .assertj .core .api .Assertions .assertThat (allProductNames ).hasSize (1 )
388386 .allMatch (name -> name .contains ("Long Nose Pliers" ));
389387 }
390388 }
391389
392- private void openPage () {
390+ private void openPage (Page page ) {
393391 page .navigate ("https://practicesoftwaretesting.com" );
394- page .waitForLoadState (LoadState .NETWORKIDLE );
395392 }
396393}
0 commit comments