Skip to content

Commit

Permalink
Making ChromeDriver directly implement the interfaces representing fe…
Browse files Browse the repository at this point in the history
…atures it implements.

Currently, LocationContext and WebStorage are the only capabilities chromedriver returns, regardless of the kind of Chrome browser it is driving.

Augmentation never worked well on ChromeDriver, e.g. issue 6681 , and at times has been completely broken.

Because default ChromeDriver constructor starts a new session it cannot be augmented without consequences. A workaround has not been found.

Overriding getScreenshotAs() is dropped as redundant, RemoteWebDriver implementation works identically.

Signed-off-by: Jason Leyba <[email protected]>
  • Loading branch information
sevaseva authored and jleyba committed Apr 30, 2014
1 parent d18fcc7 commit f93ea6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
58 changes: 42 additions & 16 deletions java/client/src/org/openqa/selenium/chrome/ChromeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
package org.openqa.selenium.chrome;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.DriverCommand;
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.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.html5.RemoteWebStorage;
import org.openqa.selenium.remote.service.DriverCommandExecutor;

/**
Expand All @@ -39,17 +44,14 @@
*
* import static org.junit.Assert.assertEquals;
*
* import org.junit.After;
* import org.junit.AfterClass;
* import org.junit.Before;
* import org.junit.BeforeClass;
* import org.junit.*;
* import org.junit.runner.RunWith;
* import org.junit.runners.BlockJUnit4ClassRunner
* import org.junit.runners.JUnit4;
* import org.openqa.selenium.chrome.ChromeDriverService;
* import org.openqa.selenium.remote.DesiredCapabilities;
* import org.openqa.selenium.remote.RemoteWebDriver;
*
* {@literal @RunWith(BlockJUnit4ClassRunner.class)}
* {@literal @RunWith(JUnit4.class)}
* public class ChromeTest extends TestCase {
*
* private static ChromeDriverService service;
Expand Down Expand Up @@ -89,12 +91,21 @@
* assertEquals("webdriver - Google Search", driver.getTitle());
* }
* }
*
* </pre></code>
*
*
* Note that unlike ChromeDriver, RemoteWebDriver doesn't directly implement
* role interfaces such as {@link LocationContext} and {@link WebStorage}.
* Therefore, to access that functionality, it needs to be
* {@link org.openqa.selenium.remote.Augmenter augmented} and then cast
* to the appropriate interface.
*
* @see ChromeDriverService#createDefaultService
*/
public class ChromeDriver extends RemoteWebDriver {
public class ChromeDriver extends RemoteWebDriver
implements LocationContext, WebStorage {

private RemoteLocationContext locationContext;
private RemoteWebStorage webStorage;

/**
* Creates a new ChromeDriver using the {@link ChromeDriverService#createDefaultService default}
Expand Down Expand Up @@ -158,6 +169,8 @@ public ChromeDriver(ChromeDriverService service, ChromeOptions options) {
*/
public ChromeDriver(ChromeDriverService service, Capabilities capabilities) {
super(new DriverCommandExecutor(service), capabilities);
locationContext = new RemoteLocationContext(getExecuteMethod());
webStorage = new RemoteWebStorage(getExecuteMethod());
}

@Override
Expand All @@ -167,10 +180,23 @@ public void setFileDetector(FileDetector detector) {
"via RemoteWebDriver");
}

public <X> X getScreenshotAs(OutputType<X> target) {
// Get the screenshot as base64.
String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue();
// ... 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);
}
}
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/chrome/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
java_library(name = "chrome",
srcs = [ "*.java" ],
deps = [
"//java/client/src/org/openqa/selenium:base",
"//java/client/src/org/openqa/selenium:webdriver-api",
"//java/client/src/org/openqa/selenium/net",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/remote/service",
Expand Down

0 comments on commit f93ea6f

Please sign in to comment.