Skip to content

Commit

Permalink
[java] Partially fixing DevTools tests by reusing the same session.
Browse files Browse the repository at this point in the history
Closing the connection was a really bad idea. But detaching from the target
seriously hits performance. So let's reuse the same session as long as possible.

Unfortunately tests sometimes hand in DevTools.send with
java.util.concurrent.TimeoutException, I could not figure the pattern yet.
  • Loading branch information
barancev committed Jul 21, 2019
1 parent 3a304f6 commit 565006f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class ChromiumDriver extends RemoteWebDriver
private final TouchScreen touchScreen;
private final RemoteNetworkConnection networkConnection;
private final Optional<Connection> connection;
private final Optional<DevTools> devTools;

protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilities, String capabilityKey) {
super(commandExecutor, capabilities);
Expand All @@ -82,6 +83,7 @@ protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilit
factory,
getCapabilities(),
capabilityKey);
devTools = connection.map(DevTools::new);
}

@Override
Expand Down Expand Up @@ -155,8 +157,7 @@ public Map<String, Object> executeCdpCommand(String commandName, Map<String, Obj

@Override
public DevTools getDevTools() {
return connection.map(DevTools::new)
.orElseThrow(() -> new WebDriverException("Unable to create DevTools connection"));
return devTools.orElseThrow(() -> new WebDriverException("Unable to create DevTools connection"));
}

public String getCastSinks() {
Expand Down
9 changes: 8 additions & 1 deletion java/client/src/org/openqa/selenium/devtools/DevTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public DevTools(Connection connection) {

@Override
public void close() {
connection.close();
connection.sendAndWait(
cdpSession, Target.detachFromTarget(Optional.of(cdpSession), Optional.empty()), timeout);
}

public <X> X send(Command<X> command) {
Expand All @@ -61,6 +62,12 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
connection.addListener(event, handler);
}

public void createSessionIfThereIsNoOne() {
if (cdpSession == null) {
createSession();
}
}

public void createSession() {
// Figure out the targets.
List<TargetInfo> infos = connection.sendAndWait(cdpSession, Target.getTargets(), timeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package org.openqa.selenium.devtools;

import static org.assertj.core.api.Assumptions.assumeThat;
import static org.openqa.selenium.testing.Safely.safelyCall;

import org.assertj.core.api.Assumptions;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.testing.JUnit4TestBase;

Expand All @@ -35,11 +32,6 @@ public void setUp() {
assumeThat(driver).isInstanceOf(HasDevTools.class);

devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
}

@After
public void tearDown() {
safelyCall(() -> devTools.close());
devTools.createSessionIfThereIsNoOne();
}
}

0 comments on commit 565006f

Please sign in to comment.