fix: allow configuring environment variables (#176)
This commit is contained in:
@@ -121,7 +121,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Specify environment variables that will be visible to the browser. Defaults to {@code process.env}.
|
||||
*/
|
||||
public String env;
|
||||
public Map<String, String> env;
|
||||
/**
|
||||
* **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is {@code true}, the {@code headless} option will be set {@code false}.
|
||||
*/
|
||||
@@ -183,7 +183,7 @@ public interface BrowserType {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
public LaunchOptions withEnv(String env) {
|
||||
public LaunchOptions withEnv(Map<String, String> env) {
|
||||
this.env = env;
|
||||
return this;
|
||||
}
|
||||
@@ -360,7 +360,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Specify environment variables that will be visible to the browser. Defaults to {@code process.env}.
|
||||
*/
|
||||
public String env;
|
||||
public Map<String, String> env;
|
||||
/**
|
||||
* **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is {@code true}, the {@code headless} option will be set {@code false}.
|
||||
*/
|
||||
@@ -491,7 +491,7 @@ public interface BrowserType {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
public LaunchPersistentContextOptions withEnv(String env) {
|
||||
public LaunchPersistentContextOptions withEnv(Map<String, String> env) {
|
||||
this.env = env;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -86,10 +86,6 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
}
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
if (options.extraHTTPHeaders != null) {
|
||||
params.remove("extraHTTPHeaders");
|
||||
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||
}
|
||||
JsonElement result = sendMessage("newContext", params);
|
||||
BrowserContextImpl context = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
|
||||
contexts.add(context);
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.microsoft.playwright.BrowserType;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
import static com.microsoft.playwright.impl.Serialization.toProtocol;
|
||||
|
||||
class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
||||
BrowserTypeImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
@@ -51,10 +52,6 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
||||
options = new LaunchPersistentContextOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
if (options.extraHTTPHeaders != null) {
|
||||
params.remove("extraHTTPHeaders");
|
||||
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||
}
|
||||
params.addProperty("userDataDir", userDataDir.toString());
|
||||
JsonObject json = sendMessage("launchPersistentContext", params).getAsJsonObject();
|
||||
return connection.getExistingObject(json.getAsJsonObject("context").get("guid").getAsString());
|
||||
|
||||
@@ -41,6 +41,7 @@ class Serialization {
|
||||
.registerTypeAdapter(Page.EmulateMediaParams.Media.class, new MediaSerializer())
|
||||
.registerTypeAdapter(Optional.class, new OptionalSerializer())
|
||||
.registerTypeHierarchyAdapter(JSHandleImpl.class, new HandleSerializer())
|
||||
.registerTypeHierarchyAdapter(Map.class, new StringMapSerializer())
|
||||
.registerTypeAdapter(Path.class, new PathSerializer()).create();
|
||||
}
|
||||
return gson;
|
||||
@@ -275,6 +276,16 @@ class Serialization {
|
||||
}
|
||||
}
|
||||
|
||||
private static class StringMapSerializer implements JsonSerializer<Map<String, String>> {
|
||||
@Override
|
||||
public JsonElement serialize(Map<String, String> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (!"java.util.Map<java.lang.String, java.lang.String>".equals(typeOfSrc.getTypeName())) {
|
||||
throw new PlaywrightException("Unexpected map type: " + typeOfSrc);
|
||||
}
|
||||
return toProtocol(src);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MediaSerializer implements JsonSerializer<Page.EmulateMediaParams.Media> {
|
||||
@Override
|
||||
public JsonElement serialize(Page.EmulateMediaParams.Media src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
@@ -57,6 +57,10 @@ public class TestBase {
|
||||
return options;
|
||||
}
|
||||
|
||||
static void currentBrowserType(Playwright playwright) {
|
||||
|
||||
}
|
||||
|
||||
static void launchBrowser(BrowserType.LaunchOptions launchOptions) {
|
||||
playwright = Playwright.create();
|
||||
|
||||
@@ -133,8 +137,10 @@ public class TestBase {
|
||||
|
||||
@AfterEach
|
||||
void closeContext() {
|
||||
context.close();
|
||||
context = null;
|
||||
page = null;
|
||||
if (context != null) {
|
||||
context.close();
|
||||
context = null;
|
||||
page = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,26 +25,10 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestBrowser {
|
||||
private static Playwright playwright;
|
||||
private Browser browser;
|
||||
private boolean isChromium;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
playwright = Playwright.create();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions();
|
||||
browser = playwright.chromium().launch(options);
|
||||
isChromium = true;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
browser.close();
|
||||
public class TestBrowser extends TestBase {
|
||||
@Override
|
||||
void createContextAndPage() {
|
||||
// Do not create anything.
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,12 +58,15 @@ public class TestBrowser {
|
||||
page.close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void versionShouldWork() {
|
||||
if (isChromium)
|
||||
if (isChromium()) {
|
||||
assertTrue(Pattern.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$", browser.version()));
|
||||
else
|
||||
} else if (isWebKit()) {
|
||||
assertTrue(Pattern.matches("^\\d+\\.\\d+", browser.version()));
|
||||
} else if (isFirefox()) {
|
||||
// It can be 85.0b1 in Firefox.
|
||||
assertTrue(Pattern.matches("^\\d+\\.\\d+.*", browser.version()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.microsoft.playwright.Utils.mapOf;
|
||||
|
||||
public class TestLaunch extends TestBase {
|
||||
|
||||
@BeforeAll
|
||||
// Hide base class method to not launch browser.
|
||||
static void launchBrowser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
void createContextAndPage() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Test
|
||||
void passEnvVar() {
|
||||
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions();
|
||||
options.withEnv(mapOf("DEBUG", "pw:protocol"));
|
||||
launchBrowser(options);
|
||||
}
|
||||
}
|
||||
@@ -279,12 +279,12 @@ class Types {
|
||||
add("Route.fulfill.response.body", "string|Buffer", "String");
|
||||
add("BrowserType.launch.options.ignoreDefaultArgs", "boolean|Array<string>", "Boolean");
|
||||
add("BrowserType.launch.options.firefoxUserPrefs", "Object<string, string|number|boolean>", "String");
|
||||
add("BrowserType.launch.options.env", "Object<string, string|number|boolean>", "String");
|
||||
add("BrowserType.launch.options.env", "Object<string, string|number|boolean>", "Map<String, String>");
|
||||
add("BrowserType.launchPersistentContext.options.ignoreDefaultArgs", "boolean|Array<string>", "String");
|
||||
add("BrowserType.launchPersistentContext.options.env", "Object<string, string|number|boolean>", "String");
|
||||
add("BrowserType.launchPersistentContext.options.env", "Object<string, string|number|boolean>", "Map<String, String>");
|
||||
add("BrowserType.launchServer.options.ignoreDefaultArgs", "boolean|Array<string>", "String");
|
||||
add("BrowserType.launchServer.options.firefoxUserPrefs", "Object<string, string|number|boolean>", "String");
|
||||
add("BrowserType.launchServer.options.env", "Object<string, string|number|boolean>", "String");
|
||||
add("BrowserType.launchServer.options.env", "Object<string, string|number|boolean>", "Map<String, String>");
|
||||
add("Logger.log.message", "string|Error", "String");
|
||||
|
||||
add("Browser.newContext.options.geolocation.latitude", "number", "double");
|
||||
|
||||
Reference in New Issue
Block a user