99import org .junit .jupiter .api .parallel .ExecutionMode ;
1010
1111import java .util .Arrays ;
12+ import java .util .Comparator ;
1213import java .util .List ;
1314
1415import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
@@ -101,11 +102,11 @@ void shouldFilterProductsByCategory() {
101102
102103 page .waitForSelector (".card" ,
103104 new Page .WaitForSelectorOptions ().setState (WaitForSelectorState .VISIBLE ).setTimeout (2000 )
104- );
105+ );
105106
106107 var filteredProducts = page .getByTestId ("product-name" ).allInnerTexts ();
107108
108- Assertions .assertThat (filteredProducts ).contains ("Sheet Sander" , "Belt Sander" ,"Random Orbit Sander" );
109+ Assertions .assertThat (filteredProducts ).contains ("Sheet Sander" , "Belt Sander" , "Random Orbit Sander" );
109110
110111 }
111112 }
@@ -127,7 +128,7 @@ void shouldDisplayToasterMessage() {
127128 assertThat (page .getByRole (AriaRole .ALERT )).isVisible ();
128129 assertThat (page .getByRole (AriaRole .ALERT )).hasText ("Product added to shopping cart." );
129130
130- page .waitForCondition ( () -> page .getByRole (AriaRole .ALERT ).isHidden () );
131+ page .waitForCondition (() -> page .getByRole (AriaRole .ALERT ).isHidden ());
131132
132133 }
133134
@@ -137,7 +138,7 @@ void shouldUpdateCartItemCount() {
137138 page .getByText ("Bolt Cutters" ).click ();
138139 page .getByText ("Add to cart" ).click ();
139140
140- page .waitForCondition ( () -> page .getByTestId ("cart-quantity" ).textContent ().equals ("1" ));
141+ page .waitForCondition (() -> page .getByTestId ("cart-quantity" ).textContent ().equals ("1" ));
141142 // page.waitForSelector("[data-test=cart-quantity]:has-text('1')");
142143 }
143144
@@ -153,6 +154,36 @@ void shouldDisplayTheCartItemCount() {
153154 // Or
154155 page .waitForSelector ("[data-test='cart-quantity']:has-text('1')" );
155156 }
157+ }
158+
159+ @ Nested
160+ class WaitingForAPICalls {
161+
162+ @ Test
163+ void sortByDescendingPrice () {
164+ page .navigate ("https://practicesoftwaretesting.com" );
165+
166+ // Sort by descending price
167+ page .waitForResponse ("**/products?sort**" ,
168+ () -> {
169+ page .getByTestId ("sort" ).selectOption ("Price (High - Low)" );
170+ });
171+
172+ // Find all the prices on the page
173+ var productPrices = page .getByTestId ("product-price" )
174+ .allInnerTexts ()
175+ .stream ()
176+ .map (WaitingForAPICalls ::extractPrice )
177+ .toList ();
178+
179+ // Are the prices in the correct order
180+ Assertions .assertThat (productPrices )
181+ .isNotEmpty ()
182+ .isSortedAccordingTo (Comparator .reverseOrder ());
183+ }
156184
185+ private static double extractPrice (String price ) {
186+ return Double .parseDouble (price .replace ("$" , "" ));
187+ }
157188 }
158189}
0 commit comments