11package com .serenitydojo .playwright ;
22
33import com .microsoft .playwright .*;
4+ import com .microsoft .playwright .junit .UsePlaywright ;
45import com .microsoft .playwright .options .AriaRole ;
56import com .microsoft .playwright .options .SelectOption ;
67import org .assertj .core .api .Assertions ;
1213
1314import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
1415
16+ @ UsePlaywright (HeadlessChromeOptions .class )
1517public class PlaywrightCollectionsTest {
1618
17- protected static Playwright playwright ;
18- protected static Browser browser ;
19- protected static BrowserContext browserContext ;
20-
21- Page page ;
22-
23- @ BeforeAll
24- static void setUpBrowser () {
25- playwright = Playwright .create ();
26- browser = playwright .chromium ().launch (
27- new BrowserType .LaunchOptions ().setHeadless (true )
28- .setArgs (Arrays .asList ("--no-sandbox" , "--disable-extensions" , "--disable-gpu" ))
29- );
30- playwright .selectors ().setTestIdAttribute ("data-test" );
31- }
32-
3319 @ BeforeEach
34- void setUp () {
35- browserContext = browser .newContext ();
36- page = browserContext .newPage ();
37- openPage ();
38- }
39-
40- @ AfterEach
41- void closeContext () {
42- browserContext .close ();
43- }
44-
45- @ AfterAll
46- static void tearDown () {
47- browser .close ();
48- playwright .close ();
20+ void setUp (Page page ) {
21+ openPage (page );
4922 }
5023
51- private void openPage () {
24+ private void openPage (Page page ) {
5225 page .navigate ("https://practicesoftwaretesting.com" );
5326 page .waitForCondition (() -> page .getByTestId ("product-name" ).count () > 0 );
5427 }
5528
5629 @ DisplayName ("Counting items in a list" )
5730 @ Test
58- void countingItemsOnThePage () {
31+ void countingItemsOnThePage (Page page ) {
5932
6033 int itemsOnThePage = page .locator (".card" ).count ();
6134
@@ -64,23 +37,23 @@ void countingItemsOnThePage() {
6437
6538 @ DisplayName ("Finding the first matching item" )
6639 @ Test
67- void findingTheFirstMatchingItem () {
40+ void findingTheFirstMatchingItem (Page page ) {
6841
6942 page .locator (".card" ).first ().click ();
7043
7144 }
7245
7346 @ DisplayName ("Finding the nth matching item" )
7447 @ Test
75- void findingNthMatchingItem () {
48+ void findingNthMatchingItem (Page page ) {
7649
7750 page .locator (".card" ).nth (2 ).click ();
7851
7952 }
8053
8154 @ DisplayName ("Finding the last matching item" )
8255 @ Test
83- void findingLastMatchingItem () {
56+ void findingLastMatchingItem (Page page ) {
8457
8558 page .locator (".card" ).last ().click ();
8659
@@ -92,7 +65,7 @@ class FindingTheTextInAList {
9265
9366 @ DisplayName ("and finding all the text values " )
9467 @ Test
95- void withAllTextContents () {
68+ void withAllTextContents (Page page ) {
9669
9770 List <String > itemNames = page .getByTestId ("product-name" ).allTextContents ();
9871
@@ -110,7 +83,7 @@ void withAllTextContents() {
11083
11184 @ DisplayName ("and asserting with hasText" )
11285 @ Test
113- void withHasText () {
86+ void withHasText (Page page ) {
11487 assertThat (page .getByTestId ("product-name" ))
11588 .hasText (new String []{
11689 " Combination Pliers " ,
0 commit comments