11package com .serenitydojo .playwright ;
22
3- import com .google .gson .Gson ;
4- import com .google .gson .JsonObject ;
53import com .microsoft .playwright .*;
6- import com .microsoft .playwright .options .AriaRole ;
7- import com .microsoft .playwright .options .RequestOptions ;
8- import org .assertj .core .api .Assertions ;
94import org .junit .jupiter .api .*;
105import org .junit .jupiter .api .parallel .Execution ;
116import org .junit .jupiter .api .parallel .ExecutionMode ;
@@ -28,7 +23,7 @@ static void setUpBrowser() {
2823 playwright = Playwright .create ();
2924 playwright .selectors ().setTestIdAttribute ("data-test" );
3025 browser = playwright .chromium ().launch (
31- new BrowserType .LaunchOptions ().setHeadless (true )
26+ new BrowserType .LaunchOptions ().setHeadless (false )
3227 .setArgs (Arrays .asList ("--no-sandbox" , "--disable-extensions" , "--disable-gpu" ))
3328 );
3429 }
@@ -37,6 +32,10 @@ static void setUpBrowser() {
3732 void setUp () {
3833 browserContext = browser .newContext ();
3934 page = browserContext .newPage ();
35+
36+ page .navigate ("https://practicesoftwaretesting.com" );
37+ page .getByTestId ("product-name" ).waitFor ();
38+
4039 }
4140
4241 @ AfterEach
@@ -50,16 +49,6 @@ static void tearDown() {
5049 playwright .close ();
5150 }
5251
53- @ BeforeEach
54- void openHomePage () {
55- page .route ("**/products/search?q=pliers" ,
56- route -> route .fulfill (new Route .FulfillOptions ()
57- .setBody ("{\" message\" : \" Internal Server Error\" }" )
58- .setStatus (404 ))
59- );
60- page .navigate ("https://practicesoftwaretesting.com" );
61- }
62-
6352 @ DisplayName ("Playwright allows us to mock out API responses" )
6453 @ Nested
6554 class MockingAPIResponses {
@@ -73,9 +62,13 @@ void whenASingleItemIsFound() {
7362 .setStatus (200 ))
7463 );
7564
76- page .navigate ("https://practicesoftwaretesting.com" );
77- page .getByPlaceholder ("Search" ).fill ("pliers" );
78- page .getByPlaceholder ("Search" ).press ("Enter" );
65+ var searchBox = page .getByPlaceholder ("Search" );
66+ searchBox .waitFor (); // Wait for element to be ready
67+ searchBox .fill ("pliers" );
68+ searchBox .press ("Enter" );
69+
70+ page .waitForResponse ("**/products/search?q=pliers" , () -> {
71+ });
7972
8073 assertThat (page .getByTestId ("product-name" )).hasCount (1 );
8174 assertThat (page .getByTestId ("product-name" )
@@ -91,10 +84,13 @@ void whenNoItemsAreFound() {
9184 .setBody (MockSearchResponses .RESPONSE_WITH_NO_ENTRIES )
9285 .setStatus (200 ))
9386 );
87+ var searchBox = page .getByPlaceholder ("Search" );
88+ searchBox .waitFor (); // Wait for element to be ready
89+ searchBox .fill ("pliers" );
90+ searchBox .press ("Enter" );
9491
95- page .navigate ("https://practicesoftwaretesting.com" );
96- page .getByPlaceholder ("Search" ).fill ("pliers" );
97- page .getByPlaceholder ("Search" ).press ("Enter" );
92+ page .waitForResponse ("**/products/search?q=pliers" , () -> {
93+ });
9894
9995 assertThat (page .getByTestId ("product-name" )).isHidden ();
10096 assertThat (page .getByTestId ("search_completed" )).hasText ("There are no products found." );
0 commit comments