Skip to content

Commit b4edd03

Browse files
committed
Updated to use the UsePlaywright annotation
1 parent 93ae648 commit b4edd03

File tree

1 file changed

+10
-41
lines changed

1 file changed

+10
-41
lines changed

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

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.serenitydojo.playwright;
22

33
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.junit.UsePlaywright;
45
import com.microsoft.playwright.options.AriaRole;
56
import org.junit.jupiter.api.*;
67
import org.junit.jupiter.api.parallel.Execution;
@@ -16,53 +17,21 @@
1617

1718
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
1819

19-
@Execution(ExecutionMode.SAME_THREAD)
20+
@UsePlaywright(HeadlessChromeOptions.class)
2021
public class PlaywrightFormsTest {
2122

22-
protected static Playwright playwright;
23-
protected static Browser browser;
24-
protected static BrowserContext browserContext;
25-
26-
Page page;
27-
28-
@BeforeAll
29-
static void setUpBrowser() {
30-
playwright = Playwright.create();
31-
browser = playwright.chromium().launch(
32-
new BrowserType.LaunchOptions().setHeadless(true)
33-
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
34-
);
35-
}
36-
37-
@BeforeEach
38-
void setUp() {
39-
browserContext = browser.newContext();
40-
page = browserContext.newPage();
41-
}
42-
43-
@AfterEach
44-
void closeContext() {
45-
browserContext.close();
46-
}
47-
48-
@AfterAll
49-
static void tearDown() {
50-
browser.close();
51-
playwright.close();
52-
}
53-
5423
@DisplayName("Interacting with text fields")
5524
@Nested
5625
class WhenInteractingWithTextFields {
5726

5827
@BeforeEach
59-
void openContactPage() {
28+
void openContactPage(Page page) {
6029
page.navigate("https://practicesoftwaretesting.com/contact");
6130
}
6231

6332
@DisplayName("Complete the form")
6433
@Test
65-
void completeForm() throws URISyntaxException {
34+
void completeForm(Page page) throws URISyntaxException {
6635
var firstNameField = page.getByLabel("First name");
6736
var lastNameField = page.getByLabel("Last name");
6837
var emailNameField = page.getByLabel("Email");
@@ -93,7 +62,7 @@ void completeForm() throws URISyntaxException {
9362
@DisplayName("Mandatory fields")
9463
@ParameterizedTest
9564
@ValueSource(strings = {"First name", "Last name", "Email", "Message"})
96-
void mandatoryFields(String fieldName) {
65+
void mandatoryFields(String fieldName, Page page) {
9766
var firstNameField = page.getByLabel("First name");
9867
var lastNameField = page.getByLabel("Last name");
9968
var emailNameField = page.getByLabel("Email");
@@ -121,7 +90,7 @@ void mandatoryFields(String fieldName) {
12190

12291
@DisplayName("Text fields")
12392
@Test
124-
void textFieldValues() {
93+
void textFieldValues(Page page) {
12594
var messageField = page.getByLabel("Message");
12695

12796
messageField.fill("This is my message");
@@ -131,7 +100,7 @@ void textFieldValues() {
131100

132101
@DisplayName("Dropdown lists")
133102
@Test
134-
void dropdownFieldValues() {
103+
void dropdownFieldValues(Page page) {
135104
var subjectField = page.getByLabel("Subject");
136105

137106
subjectField.selectOption("Warranty");
@@ -141,7 +110,7 @@ void dropdownFieldValues() {
141110

142111
@DisplayName("File uploads")
143112
@Test
144-
void fileUploads() throws URISyntaxException {
113+
void fileUploads(Page page) throws URISyntaxException {
145114
var attachmentField = page.getByLabel("Attachment");
146115

147116
Path attachment = Paths.get(ClassLoader.getSystemResource("data/sample-data.txt").toURI());
@@ -156,7 +125,7 @@ void fileUploads() throws URISyntaxException {
156125

157126
@DisplayName("By CSS class")
158127
@Test
159-
void locateTheSendButtonByCssClass() {
128+
void locateTheSendButtonByCssClass(Page page) {
160129
page.locator("#first_name").fill("Sarah-Jane");
161130
page.locator(".btnSubmit").click();
162131
List<String> alertMessages = page.locator(".alert").allTextContents();
@@ -166,7 +135,7 @@ void locateTheSendButtonByCssClass() {
166135

167136
@DisplayName("By attribute")
168137
@Test
169-
void locateTheSendButtonByAttribute() {
138+
void locateTheSendButtonByAttribute(Page page) {
170139
page.locator("input[placeholder='Your last name *']").fill("Smith");
171140
assertThat(page.locator("#last_name")).hasValue("Smith");
172141
}

0 commit comments

Comments
 (0)