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 ;
@@ -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
@@ -57,40 +56,44 @@ class MockingAPIResponses {
5756 @ Test
5857 @ DisplayName ("When a search returns a single product" )
5958 void whenASingleItemIsFound () {
60-
61- // /products/search?q=Pliers
62- page .route ("**/products/search?q=Pliers" , route -> {
63- route .fulfill (
64- new Route .FulfillOptions ()
65- .setBody (MockSearchResponses .RESPONSE_WITH_A_SINGLE_ENTRY )
66- .setStatus (200 )
67- );
59+ page .route ("**/products/search?q=pliers" ,
60+ route -> route .fulfill (new Route .FulfillOptions ()
61+ .setBody (MockSearchResponses .RESPONSE_WITH_A_SINGLE_ENTRY )
62+ .setStatus (200 ))
63+ );
64+
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" , () -> {
6871 });
69- page .navigate ("https://practicesoftwaretesting.com" );
70- page .getByPlaceholder ("Search" ).fill ("Pliers" );
71- page .getByPlaceholder ("Search" ).press ("Enter" );
7272
7373 assertThat (page .getByTestId ("product-name" )).hasCount (1 );
74- assertThat (page .getByTestId ("product-name" )).hasText ("Super Pliers" );
74+ assertThat (page .getByTestId ("product-name" )
75+ .filter (new Locator .FilterOptions ().setHasText ("Super Pliers" )))
76+ .isVisible ();
7577 }
7678
7779 @ Test
7880 @ DisplayName ("When a search returns no products" )
7981 void whenNoItemsAreFound () {
80- page .route ("**/products/search?q=Pliers" , route -> {
81- route .fulfill (
82- new Route .FulfillOptions ()
83- .setBody (MockSearchResponses .RESPONSE_WITH_NO_ENTRIES )
84- .setStatus (200 )
85- );
82+ page .route ("**/products/search?q=pliers" ,
83+ route -> route .fulfill (new Route .FulfillOptions ()
84+ .setBody (MockSearchResponses .RESPONSE_WITH_NO_ENTRIES )
85+ .setStatus (200 ))
86+ );
87+ var searchBox = page .getByPlaceholder ("Search" );
88+ searchBox .waitFor (); // Wait for element to be ready
89+ searchBox .fill ("pliers" );
90+ searchBox .press ("Enter" );
91+
92+ page .waitForResponse ("**/products/search?q=pliers" , () -> {
8693 });
87- page .navigate ("https://practicesoftwaretesting.com" );
88- page .getByPlaceholder ("Search" ).fill ("Pliers" );
89- page .getByPlaceholder ("Search" ).press ("Enter" );
9094
91- assertThat (page .getByTestId ("product-name" )).hasCount ( 0 );
95+ assertThat (page .getByTestId ("product-name" )).isHidden ( );
9296 assertThat (page .getByTestId ("search_completed" )).hasText ("There are no products found." );
93-
9497 }
9598 }
9699}
0 commit comments