Skip to content

Commit a127bfe

Browse files
authored
cherry-pick(#978,#984): recent test fixes (#985)
1 parent 0608243 commit a127bfe

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ jobs:
3131
- name: Build & Install
3232
run: mvn -B install -D skipTests --no-transfer-progress
3333
- name: Run tests
34-
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end
34+
run: mvn test --no-transfer-progress --fail-at-end
3535
env:
3636
BROWSER: ${{ matrix.browser }}
3737
- name: Run tracing tests w/ sources
38-
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end -D test=*TestTracing*
38+
run: mvn test --no-transfer-progress --fail-at-end -D test=*TestTracing*
3939
env:
4040
BROWSER: ${{ matrix.browser }}
4141
PLAYWRIGHT_JAVA_SRC: src/test/java
42-
- name: Run driver throw tests
43-
run: mvn test -Dgroups=driverThrowTest --no-transfer-progress --fail-at-end
44-
env:
45-
BROWSER: ${{ matrix.browser }}
4642
- name: Test Spring Boot Starter
4743
shell: bash
4844
env:
@@ -83,12 +79,7 @@ jobs:
8379
- name: Build & Install
8480
run: mvn -B install -D skipTests --no-transfer-progress
8581
- name: Run tests
86-
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end
87-
env:
88-
BROWSER: chromium
89-
BROWSER_CHANNEL: ${{ matrix.browser-channel }}
90-
- name: Run driver throw tests
91-
run: mvn test -Dgroups=driverThrowTest --no-transfer-progress --fail-at-end
82+
run: mvn test --no-transfer-progress --fail-at-end
9283
env:
9384
BROWSER: chromium
9485
BROWSER_CHANNEL: ${{ matrix.browser-channel }}

driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,41 @@
1818

1919
import com.microsoft.playwright.impl.Driver;
2020
import com.microsoft.playwright.impl.DriverJar;
21-
import org.junit.jupiter.api.*;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.io.TempDir;
2324

25+
import java.io.IOException;
26+
import java.lang.reflect.Field;
27+
import java.net.MalformedURLException;
28+
import java.net.ServerSocket;
2429
import java.nio.file.Files;
2530
import java.nio.file.Path;
2631
import java.util.Collections;
2732
import java.util.HashMap;
2833
import java.util.Map;
2934
import java.util.concurrent.TimeUnit;
3035

31-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
32-
import static org.junit.jupiter.api.Assertions.assertEquals;
33-
import static org.junit.jupiter.api.Assertions.assertThrows;
34-
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
import static org.junit.jupiter.api.Assertions.*;
3537

3638
public class TestInstall {
39+
private static boolean isPortAvailable(int port) {
40+
try (ServerSocket ignored = new ServerSocket(port)) {
41+
return true;
42+
} catch (IOException ignored) {
43+
return false;
44+
}
45+
}
46+
47+
private static int unusedPort() {
48+
for (int i = 10000; i < 11000; i++) {
49+
if (isPortAvailable(i)) {
50+
return i;
51+
}
52+
}
53+
throw new RuntimeException("Cannot find unused local port");
54+
}
55+
3756
@BeforeEach
3857
void clearSystemProperties() {
3958
// Clear system property to ensure that the driver is loaded from jar.
@@ -44,16 +63,29 @@ void clearSystemProperties() {
4463
}
4564

4665
@Test
47-
@Tags({@Tag("isolated"), @Tag("driverThrowTest")})
48-
void shouldThrowWhenBrowserPathIsInvalid(@TempDir Path tmpDir) {
66+
void shouldThrowWhenBrowserPathIsInvalid(@TempDir Path tmpDir) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
4967
Map<String,String> env = new HashMap<>();
50-
env.put("PLAYWRIGHT_DOWNLOAD_HOST", "https://127.0.0.127");
68+
69+
// On macOS we can only use 127.0.0.1, so pick unused port instead.
70+
// https://superuser.com/questions/458875/how-do-you-get-loopback-addresses-other-than-127-0-0-1-to-work-on-os-x
71+
env.put("PLAYWRIGHT_DOWNLOAD_HOST", "https://127.0.0.1:" + unusedPort());
5172
// Make sure the browsers are not installed yet by pointing at an empty dir.
5273
env.put("PLAYWRIGHT_BROWSERS_PATH", tmpDir.toString());
5374
env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "false");
5475

55-
assertThrows(RuntimeException.class, () -> Driver.ensureDriverInstalled(env, true));
56-
assertThrows(RuntimeException.class, () -> Driver.ensureDriverInstalled(env, true));
76+
// Reset instance field value to null for the test.
77+
Field field = Driver.class.getDeclaredField("instance");
78+
field.setAccessible(true);
79+
Object value = field.get(Driver.class);
80+
field.set(Driver.class, null);
81+
82+
for (int i = 0; i < 2; i++){
83+
RuntimeException exception = assertThrows(RuntimeException.class, () -> Driver.ensureDriverInstalled(env, true));
84+
String message = exception.getMessage();
85+
assertTrue(message.contains("Failed to create driver"), message);
86+
}
87+
88+
field.set(Driver.class, value);
5789
}
5890

5991
@Test

0 commit comments

Comments
 (0)