Skip to content

Commit 6652af2

Browse files
committed
Updated sample code
1 parent 2de8ad6 commit 6652af2

File tree

4 files changed

+613
-349
lines changed

4 files changed

+613
-349
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.serenitydojo.playwright;
2+
3+
import com.deque.html.axecore.playwright.AxeBuilder;
4+
import com.deque.html.axecore.results.AxeResults;
5+
import com.microsoft.playwright.*;
6+
import org.junit.jupiter.api.*;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class AccessibilityTest {
14+
15+
private static Playwright playwright;
16+
private static Browser browser;
17+
private static BrowserContext browserContext;
18+
19+
Page page;
20+
21+
@BeforeAll
22+
public static void setUpBrowser() {
23+
playwright = Playwright.create();
24+
browser = playwright.chromium().launch(
25+
new BrowserType.LaunchOptions()
26+
.setHeadless(true)
27+
.setArgs(Arrays.asList("--no-sandbox","--disable-extensions","--disable-gpu"))
28+
);
29+
browserContext = browser.newContext();
30+
}
31+
32+
@BeforeEach
33+
public void setUp() {
34+
page = browserContext.newPage();
35+
}
36+
37+
@AfterAll
38+
public static void tearDown() {
39+
browser.close();
40+
playwright.close();
41+
}
42+
43+
// This test will fail
44+
@Test
45+
@Disabled
46+
void homePageShouldNotHaveAccessibilityIssues() {
47+
page.navigate("https://practicesoftwaretesting.com");
48+
49+
AxeResults accessibilityScanResults = new AxeBuilder(page).analyze();
50+
51+
assertThat(accessibilityScanResults.getViolations()).isEmpty();
52+
}
53+
54+
@Test
55+
void navBarShouldNotHaveAnyAccessibilityIssues() {
56+
AxeResults accessibilityScanResults = new AxeBuilder(page)
57+
.include(List.of(".navbar-nav"))
58+
.analyze();
59+
60+
assertThat(accessibilityScanResults.getViolations()).isEmpty();
61+
}
62+
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.serenitydojo.playwright;
2+
3+
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.assertions.PlaywrightAssertions;
5+
import com.microsoft.playwright.options.AriaRole;
6+
import com.microsoft.playwright.options.LoadState;
7+
import com.microsoft.playwright.options.SelectOption;
8+
import org.assertj.core.api.Assertions;
9+
import org.junit.jupiter.api.*;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.regex.Pattern;
14+
15+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
16+
17+
public class AddingItemsToTheCartTest {
18+
19+
protected static Playwright playwright;
20+
protected static Browser browser;
21+
protected static BrowserContext browserContext;
22+
23+
Page page;
24+
25+
@BeforeAll
26+
static void setUpBrowser() {
27+
playwright = Playwright.create();
28+
browser = playwright.chromium().launch(
29+
new BrowserType.LaunchOptions()
30+
.setHeadless(false)
31+
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
32+
);
33+
playwright.selectors().setTestIdAttribute("data-test");
34+
}
35+
36+
@BeforeEach
37+
void setUp() {
38+
browserContext = browser.newContext();
39+
page = browserContext.newPage();
40+
}
41+
42+
@AfterEach
43+
void closeContext() {
44+
browserContext.close();
45+
}
46+
47+
@AfterAll
48+
static void tearDown() {
49+
browser.close();
50+
playwright.close();
51+
}
52+
53+
@DisplayName("Search for pliers")
54+
@Test
55+
void searchForPliers() {
56+
page.onConsoleMessage(msg -> System.out.println(msg.text()));
57+
58+
page.navigate("https://practicesoftwaretesting.com");
59+
page.getByPlaceholder("Search").fill("Pliers");
60+
page.getByPlaceholder("Search").press("Enter");
61+
62+
page.waitForLoadState();
63+
page.waitForCondition( () -> page.getByTestId("product-name").count() > 0);
64+
65+
List<String> products = page.getByTestId("product-name").allTextContents();
66+
Assertions.assertThat(products.get(0)).containsIgnoringCase("Pliers");
67+
68+
assertThat(page.locator(".card")).hasCount(4);
69+
70+
List<String> productNames = page.getByTestId("product-name").allTextContents();
71+
Assertions.assertThat(productNames).allMatch(name -> name.contains("Pliers"));
72+
73+
Locator outOfStockItem = page.locator(".card")
74+
.filter(new Locator.FilterOptions().setHasText("Out of stock"))
75+
.getByTestId("product-name");
76+
77+
assertThat(outOfStockItem).hasCount(1);
78+
assertThat(outOfStockItem).hasText("Long Nose Pliers");
79+
}
80+
81+
@Test
82+
@DisplayName("Chaining assertions on product search results with AssertJ")
83+
void chainedAssertionsOnProductSearchResults() {
84+
page.navigate("https://practicesoftwaretesting.com");
85+
86+
// Using AssertJ assertions to chain multiple checks on product search results
87+
Locator searchField = page.getByPlaceholder("Search");
88+
searchField.fill("Pliers");
89+
searchField.press("Enter");
90+
91+
92+
}
93+
94+
95+
}

0 commit comments

Comments
 (0)