Skip to content

Commit 6b30c0b

Browse files
authored
fix: pass required env variables for new driver (#774)
Fixes #772
1 parent a006d51 commit 6b30c0b

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

driver/src/main/java/com/microsoft/playwright/impl/Driver.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ public Path driverPath() {
6565
return driverDir().resolve(cliFileName);
6666
}
6767

68+
public static void setRequiredEnvironmentVariables(ProcessBuilder pb) {
69+
if (!pb.environment().containsKey("PW_CLI_TARGET_LANG")) {
70+
pb.environment().put("PW_CLI_TARGET_LANG", "java");
71+
pb.environment().put("PW_CLI_TARGET_LANG_VERSION", getMajorJavaVersion());
72+
}
73+
}
74+
75+
private static String getMajorJavaVersion() {
76+
String version = System.getProperty("java.version");
77+
if (version.startsWith("1.")) {
78+
return version.substring(2, 3);
79+
}
80+
int dot = version.indexOf(".");
81+
if (dot != -1) {
82+
return version.substring(0, dot);
83+
}
84+
return version;
85+
}
86+
6887
private static Driver createDriver() throws Exception {
6988
String pathFromProperty = System.getProperty("playwright.cli.dir");
7089
if (pathFromProperty != null) {

playwright/src/main/java/com/microsoft/playwright/CLI.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public static void main(String[] args) throws IOException, InterruptedException
3232
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap());
3333
ProcessBuilder pb = new ProcessBuilder(driver.toString());
3434
pb.command().addAll(asList(args));
35-
if (!pb.environment().containsKey("PW_CLI_TARGET_LANG")) {
36-
pb.environment().put("PW_CLI_TARGET_LANG", "java");
37-
}
35+
Driver.setRequiredEnvironmentVariables(pb);
3836
String version = Playwright.class.getPackage().getImplementationVersion();
3937
if (version != null) {
4038
pb.environment().put("PW_CLI_DISPLAY_VERSION", version);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static PlaywrightImpl create(CreateOptions options) {
4141
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "run-driver");
4242
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
4343
pb.environment().putAll(env);
44+
Driver.setRequiredEnvironmentVariables(pb);
4445
Process p = pb.start();
4546
Connection connection = new Connection(new PipeTransport(p.getInputStream(), p.getOutputStream()));
4647
PlaywrightImpl result = connection.initializePlaywright();

playwright/src/test/java/com/microsoft/playwright/TestGlobalFetch.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
import static org.junit.jupiter.api.Assertions.*;
1717

1818
public class TestGlobalFetch extends TestBase {
19+
@Test
20+
void shouldHaveJavaInDefaultUesrAgent() throws ExecutionException, InterruptedException {
21+
APIRequestContext request = playwright.request().newContext(new APIRequest.NewContextOptions());
22+
Future<Server.Request> serverRequest = server.futureRequest("/empty.html");
23+
APIResponse response = request.get(server.EMPTY_PAGE);
24+
assertTrue(response.ok());
25+
assertEquals(server.EMPTY_PAGE, response.url());
26+
String version = System.getProperty("java.version");
27+
if (version.startsWith("1.")) {
28+
version = version.substring(2, 3);
29+
} else {
30+
int dot = version.indexOf(".");
31+
if (dot != -1) {
32+
version = version.substring(0, dot);
33+
}
34+
}
35+
assertTrue(serverRequest.get().headers.get("user-agent").get(0).contains("java/" + version));
36+
}
37+
1938
@Test
2039
void fetchShouldWork() {
2140
APIRequestContext request = playwright.request().newContext();

scripts/CLI_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.18.0-beta-1642115083000
1+
1.18.0-rc1

0 commit comments

Comments
 (0)