1
0
mirror of synced 2026-08-05 15:06:54 +00:00

fix: support slowMo options in browserType.connect (#434)

This commit is contained in:
Yury Semikhatsky
2021-05-07 23:21:07 +00:00
committed by GitHub
parent f143ebd053
commit 06b88b3d4b
4 changed files with 24 additions and 4 deletions
@@ -61,6 +61,7 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
try {
Duration timeout = Duration.ofDays(1);
Map<String, String> headers = Collections.emptyMap();
Duration slowMo = null;
if (options != null) {
if (options.timeout != null) {
timeout = Duration.ofMillis(Math.round(options.timeout));
@@ -68,8 +69,11 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
if (options.headers != null) {
headers = options.headers;
}
if (options.slowMo != null) {
slowMo = Duration.ofMillis(options.slowMo.intValue());
}
}
WebSocketTransport transport = new WebSocketTransport(new URI(wsEndpoint), headers, timeout);
WebSocketTransport transport = new WebSocketTransport(new URI(wsEndpoint), headers, timeout, slowMo);
Connection connection = new Connection(transport);
PlaywrightImpl playwright = (PlaywrightImpl) connection.waitForObjectWithKnownName("Playwright");
if (!playwright.initializer.has("preLaunchedBrowser")) {
@@ -38,7 +38,6 @@ class Utils {
.registerTypeAdapter(Optional.class, new OptionalSerializer())
.create();
String json = gson.toJson(f);
System.err.println("json = " + json);
return gson.fromJson(json, t);
}
@@ -33,6 +33,7 @@ import java.util.function.Consumer;
class WebSocketTransport implements Transport {
private final BlockingQueue<String> incoming = new LinkedBlockingQueue<>();
private final ClientConnection clientConnection;
private final Duration slowMo;
private boolean isClosed;
private volatile Exception lastError;
ListenerCollection<EventType> listeners = new ListenerCollection<>();
@@ -63,7 +64,7 @@ class WebSocketTransport implements Transport {
}
}
WebSocketTransport(URI uri, Map<String, String> headers, Duration timeout) {
WebSocketTransport(URI uri, Map<String, String> headers, Duration timeout, Duration slowMo) {
clientConnection = new ClientConnection(uri);
for (Map.Entry<String, String> entry : headers.entrySet()) {
clientConnection.addHeader(entry.getKey(), entry.getValue());
@@ -75,6 +76,7 @@ class WebSocketTransport implements Transport {
} catch (InterruptedException e) {
throw new PlaywrightException("Failed to connect", e);
}
this.slowMo = slowMo;
}
@Override
@@ -87,7 +89,11 @@ class WebSocketTransport implements Transport {
public String poll(Duration timeout) {
checkIfClosed();
try {
return incoming.poll(timeout.toMillis(), TimeUnit.MILLISECONDS);
String message = incoming.poll(timeout.toMillis(), TimeUnit.MILLISECONDS);
if (slowMo != null && message != null) {
Thread.sleep(slowMo.toMillis());
}
return message;
} catch (InterruptedException e) {
throw new PlaywrightException("Failed to read message", e);
}
@@ -116,6 +116,17 @@ public class TestBrowserTypeConnect extends TestBase {
}
}
@Test
void shouldSupportSlowMo() {
Browser browser = browserType.connect(wsEndpoint,
new BrowserType.ConnectOptions().setSlowMo(1));
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
assertEquals(121, page.evaluate("11 * 11"));
page.navigate(server.EMPTY_PAGE);
browser.close();
}
@Test
void shouldBeAbleToConnectTwoBrowsersAtTheSameTime() {
Browser browser1 = browserType.connect(wsEndpoint);