Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions driver/src/main/java/com/microsoft/playwright/impl/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public Path driverPath() {
return driverDir().resolve(cliFileName);
}

public static void setRequiredEnvironmentVariables(ProcessBuilder pb) {
if (!pb.environment().containsKey("PW_CLI_TARGET_LANG")) {
pb.environment().put("PW_CLI_TARGET_LANG", "java");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like PW_CLI_TARGET_LANG /PW_CLI_TARGET_LANG_VERSION are used beyond just codegen, let's rename them (in separate pr) to PW_LANG / PW_LANG_VERSION before it's too late?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this in a follow-up and on tip-of-tree only.

I filed an issue for myself regarding this: microsoft/playwright#11450

pb.environment().put("PW_CLI_TARGET_LANG_VERSION", getMajorJavaVersion());
}
}

private static String getMajorJavaVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
return version.substring(2, 3);
}
int dot = version.indexOf(".");
if (dot != -1) {
return version.substring(0, dot);
}
return version;
}

private static Driver createDriver() throws Exception {
String pathFromProperty = System.getProperty("playwright.cli.dir");
if (pathFromProperty != null) {
Expand Down
4 changes: 1 addition & 3 deletions playwright/src/main/java/com/microsoft/playwright/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public static void main(String[] args) throws IOException, InterruptedException
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap());
ProcessBuilder pb = new ProcessBuilder(driver.toString());
pb.command().addAll(asList(args));
if (!pb.environment().containsKey("PW_CLI_TARGET_LANG")) {
pb.environment().put("PW_CLI_TARGET_LANG", "java");
}
Driver.setRequiredEnvironmentVariables(pb);
String version = Playwright.class.getPackage().getImplementationVersion();
if (version != null) {
pb.environment().put("PW_CLI_DISPLAY_VERSION", version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static PlaywrightImpl create(CreateOptions options) {
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "run-driver");
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb.environment().putAll(env);
Driver.setRequiredEnvironmentVariables(pb);
Process p = pb.start();
Connection connection = new Connection(new PipeTransport(p.getInputStream(), p.getOutputStream()));
PlaywrightImpl result = connection.initializePlaywright();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
import static org.junit.jupiter.api.Assertions.*;

public class TestGlobalFetch extends TestBase {
@Test
void shouldHaveJavaInDefaultUesrAgent() throws ExecutionException, InterruptedException {
APIRequestContext request = playwright.request().newContext(new APIRequest.NewContextOptions());
Future<Server.Request> serverRequest = server.futureRequest("/empty.html");
APIResponse response = request.get(server.EMPTY_PAGE);
assertTrue(response.ok());
assertEquals(server.EMPTY_PAGE, response.url());
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
assertTrue(serverRequest.get().headers.get("user-agent").get(0).contains("java/" + version));
}

@Test
void fetchShouldWork() {
APIRequestContext request = playwright.request().newContext();
Expand Down
2 changes: 1 addition & 1 deletion scripts/CLI_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.18.0-beta-1642115083000
1.18.0-rc1