Permalink
Browse files

Adding support for Blink-based Opera to Java binding

  • Loading branch information...
barancev committed Nov 18, 2014
1 parent 2a9b686 commit d69a533af1a1a1dc20cb380beabafd6093a6bbad
@@ -92,6 +92,7 @@ java_library(name = "client-combined",
"//java/client/src/org/openqa/selenium/htmlunit",
"//java/client/src/org/openqa/selenium/firefox",
"//java/client/src/org/openqa/selenium/ie",
"//java/client/src/org/openqa/selenium/opera",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/safari",
"//java/client/src/org/openqa/selenium/support",
@@ -0,0 +1,16 @@
java_library(
name = 'opera',
srcs = glob(['*.java']),
deps = [
'//java/client/src/org/openqa/selenium:webdriver-api',
'//java/client/src/org/openqa/selenium/logging:logging',
'//java/client/src/org/openqa/selenium/net:net',
'//java/client/src/org/openqa/selenium/remote:api',
'//java/client/src/org/openqa/selenium/remote:capabilities',
'//java/client/src/org/openqa/selenium/remote:remote',
'//java/client/src/org/openqa/selenium/remote/service:service',
'//third_party/java/gson:gson',
'//third_party/java/guava-libraries:guava-libraries',
],
visibility = [ 'PUBLIC' ],
)
@@ -0,0 +1,202 @@
/*
Copyright 2012 Selenium committers
Copyright 2012 Software Freedom Conservancy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.openqa.selenium.opera;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
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;
/**
* A {@link WebDriver} implementation that controls a Blink-based Opera browser running on the local
* machine. This class is provided as a convenience for easily testing the Chrome browser. The
* control server which each instance communicates with will live and die with the instance.
*
* <p/>
* To avoid unnecessarily restarting the OperaDriver server with each instance, use a
* {@link RemoteWebDriver} coupled with the desired {@link OperaDriverService}, which is managed
* separately. For example: <code><pre>
*
* import static org.junit.Assert.assertEquals;
*
* import org.junit.*;
* import org.junit.runner.RunWith;
* import org.junit.runners.JUnit4;
* import org.openqa.selenium.opera.OperaDriverService;
* import org.openqa.selenium.remote.DesiredCapabilities;
* import org.openqa.selenium.remote.RemoteWebDriver;
*
* {@literal @RunWith(JUnit4.class)}
* public class OperaTest extends TestCase {
*
* private static OperaDriverService service;
* private WebDriver driver;
*
* {@literal @BeforeClass}
* public static void createAndStartService() {
* service = new OperaDriverService.Builder()
* .usingDriverExecutable(new File("path/to/my/operadriver.exe"))
* .usingAnyFreePort()
* .build();
* service.start();
* }
*
* {@literal @AfterClass}
* public static void createAndStopService() {
* service.stop();
* }
*
* {@literal @Before}
* public void createDriver() {
* driver = new RemoteWebDriver(service.getUrl(),
* DesiredCapabilities.opera());
* }
*
* {@literal @After}
* public void quitDriver() {
* driver.quit();
* }
*
* {@literal @Test}
* public void testGoogleSearch() {
* driver.get("http://www.google.com");
* WebElement searchBox = driver.findElement(By.name("q"));
* searchBox.sendKeys("webdriver");
* searchBox.quit();
* assertEquals("webdriver - Google Search", driver.getTitle());
* }
* }
* </pre></code>
*
* Note that unlike OperaDriver, 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 OperaDriverService#createDefaultService
*/
public class OperaDriver extends RemoteWebDriver
implements LocationContext, WebStorage {
private RemoteLocationContext locationContext;
private RemoteWebStorage webStorage;
/**
* Creates a new OperaDriver using the {@link OperaDriverService#createDefaultService default}
* server configuration.
*
* @see #OperaDriver(OperaDriverService, OperaOptions)
*/
public OperaDriver() {
this(OperaDriverService.createDefaultService(), new OperaOptions());
}
/**
* Creates a new OperaDriver instance. The {@code service} will be started along with the driver,
* and shutdown upon calling {@link #quit()}.
*
* @param service The service to use.
* @see #OperaDriver(OperaDriverService, OperaOptions)
*/
public OperaDriver(OperaDriverService service) {
this(service, new OperaOptions());
}
/**
* Creates a new OperaDriver instance. The {@code capabilities} will be passed to the
* chromedriver service.
*
* @param capabilities The capabilities required from the OperaDriver.
* @see #OperaDriver(OperaDriverService, Capabilities)
*/
public OperaDriver(Capabilities capabilities) {
this(OperaDriverService.createDefaultService(), capabilities);
}
/**
* Creates a new OperaDriver instance with the specified options.
*
* @param options The options to use.
* @see #OperaDriver(OperaDriverService, OperaOptions)
*/
public OperaDriver(OperaOptions options) {
this(OperaDriverService.createDefaultService(), options);
}
/**
* Creates a new OperaDriver instance with the specified options. The {@code service} will be
* started along with the driver, and shutdown upon calling {@link #quit()}.
*
* @param service The service to use.
* @param options The options to use.
*/
public OperaDriver(OperaDriverService service, OperaOptions options) {
this(service, options.toCapabilities());
}
/**
* Creates a new OperaDriver instance. The {@code service} will be started along with the
* driver, and shutdown upon calling {@link #quit()}.
*
* @param service The service to use.
* @param capabilities The capabilities required from the OperaDriver.
*/
public OperaDriver(OperaDriverService service, Capabilities capabilities) {
super(new DriverCommandExecutor(service), capabilities);
locationContext = new RemoteLocationContext(getExecuteMethod());
webStorage = new RemoteWebStorage(getExecuteMethod());
}
@Override
public void setFileDetector(FileDetector detector) {
throw new WebDriverException(
"Setting the file detector only works on remote webdriver instances obtained " +
"via RemoteWebDriver");
}
@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,0 +1,157 @@
/*
Copyright 2011-2012 Selenium committers
Copyright 2011-2012 Software Freedom Conservancy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.openqa.selenium.opera;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.service.DriverService;
import java.io.File;
import java.io.IOException;
/**
* Manages the life and death of a operadriver server.
*/
public class OperaDriverService extends DriverService {
/**
* System property that defines the location of the operadriver executable that will be used by
* the {@link #createDefaultService() default service}.
*/
public static final String OPERA_DRIVER_EXE_PROPERTY = "webdriver.opera.driver";
/**
* System property that defines the location of the log that will be written by
* the {@link #createDefaultService() default service}.
*/
public final static String OPERA_DRIVER_LOG_PROPERTY = "webdriver.opera.logfile";
/**
* Boolean system property that defines whether the OperaDriver executable should be started
* with verbose logging.
*/
public static final String OPERA_DRIVER_VERBOSE_LOG_PROPERTY =
"webdriver.opera.verboseLogging";
/**
* Boolean system property that defines whether the OperaDriver executable should be started
* in silent mode.
*/
public static final String OPERA_DRIVER_SILENT_OUTPUT_PROPERTY =
"webdriver.opera.silentOutput";
/**
*
* @param executable The operadriver executable.
* @param port Which port to start the operadriver on.
* @param args The arguments to the launched server.
* @param environment The environment for the launched server.
* @throws IOException If an I/O error occurs.
*/
public OperaDriverService(File executable, int port, ImmutableList<String> args,
ImmutableMap<String, String> environment) throws IOException {
super(executable, port, args, environment);
}
/**
* Configures and returns a new {@link OperaDriverService} using the default configuration. In
* this configuration, the service will use the operadriver executable identified by the
* {@link #OPERA_DRIVER_EXE_PROPERTY} system property. Each service created by this method will
* be configured to use a free port on the current system.
*
* @return A new OperaDriverService using the default configuration.
*/
public static OperaDriverService createDefaultService() {
return new Builder().usingAnyFreePort().build();
}
/**
* Builder used to configure new {@link OperaDriverService} instances.
*/
public static class Builder extends DriverService.Builder<OperaDriverService> {
private boolean verbose = Boolean.getBoolean(OPERA_DRIVER_VERBOSE_LOG_PROPERTY);
private boolean silent = Boolean.getBoolean(OPERA_DRIVER_SILENT_OUTPUT_PROPERTY);
/**
* Configures the driver server verbosity.
*
* @param verbose true for verbose output, false otherwise.
* @return A self reference.
*/
public Builder withVerbose(boolean verbose) {
this.verbose = verbose;
return this;
}
/**
* Configures the driver server for silent output.
*
* @param silent true for silent output, false otherwise.
* @return A self reference.
*/
public Builder withSilent(boolean silent) {
this.silent = silent;
return this;
}
@Override
protected File findDefaultExecutable() {
return findExecutable("operadriver", OPERA_DRIVER_EXE_PROPERTY,
"https://github.com/operasoftware/operachromiumdriver",
"https://github.com/operasoftware/operachromiumdriver/releases");
}
@Override
protected ImmutableList<String> createArgs() {
if (getLogFile() == null) {
String logFilePath = System.getProperty(OPERA_DRIVER_LOG_PROPERTY);
if (logFilePath != null) {
withLogFile(new File(logFilePath));
}
}
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
if (getLogFile() != null) {
argsBuilder.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
}
if (verbose) {
argsBuilder.add("--verbose");
}
if (silent) {
argsBuilder.add("--silent");
}
return argsBuilder.build();
}
@Override
protected OperaDriverService createDriverService(File exe, int port,
ImmutableList<String> args,
ImmutableMap<String, String> environment) {
try {
return new OperaDriverService(exe, port, args, environment);
} catch (IOException e) {
throw new WebDriverException(e);
}
}
}
}
Oops, something went wrong.

0 comments on commit d69a533

Please sign in to comment.