feat: accept PLAYWRIGHT_JAVA_SRC in Playwright.create (#980)
This commit is contained in:
@@ -82,7 +82,7 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
|||||||
|
|
||||||
JsonObject json = sendMessage("connect", params).getAsJsonObject();
|
JsonObject json = sendMessage("connect", params).getAsJsonObject();
|
||||||
JsonPipe pipe = connection.getExistingObject(json.getAsJsonObject("pipe").get("guid").getAsString());
|
JsonPipe pipe = connection.getExistingObject(json.getAsJsonObject("pipe").get("guid").getAsString());
|
||||||
Connection connection = new Connection(pipe);
|
Connection connection = new Connection(pipe, this.connection.env);
|
||||||
PlaywrightImpl playwright = connection.initializePlaywright();
|
PlaywrightImpl playwright = connection.initializePlaywright();
|
||||||
if (!playwright.initializer.has("preLaunchedBrowser")) {
|
if (!playwright.initializer.has("preLaunchedBrowser")) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ public class Connection {
|
|||||||
isLogging = (debug != null) && debug.contains("pw:channel");
|
isLogging = (debug != null) && debug.contains("pw:channel");
|
||||||
}
|
}
|
||||||
LocalUtils localUtils;
|
LocalUtils localUtils;
|
||||||
|
final Map<String, String> env;
|
||||||
|
|
||||||
class Root extends ChannelOwner {
|
class Root extends ChannelOwner {
|
||||||
Root(Connection connection) {
|
Root(Connection connection) {
|
||||||
@@ -79,13 +80,14 @@ public class Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection(Transport transport) {
|
Connection(Transport transport, Map<String, String> env) {
|
||||||
|
this.env = env;
|
||||||
if (isLogging) {
|
if (isLogging) {
|
||||||
transport = new TransportLogger(transport);
|
transport = new TransportLogger(transport);
|
||||||
}
|
}
|
||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
root = new Root(this);
|
root = new Root(this);
|
||||||
stackTraceCollector = StackTraceCollector.createFromEnv();
|
stackTraceCollector = StackTraceCollector.createFromEnv(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isCollectingStacks() {
|
boolean isCollectingStacks() {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
|
|||||||
pb.environment().putAll(env);
|
pb.environment().putAll(env);
|
||||||
Driver.setRequiredEnvironmentVariables(pb);
|
Driver.setRequiredEnvironmentVariables(pb);
|
||||||
Process p = pb.start();
|
Process p = pb.start();
|
||||||
Connection connection = new Connection(new PipeTransport(p.getInputStream(), p.getOutputStream()));
|
Connection connection = new Connection(new PipeTransport(p.getInputStream(), p.getOutputStream()), env);
|
||||||
PlaywrightImpl result = connection.initializePlaywright();
|
PlaywrightImpl result = connection.initializePlaywright();
|
||||||
result.driverProcess = p;
|
result.driverProcess = p;
|
||||||
result.initSharedSelectors(null);
|
result.initSharedSelectors(null);
|
||||||
|
|||||||
@@ -28,18 +28,25 @@ import java.util.*;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
class StackTraceCollector {
|
class StackTraceCollector {
|
||||||
|
static final String PLAYWRIGHT_JAVA_SRC = "PLAYWRIGHT_JAVA_SRC";
|
||||||
private final List<Path> srcDirs;
|
private final List<Path> srcDirs;
|
||||||
private final Map<Path, String> classToSourceCache = new HashMap<>();
|
private final Map<Path, String> classToSourceCache = new HashMap<>();
|
||||||
|
|
||||||
static StackTraceCollector createFromEnv() {
|
static StackTraceCollector createFromEnv(Map<String, String> env) {
|
||||||
String srcRoots = System.getenv("PLAYWRIGHT_JAVA_SRC");
|
String srcRoots = null;
|
||||||
|
if (env != null) {
|
||||||
|
srcRoots = env.get(PLAYWRIGHT_JAVA_SRC);
|
||||||
|
}
|
||||||
|
if (srcRoots == null) {
|
||||||
|
srcRoots = System.getenv(PLAYWRIGHT_JAVA_SRC);
|
||||||
|
}
|
||||||
if (srcRoots == null) {
|
if (srcRoots == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<Path> srcDirs = Arrays.stream(srcRoots.split(File.pathSeparator)).map(p -> Paths.get(p)).collect(Collectors.toList());
|
List<Path> srcDirs = Arrays.stream(srcRoots.split(File.pathSeparator)).map(p -> Paths.get(p)).collect(Collectors.toList());
|
||||||
for (Path srcDir: srcDirs) {
|
for (Path srcDir: srcDirs) {
|
||||||
if (!Files.exists(srcDir.toAbsolutePath())) {
|
if (!Files.exists(srcDir.toAbsolutePath())) {
|
||||||
throw new PlaywrightException("Source location specified in PLAYWRIGHT_JAVA_SRC doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
throw new PlaywrightException("Source location specified in " + PLAYWRIGHT_JAVA_SRC + " doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new StackTraceCollector(srcDirs);
|
return new StackTraceCollector(srcDirs);
|
||||||
|
|||||||
@@ -72,8 +72,12 @@ public class TestBase {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Playwright.CreateOptions playwrightOptions() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
void initBrowserType() {
|
void initBrowserType() {
|
||||||
playwright = Playwright.create();
|
playwright = Playwright.create(playwrightOptions());
|
||||||
browserType = Utils.getBrowserTypeFromEnv(playwright);
|
browserType = Utils.getBrowserTypeFromEnv(playwright);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
package com.microsoft.playwright;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.microsoft.playwright.Utils.mapOf;
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class TestJavaSourceLocationInConstructor extends TestBase {
|
||||||
|
private static final String SRC_DIRS = System.getenv("PLAYWRIGHT_JAVA_SRC") == null ? "src/test/java" : System.getenv("PLAYWRIGHT_JAVA_SRC");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Playwright.CreateOptions playwrightOptions() {
|
||||||
|
return new Playwright.CreateOptions().setEnv(mapOf("PLAYWRIGHT_JAVA_SRC", SRC_DIRS));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSupportSourcesLocationPassedToPlaywrightCreate(@TempDir Path tmpDir) throws IOException {
|
||||||
|
context.tracing().start(new Tracing.StartOptions().setSources(true));
|
||||||
|
page.navigate(server.EMPTY_PAGE);
|
||||||
|
page.setContent("<button>Click</button>");
|
||||||
|
page.click("'Click'");
|
||||||
|
Path trace = tmpDir.resolve("trace1.zip");
|
||||||
|
context.tracing().stop(new Tracing.StopOptions().setPath(trace));
|
||||||
|
|
||||||
|
Map<String, byte[]> entries = Utils.parseZip(trace);
|
||||||
|
Map<String, byte[]> sources = entries.entrySet().stream().filter(e -> e.getKey().endsWith(".txt")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
assertEquals(1, sources.size());
|
||||||
|
|
||||||
|
String path = getClass().getName().replace('.', File.separatorChar);
|
||||||
|
String[] srcRoots = SRC_DIRS.split(File.pathSeparator);
|
||||||
|
// Resolve in the last specified source dir.
|
||||||
|
Path sourceFile = Paths.get(srcRoots[srcRoots.length - 1], path + ".java");
|
||||||
|
byte[] thisFile = Files.readAllBytes(sourceFile);
|
||||||
|
assertEquals(new String(thisFile, UTF_8), new String(sources.values().iterator().next(), UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user