Skip to content

Commit

Permalink
[java] Making TestChromeDriver a subclass of ChromeDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jul 20, 2019
1 parent 85e17c1 commit 76daf60
Showing 1 changed file with 14 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.html5.LocalStorage;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.html5.LocationContext;
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.html5.WebStorage;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.html5.RemoteWebStorage;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand All @@ -45,33 +37,25 @@
* entire test suite. We do not use {@link org.openqa.selenium.chrome.ChromeDriver} since that starts and stops the service
* with each instance (and that is too expensive for our purposes).
*/
public class TestChromeDriver extends RemoteWebDriver implements WebStorage, LocationContext {
public class TestChromeDriver extends ChromeDriver {
private final static Logger LOG = Logger.getLogger(TestChromeDriver.class.getName());

private static ChromeDriverService service;
private RemoteWebStorage webStorage;
private RemoteLocationContext locationContext;

public TestChromeDriver(Capabilities capabilities) {
super(getServiceUrl(), chromeWithCustomCapabilities(capabilities));
webStorage = new RemoteWebStorage(getExecuteMethod());
locationContext = new RemoteLocationContext(getExecuteMethod());
super(getService(), chromeWithCustomCapabilities(capabilities));
}

private static URL getServiceUrl() {
private static ChromeDriverService getService() {
try {
if (service == null) {
Path logFile = Files.createTempFile("chromedriver", ".log");
service = new ChromeDriverService.Builder()
.withVerbose(true)
.withLogFile(logFile.toFile())
.build();
LOG.info("chromedriver will log to " + logFile);
service.start();
// Fugly.
Runtime.getRuntime().addShutdownHook(new Thread(() -> service.stop()));
}
return service.getUrl();
Path logFile = Files.createTempFile("chromedriver", ".log");
ChromeDriverService service = new ChromeDriverService.Builder()
.withVerbose(true)
.withLogFile(logFile.toFile())
.build();
LOG.info("chromedriver will log to " + logFile);
service.start();
// Fugly.
Runtime.getRuntime().addShutdownHook(new Thread(() -> service.stop()));
return service;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -103,24 +87,4 @@ public <X> X getScreenshotAs(OutputType<X> target) {
// ... and convert it.
return target.convertFromBase64Png(base64);
}

@Override
public LocalStorage getLocalStorage() {
return webStorage.getLocalStorage();
}

@Override
public SessionStorage getSessionStorage() {
return webStorage.getSessionStorage();
}

@Override
public Location location() {
return locationContext.location();
}

@Override
public void setLocation(Location location) {
locationContext.setLocation(location);
}
}

0 comments on commit 76daf60

Please sign in to comment.