Skip to content

Commit 441d9f0

Browse files
authored
test: port the upstream page and library tests (#2024)
1 parent 539bd5d commit 441d9f0

909 files changed

Lines changed: 254552 additions & 59 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/test.yml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ env:
1212
PW_MAX_RETRIES: 3
1313
jobs:
1414
dev:
15-
timeout-minutes: 30
15+
timeout-minutes: 90
1616
strategy:
1717
fail-fast: false
1818
matrix:
@@ -34,11 +34,11 @@ jobs:
3434
- name: Install browsers
3535
run: mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps" -f playwright/pom.xml --no-transfer-progress
3636
- name: Run tests
37-
run: mvn test --no-transfer-progress --fail-at-end -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
37+
run: ${{ runner.os == 'Linux' && 'xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24"' || '' }} mvn test --no-transfer-progress --fail-at-end ${{ runner.os == 'Windows' && '-D junit.parallel.factor=0.25' || '' }} -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
3838
env:
3939
BROWSER: ${{ matrix.browser }}
4040
- name: Run tracing tests w/ sources
41-
run: mvn test --no-transfer-progress --fail-at-end --projects=playwright -D test=*TestTracing* -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
41+
run: ${{ runner.os == 'Linux' && 'xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24"' || '' }} mvn test --no-transfer-progress --fail-at-end --projects=playwright -D test=*TestTracing* -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
4242
env:
4343
BROWSER: ${{ matrix.browser }}
4444
PLAYWRIGHT_JAVA_SRC: src/test/java
@@ -52,7 +52,7 @@ jobs:
5252
java -jar target/test-spring-boot*.jar
5353
5454
stable:
55-
timeout-minutes: 30
55+
timeout-minutes: 90
5656
strategy:
5757
fail-fast: false
5858
matrix:
@@ -81,13 +81,13 @@ jobs:
8181
- name: Install browsers
8282
run: mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps" -f playwright/pom.xml --no-transfer-progress
8383
- name: Run tests
84-
run: mvn test --no-transfer-progress --fail-at-end -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
84+
run: ${{ runner.os == 'Linux' && 'xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24"' || '' }} mvn test --no-transfer-progress --fail-at-end ${{ runner.os == 'Windows' && '-D junit.parallel.factor=0.25' || '' }} -D org.slf4j.simpleLogger.showDateTime=true -D org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
8585
env:
8686
BROWSER: chromium
8787
BROWSER_CHANNEL: ${{ matrix.browser-channel }}
8888

8989
Java_25:
90-
timeout-minutes: 30
90+
timeout-minutes: 90
9191
strategy:
9292
fail-fast: false
9393
matrix:
@@ -111,7 +111,7 @@ jobs:
111111
- name: Install browsers
112112
run: mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps" -f playwright/pom.xml --no-transfer-progress
113113
- name: Run tests
114-
run: mvn test --no-transfer-progress --fail-at-end
114+
run: xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" mvn test --no-transfer-progress --fail-at-end
115115
env:
116116
BROWSER: ${{ matrix.browser }}
117117
- name: Test Spring Boot Starter

‎driver/src/main/java/com/microsoft/playwright/impl/driver/jar/DriverJar.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,19 @@ public static URI getDriverResourceURI() throws URISyntaxException {
139139
}
140140

141141
void extractDriverToTempDir() throws URISyntaxException, IOException {
142-
extractResourceToDir("driver/package", driverTempDir.resolve("package"));
143-
if (preinstalledNodePath == null) {
144-
String platformResource = "driver/" + platformDir();
145-
if (DriverJar.class.getClassLoader().getResource(platformResource) == null) {
146-
throw new RuntimeException("Failed to find the bundled Node.js for platform '" + platformDir()
147-
+ "'. Add the com.microsoft.playwright:driver-bundle dependency, or set the "
148-
+ PLAYWRIGHT_NODEJS_PATH + " environment variable (or the playwright.nodejs.path system "
149-
+ "property) to point at a preinstalled Node.js.");
142+
// Concurrent extractions would share the zip file system of the jar, and the first one to finish closes it.
143+
synchronized (DriverJar.class) {
144+
extractResourceToDir("driver/package", driverTempDir.resolve("package"));
145+
if (preinstalledNodePath == null) {
146+
String platformResource = "driver/" + platformDir();
147+
if (DriverJar.class.getClassLoader().getResource(platformResource) == null) {
148+
throw new RuntimeException("Failed to find the bundled Node.js for platform '" + platformDir()
149+
+ "'. Add the com.microsoft.playwright:driver-bundle dependency, or set the "
150+
+ PLAYWRIGHT_NODEJS_PATH + " environment variable (or the playwright.nodejs.path system "
151+
+ "property) to point at a preinstalled Node.js.");
152+
}
153+
extractResourceToDir(platformResource, driverTempDir);
150154
}
151-
extractResourceToDir(platformResource, driverTempDir);
152155
}
153156
}
154157

‎playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,14 @@ private void updateWebSocketInterceptionPatterns() {
743743
void handleRoute(RouteImpl route) {
744744
Router.HandleResult handled = routes.handle(route);
745745
if (handled != Router.HandleResult.NoMatchingHandler) {
746-
updateInterceptionPatterns();
746+
try {
747+
updateInterceptionPatterns();
748+
} catch (PlaywrightException e) {
749+
// Like the Node.js client, ignore the error when the page or the context was closed meanwhile.
750+
if (!isSafeCloseError(e)) {
751+
throw e;
752+
}
753+
}
747754
}
748755
if (handled == Router.HandleResult.NoMatchingHandler || handled == Router.HandleResult.Fallback) {
749756
route.resume(null, true);

‎playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,8 @@ private void waitForLoadStateImpl(WaitUntilState state, WaitForLoadStateOptions
847847
}
848848

849849
List<Waitable<Void>> waitables = new ArrayList<>();
850+
waitables.add(page.createWaitForCloseHelper(true));
850851
waitables.add(new WaitForLoadStateHelper(state, logger));
851-
waitables.add(page.createWaitForCloseHelper());
852852
waitables.add(page.createWaitableTimeout(options.timeout));
853853
runUntil(() -> {}, new WaitableRace<>(waitables));
854854
}
@@ -982,7 +982,7 @@ private Response waitForNavigationImpl(Logger logger, Runnable code, WaitForNavi
982982
}
983983
logger.log("waiting for navigation " + matcher);
984984
waitables.add(new WaitForNavigationHelper(matcher, options.waitUntil, logger));
985-
waitables.add(page.createWaitForCloseHelper());
985+
waitables.add(page.createWaitForCloseHelper(true));
986986
waitables.add(page.createWaitableFrameDetach(this));
987987
waitables.add(page.createWaitableNavigationTimeout(options.timeout));
988988
return runUntil(code, new WaitableRace<>(waitables));

‎playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,14 @@ protected void handleEvent(String event, JsonObject params) {
225225
route.browserContext = browserContext;
226226
Router.HandleResult handled = routes.handle(route);
227227
if (handled != Router.HandleResult.NoMatchingHandler) {
228-
updateInterceptionPatterns();
228+
try {
229+
updateInterceptionPatterns();
230+
} catch (PlaywrightException e) {
231+
// Like the Node.js client, ignore the error when the page or the context was closed meanwhile.
232+
if (!isSafeCloseError(e)) {
233+
throw e;
234+
}
235+
}
229236
}
230237
if (handled == Router.HandleResult.NoMatchingHandler || handled == Router.HandleResult.Fallback) {
231238
browserContext.handleRoute(route);
@@ -1513,12 +1520,25 @@ <T> Waitable<T> createWaitableFrameDetach(Frame frame) {
15131520
}
15141521

15151522
<T> Waitable<T> createWaitForCloseHelper() {
1516-
return new WaitableRace<T>(asList(new WaitablePageClose(), new WaitablePageCrash()));
1523+
return createWaitForCloseHelper(false);
1524+
}
1525+
1526+
/** With {@code rejectIfClosed}, it is done at once when the page is already closed, like the navigation waiter of the Node.js client. */
1527+
<T> Waitable<T> createWaitForCloseHelper(boolean rejectIfClosed) {
1528+
return new WaitableRace<T>(asList(new WaitablePageClose(rejectIfClosed && isClosed), new WaitablePageCrash()));
15171529
}
15181530

15191531
private class WaitablePageClose<T> extends WaitableEvent<EventType, T> {
1520-
WaitablePageClose() {
1532+
private final boolean closedBefore;
1533+
1534+
WaitablePageClose(boolean closedBefore) {
15211535
super(PageImpl.this.listeners, EventType.CLOSE);
1536+
this.closedBefore = closedBefore;
1537+
}
1538+
1539+
@Override
1540+
public boolean isDone() {
1541+
return closedBefore || super.isDone();
15221542
}
15231543

15241544
@Override

‎playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static PlaywrightImpl createImpl(CreateOptions options, boolean forceNewD
4646
Driver.ensureDriverInstalled(env, true);
4747
try {
4848
ProcessBuilder pb = driver.createProcessBuilder();
49+
// The driver is installed once per JVM with the env of the first instance, so add the env of this one.
50+
pb.environment().putAll(env);
4951
pb.command().add("run-driver");
5052
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
5153
Process p = pb.start();

‎playwright/src/main/java/com/microsoft/playwright/impl/StackTraceCollector.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ JsonArray currentStackTrace() {
107107
// Find Playwright API call
108108
while (index < stack.length && stack[index].getClassName().startsWith("com.microsoft.playwright.")) {
109109
// hack for tests
110-
if (stack[index].getClassName().startsWith("com.microsoft.playwright.Test")) {
110+
if (stack[index].getClassName().startsWith("com.microsoft.playwright.Test")
111+
|| stack[index].getClassName().startsWith("com.microsoft.playwright.tests.")) {
111112
break;
112113
}
113114
index++;

0 commit comments

Comments
 (0)