Skip to content

Commit 1e23c02

Browse files
committed
Sample code for waits
1 parent 5b7d639 commit 1e23c02

File tree

1 file changed

+64
-47
lines changed

1 file changed

+64
-47
lines changed

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

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void setUpBrowser() {
2727
playwright = Playwright.create();
2828
playwright.selectors().setTestIdAttribute("data-test");
2929
browser = playwright.chromium().launch(
30-
new BrowserType.LaunchOptions().setHeadless(true)
30+
new BrowserType.LaunchOptions().setHeadless(false)
3131
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
3232
);
3333
}
@@ -49,48 +49,74 @@ static void tearDown() {
4949
playwright.close();
5050
}
5151

52-
@BeforeEach
53-
void openHomePage() {
54-
page.navigate("https://practicesoftwaretesting.com");
55-
page.waitForSelector("[data-test='product-name']");
52+
@Nested
53+
class WaitingForState {
54+
@BeforeEach
55+
void openHomePage() {
56+
page.navigate("https://practicesoftwaretesting.com");
57+
page.waitForSelector(".card-img-top");
58+
}
59+
60+
@Test
61+
void shouldShowAllProductNames() {
62+
List<String> productNames = page.getByTestId("product-name").allInnerTexts();
63+
Assertions.assertThat(productNames).contains("Pliers", "Bolt Cutters", "Hammer");
64+
}
65+
66+
@Test
67+
void shouldShowAllProductImages() {
68+
List<String> productImageTitles = page.locator(".card-img-top").all()
69+
.stream()
70+
.map(img -> img.getAttribute("alt"))
71+
.toList();
72+
73+
Assertions.assertThat(productImageTitles).contains("Pliers", "Bolt Cutters", "Hammer");
74+
}
5675
}
5776

58-
@DisplayName("Playwright waits automatically for elements by default")
5977
@Nested
60-
class AutoWaits {
78+
class AutomaticWaits {
79+
@BeforeEach
80+
void openHomePage() {
81+
page.navigate("https://practicesoftwaretesting.com");
82+
}
6183

6284
// Automatic wait
6385
@Test
64-
@DisplayName("It should wait for the filter checkbox options to appear before clicking")
86+
@DisplayName("Should wait for the filter checkbox options to appear before clicking")
6587
void shouldWaitForTheFilterCheckboxes() {
66-
var screwDriverFilter = page.getByLabel("Screwdriver");
67-
//
68-
// The checkboxes take an instant to render on the page
69-
//
70-
screwDriverFilter.click();
7188

72-
assertThat(screwDriverFilter).isChecked();
89+
var screwdriverFilter = page.getByLabel("Screwdriver");
90+
91+
screwdriverFilter.click();
92+
93+
assertThat(screwdriverFilter).isChecked();
7394
}
7495

75-
// Explicit wait
7696
@Test
77-
@DisplayName("Specify customized options when waiting for an element")
78-
void shouldWaitForAnElement() {
79-
80-
// Open the categories menu
97+
@DisplayName("Should filter products by category")
98+
void shouldFilterProductsByCategory() {
8199
page.getByRole(AriaRole.MENUBAR).getByText("Categories").click();
82100
page.getByRole(AriaRole.MENUBAR).getByText("Power Tools").click();
83101

84-
85102
page.waitForSelector(".card",
86-
new Page.WaitForSelectorOptions()
87-
.setState(WaitForSelectorState.VISIBLE)
88-
.setTimeout(5000));
103+
new Page.WaitForSelectorOptions().setState(WaitForSelectorState.VISIBLE).setTimeout(2000)
104+
);
105+
106+
var filteredProducts = page.getByTestId("product-name").allInnerTexts();
107+
108+
Assertions.assertThat(filteredProducts).contains("Sheet Sander", "Belt Sander","Random Orbit Sander");
89109

90-
assertThat(page.getByTestId("product-name").getByText("Sheet Sander")).isVisible();
110+
}
111+
}
112+
113+
@Nested
114+
class WaitingForElementsToAppearAndDisappear {
115+
@BeforeEach
116+
void openHomePage() {
117+
page.navigate("https://practicesoftwaretesting.com");
91118
}
92119

93-
// Wait for an element to appear or disappear
94120
@Test
95121
@DisplayName("It should display a toaster message when an item is added to the cart")
96122
void shouldDisplayToasterMessage() {
@@ -101,8 +127,18 @@ void shouldDisplayToasterMessage() {
101127
assertThat(page.getByRole(AriaRole.ALERT)).isVisible();
102128
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Product added to shopping cart.");
103129

104-
// Wait for the toaster message to disappear
105-
page.waitForCondition(() -> page.getByRole(AriaRole.ALERT).isHidden());
130+
page.waitForCondition( () -> page.getByRole(AriaRole.ALERT).isHidden() );
131+
132+
}
133+
134+
@Test
135+
@DisplayName("Should update the cart item count")
136+
void shouldUpdateCartItemCount() {
137+
page.getByText("Bolt Cutters").click();
138+
page.getByText("Add to cart").click();
139+
140+
page.waitForCondition( () -> page.getByTestId("cart-quantity").textContent().equals("1"));
141+
// page.waitForSelector("[data-test=cart-quantity]:has-text('1')");
106142
}
107143

108144
// Wait for an element to have a particular state
@@ -118,24 +154,5 @@ void shouldDisplayTheCartItemCount() {
118154
page.waitForSelector("[data-test='cart-quantity']:has-text('1')");
119155
}
120156

121-
122-
// Wait for an API call to respond after an action
123-
@Test
124-
@DisplayName("It should wait for the product list to update before reading the product names")
125-
void shouldWaitForProductListToUpdate() {
126-
127-
// Wait for the results to come back from the API call
128-
page.waitForResponse(
129-
"**/product*",
130-
() -> {
131-
page.getByLabel("Screwdriver").click();
132-
page.getByLabel("Chisels").click();
133-
});
134-
135-
List<String> matchingProducts = page.getByTestId("product-name").allTextContents();
136-
Assertions.assertThat(matchingProducts)
137-
.allMatch(product -> product.contains("Screwdriver") || product.contains("Chisels"));
138-
139-
}
140157
}
141-
}
158+
}

0 commit comments

Comments
 (0)