11package com .serenitydojo .playwright ;
22
3+ import com .google .gson .Gson ;
4+ import com .google .gson .JsonArray ;
5+ import com .google .gson .JsonObject ;
36import com .microsoft .playwright .*;
7+ import org .assertj .core .api .Assertions ;
48import org .junit .jupiter .api .*;
59import org .junit .jupiter .api .parallel .Execution ;
610import org .junit .jupiter .api .parallel .ExecutionMode ;
11+ import org .junit .jupiter .params .ParameterizedTest ;
12+ import org .junit .jupiter .params .provider .MethodSource ;
13+
714
815import java .util .Arrays ;
16+ import java .util .HashMap ;
17+ import java .util .stream .Stream ;
918
1019import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
1120
@@ -23,7 +32,7 @@ static void setUpBrowser() {
2332 playwright = Playwright .create ();
2433 playwright .selectors ().setTestIdAttribute ("data-test" );
2534 browser = playwright .chromium ().launch (
26- new BrowserType .LaunchOptions ().setHeadless (true )
35+ new BrowserType .LaunchOptions ().setHeadless (false )
2736 .setArgs (Arrays .asList ("--no-sandbox" , "--disable-extensions" , "--disable-gpu" ))
2837 );
2938 }
@@ -88,4 +97,59 @@ void whenNoItemsAreFound() {
8897 assertThat (page .getByTestId ("search_completed" )).hasText ("There are no products found." );
8998 }
9099 }
100+
101+ @ Nested
102+ class MakingAPICalls {
103+
104+ record Product (String name , Double price ) {}
105+
106+ private static APIRequestContext requestContext ;
107+
108+ @ BeforeAll
109+ public static void setupRequestContext () {
110+ requestContext = playwright .request ().newContext (
111+ new APIRequest .NewContextOptions ()
112+ .setBaseURL ("https://api.practicesoftwaretesting.com" )
113+ .setExtraHTTPHeaders (new HashMap <>() {{
114+ put ("Accept" , "application/json" );
115+ }})
116+ );
117+ }
118+
119+ @ DisplayName ("Check presence of known products" )
120+ @ ParameterizedTest (name = "Checking product {0}" )
121+ @ MethodSource ("products" )
122+ void checkKnownProduct (Product product ) {
123+ page .fill ("[placeholder='Search']" , product .name );
124+ page .click ("button:has-text('Search')" );
125+
126+ // Check that the product appears with the correct name and price
127+
128+ Locator productCard = page .locator (".card" )
129+ .filter (
130+ new Locator .FilterOptions ()
131+ .setHasText (product .name )
132+ .setHasText (Double .toString (product .price ))
133+ );
134+ assertThat (productCard ).isVisible ();
135+ }
136+
137+ static Stream <Product > products () {
138+ APIResponse response = requestContext .get ("/products?page=2" );
139+ Assertions .assertThat (response .status ()).isEqualTo (200 );
140+
141+ JsonObject jsonObject = new Gson ().fromJson (response .text (), JsonObject .class );
142+ JsonArray data = jsonObject .getAsJsonArray ("data" );
143+
144+ return data .asList ().stream ()
145+ .map (jsonElement -> {
146+ JsonObject productJson = jsonElement .getAsJsonObject ();
147+ return new Product (
148+ productJson .get ("name" ).getAsString (),
149+ productJson .get ("price" ).getAsDouble ()
150+ );
151+ });
152+ }
153+
154+ }
91155}
0 commit comments