Skip to content

Commit 540b70e

Browse files
committed
Parallel execution
1 parent e9a853b commit 540b70e

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

src/test/java/com/serenitydojo/playwright/toolshop/catalog/AddToCartTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import org.assertj.core.api.Assertions;
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
8-
import org.junit.jupiter.api.parallel.Execution;
9-
import org.junit.jupiter.api.parallel.ExecutionMode;
108

119
import java.util.List;
1210

13-
@Execution(ExecutionMode.SAME_THREAD)
1411
public class AddToCartTest extends PlaywrightTestCase {
1512

1613
SearchComponent searchComponent;

src/test/java/com/serenitydojo/playwright/toolshop/catalog/SearchForProductsTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.serenitydojo.playwright.toolshop.catalog;
22

3-
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
43
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.ProductList;
54
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.SearchComponent;
5+
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
66
import org.assertj.core.api.Assertions;
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.DisplayName;
99
import org.junit.jupiter.api.Nested;
1010
import org.junit.jupiter.api.Test;
11-
import org.junit.jupiter.api.parallel.Execution;
12-
import org.junit.jupiter.api.parallel.ExecutionMode;
1311

14-
@Execution(ExecutionMode.SAME_THREAD)
1512
@DisplayName("Searching for products")
1613
public class SearchForProductsTest extends PlaywrightTestCase {
1714

src/test/java/com/serenitydojo/playwright/toolshop/contact/ContactFormTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.DisplayName;
88
import org.junit.jupiter.api.Test;
9-
import org.junit.jupiter.api.parallel.Execution;
10-
import org.junit.jupiter.api.parallel.ExecutionMode;
119
import org.junit.jupiter.params.ParameterizedTest;
1210
import org.junit.jupiter.params.provider.ValueSource;
1311

@@ -17,7 +15,6 @@
1715

1816
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
1917

20-
@Execution(ExecutionMode.SAME_THREAD)
2118
public class ContactFormTest extends PlaywrightTestCase {
2219

2320
ContactForm contactForm;
@@ -71,7 +68,7 @@ void mandatoryFields(String fieldName) {
7168

7269
@DisplayName("The message must be at least 50 characters long")
7370
@Test
74-
void messageField() {
71+
void messageTooShort() {
7572

7673
contactForm.setFirstName("Sarah-Jane");
7774
contactForm.setLastName("Smith");
@@ -80,10 +77,25 @@ void messageField() {
8077
contactForm.selectSubject("Warranty");
8178

8279
contactForm.submitForm();
83-
;
8480

8581
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Message must be minimal 50 characters");
8682
}
8783

84+
@DisplayName("The email address must be correctly formatted")
85+
@ParameterizedTest
86+
@ValueSource(strings = {"not-an-email", "not-an.email.com", "notanemail"})
87+
void invalidEmailField(String invalidEmail) {
88+
89+
contactForm.setFirstName("Sarah-Jane");
90+
contactForm.setLastName("Smith");
91+
contactForm.setEmail(invalidEmail);
92+
contactForm.setMessage("A very long message to the warranty service about a warranty on a product!");
93+
contactForm.selectSubject("Warranty");
94+
95+
contactForm.submitForm();
96+
97+
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Email format is invalid");
98+
}
99+
88100

89101
}

src/test/java/com/serenitydojo/playwright/toolshop/fixtures/PlaywrightTestCase.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
import com.microsoft.playwright.*;
44
import org.junit.jupiter.api.AfterAll;
55
import org.junit.jupiter.api.AfterEach;
6-
import org.junit.jupiter.api.BeforeAll;
76
import org.junit.jupiter.api.BeforeEach;
87

98
import java.util.Arrays;
109

1110
public abstract class PlaywrightTestCase {
1211

13-
protected static Playwright playwright;
14-
protected static Browser browser;
15-
protected static BrowserContext browserContext;
12+
protected static ThreadLocal<Playwright> playwright
13+
= ThreadLocal.withInitial(() -> {
14+
Playwright playwright = Playwright.create();
15+
playwright.selectors().setTestIdAttribute("data-test");
16+
return playwright;
17+
}
18+
);
1619

17-
protected Page page;
18-
19-
@BeforeAll
20-
static void setUpBrowser() {
21-
playwright = Playwright.create();
22-
playwright.selectors().setTestIdAttribute("data-test");
23-
browser = playwright.chromium().launch(
20+
protected static ThreadLocal<Browser> browser = ThreadLocal.withInitial(() ->
21+
playwright.get().chromium().launch(
2422
new BrowserType.LaunchOptions().setHeadless(true)
2523
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
26-
);
27-
}
24+
)
25+
);
26+
27+
protected BrowserContext browserContext;
28+
29+
protected Page page;
2830

2931
@BeforeEach
3032
void setUpBrowserContext() {
31-
browserContext = browser.newContext();
33+
browserContext = browser.get().newContext();
3234
page = browserContext.newPage();
3335
}
3436

@@ -39,8 +41,11 @@ void closeContext() {
3941

4042
@AfterAll
4143
static void tearDown() {
42-
browser.close();
43-
playwright.close();
44+
browser.get().close();
45+
browser.remove();
46+
47+
playwright.get().close();
48+
playwright.remove();
4449
}
4550

4651
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
junit.jupiter.execution.parallel.enabled=false
2+
3+
junit.jupiter.execution.parallel.mode.default=concurrent
4+
junit.jupiter.execution.parallel.mode.classes.default=concurrent
5+
junit.jupiter.execution.parallel.console.mode=verbose
6+
7+
junit.jupiter.execution.parallel.config.strategy=dynamic
8+
junit.jupiter.execution.parallel.config.dynamic.factor=4
9+

0 commit comments

Comments
 (0)