Skip to content

Commit a1d0d7d

Browse files
committed
Live coding
1 parent 6a6644e commit a1d0d7d

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

src/test/java/com/serenitydojo/playwright/PlaywrightPageObjectTest.java

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ void withPageObjects() {
8787
@Nested
8888
class WhenAddingItemsToTheCart {
8989

90+
91+
SearchComponent searchComponent;
92+
ProductList productList;
93+
ProductDetails productDetails;
94+
NavBar navBar;
95+
CheckoutCart checkoutCart;
96+
97+
@BeforeEach
98+
void setUp() {
99+
searchComponent = new SearchComponent(page);
100+
productList = new ProductList(page);
101+
productDetails = new ProductDetails(page);
102+
navBar = new NavBar(page);
103+
checkoutCart = new CheckoutCart(page);
104+
}
105+
90106
@DisplayName("Without Page Objects")
91107
@Test
92108
void withoutPageObjects() {
@@ -115,12 +131,6 @@ void withoutPageObjects() {
115131

116132
@Test
117133
void withPageObjects() {
118-
SearchComponent searchComponent = new SearchComponent(page);
119-
ProductList productList = new ProductList(page);
120-
ProductDetails productDetails = new ProductDetails(page);
121-
NavBar navBar = new NavBar(page);
122-
CheckoutCart checkoutCart = new CheckoutCart(page);
123-
124134
searchComponent.searchBy("pliers");
125135
productList.viewProductDetails("Combination Pliers");
126136

@@ -140,6 +150,35 @@ void withPageObjects() {
140150
Assertions.assertThat(item.total()).isEqualTo(item.quantity() * item.price());
141151
});
142152
}
153+
154+
@Test
155+
void whenCheckingOutMultipleItems() {
156+
navBar.openHomePage();
157+
productList.viewProductDetails("Bolt Cutters");
158+
productDetails.increaseQuanityBy(2);
159+
productDetails.addToCart();
160+
161+
navBar.openHomePage();
162+
productList.viewProductDetails("Slip Joint Pliers");
163+
productDetails.addToCart();
164+
165+
navBar.openCart();
166+
167+
List<CartLineItem> lineItems = checkoutCart.getLineItems();
168+
169+
Assertions.assertThat(lineItems).hasSize(2);
170+
List<String> productNames = lineItems.stream().map(CartLineItem::title).toList();
171+
Assertions.assertThat(productNames).contains("Bolt Cutters","Slip Joint Pliers");
172+
173+
Assertions.assertThat(lineItems)
174+
.allSatisfy(item -> {
175+
Assertions.assertThat(item.quantity()).isGreaterThanOrEqualTo(1);
176+
Assertions.assertThat(item.price()).isGreaterThan(0.0);
177+
Assertions.assertThat(item.total()).isGreaterThan(0.0);
178+
Assertions.assertThat(item.total()).isEqualTo(item.quantity() * item.price());
179+
});
180+
181+
}
143182
}
144183

145184
class SearchComponent {
@@ -208,6 +247,10 @@ class NavBar {
208247
public void openCart() {
209248
page.getByTestId("nav-cart").click();
210249
}
250+
251+
public void openHomePage() {
252+
page.navigate("https://practicesoftwaretesting.com");
253+
}
211254
}
212255

213256
record CartLineItem(String title, int quantity, double price, double total) {}
@@ -219,20 +262,24 @@ class CheckoutCart {
219262
}
220263

221264
public List<CartLineItem> getLineItems() {
222-
page.locator("app-cart tbody tr").waitFor();
265+
page.locator("app-cart tbody tr").first().waitFor();
223266
return page.locator("app-cart tbody tr")
224267
.all()
225268
.stream()
226269
.map(
227270
row -> {
228-
String title = row.getByTestId("product-title").innerText();
271+
String title = trimmed(row.getByTestId("product-title").innerText());
229272
int quantity = Integer.parseInt(row.getByTestId("product-quantity").inputValue());
230273
double price = Double.parseDouble(price(row.getByTestId("product-price").innerText()));
231274
double linePrice = Double.parseDouble(price(row.getByTestId("line-price").innerText()));
232275
return new CartLineItem(title, quantity, price, linePrice);
233276
}
234277
).toList();
235278
}
279+
280+
private String trimmed(String value) {
281+
return value.strip().replaceAll("\u00A0", "");
282+
}
236283
}
237284

238285
private String price(String value) {

0 commit comments

Comments
 (0)