|
| 1 | +/* |
| 2 | +Copyright 2012 Selenium committers |
| 3 | +Copyright 2012 Software Freedom Conservancy |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | + |
| 19 | +package org.openqa.selenium.opera; |
| 20 | + |
| 21 | +import org.openqa.selenium.Capabilities; |
| 22 | +import org.openqa.selenium.WebDriver; |
| 23 | +import org.openqa.selenium.WebDriverException; |
| 24 | +import org.openqa.selenium.html5.LocalStorage; |
| 25 | +import org.openqa.selenium.html5.Location; |
| 26 | +import org.openqa.selenium.html5.LocationContext; |
| 27 | +import org.openqa.selenium.html5.SessionStorage; |
| 28 | +import org.openqa.selenium.html5.WebStorage; |
| 29 | +import org.openqa.selenium.remote.FileDetector; |
| 30 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 31 | +import org.openqa.selenium.remote.html5.RemoteLocationContext; |
| 32 | +import org.openqa.selenium.remote.html5.RemoteWebStorage; |
| 33 | +import org.openqa.selenium.remote.service.DriverCommandExecutor; |
| 34 | + |
| 35 | +/** |
| 36 | + * A {@link WebDriver} implementation that controls a Blink-based Opera browser running on the local |
| 37 | + * machine. This class is provided as a convenience for easily testing the Chrome browser. The |
| 38 | + * control server which each instance communicates with will live and die with the instance. |
| 39 | + * |
| 40 | + * <p/> |
| 41 | + * To avoid unnecessarily restarting the OperaDriver server with each instance, use a |
| 42 | + * {@link RemoteWebDriver} coupled with the desired {@link OperaDriverService}, which is managed |
| 43 | + * separately. For example: <code><pre> |
| 44 | + * |
| 45 | + * import static org.junit.Assert.assertEquals; |
| 46 | + * |
| 47 | + * import org.junit.*; |
| 48 | + * import org.junit.runner.RunWith; |
| 49 | + * import org.junit.runners.JUnit4; |
| 50 | + * import org.openqa.selenium.opera.OperaDriverService; |
| 51 | + * import org.openqa.selenium.remote.DesiredCapabilities; |
| 52 | + * import org.openqa.selenium.remote.RemoteWebDriver; |
| 53 | + * |
| 54 | + * {@literal @RunWith(JUnit4.class)} |
| 55 | + * public class OperaTest extends TestCase { |
| 56 | + * |
| 57 | + * private static OperaDriverService service; |
| 58 | + * private WebDriver driver; |
| 59 | + * |
| 60 | + * {@literal @BeforeClass} |
| 61 | + * public static void createAndStartService() { |
| 62 | + * service = new OperaDriverService.Builder() |
| 63 | + * .usingDriverExecutable(new File("path/to/my/operadriver.exe")) |
| 64 | + * .usingAnyFreePort() |
| 65 | + * .build(); |
| 66 | + * service.start(); |
| 67 | + * } |
| 68 | + * |
| 69 | + * {@literal @AfterClass} |
| 70 | + * public static void createAndStopService() { |
| 71 | + * service.stop(); |
| 72 | + * } |
| 73 | + * |
| 74 | + * {@literal @Before} |
| 75 | + * public void createDriver() { |
| 76 | + * driver = new RemoteWebDriver(service.getUrl(), |
| 77 | + * DesiredCapabilities.opera()); |
| 78 | + * } |
| 79 | + * |
| 80 | + * {@literal @After} |
| 81 | + * public void quitDriver() { |
| 82 | + * driver.quit(); |
| 83 | + * } |
| 84 | + * |
| 85 | + * {@literal @Test} |
| 86 | + * public void testGoogleSearch() { |
| 87 | + * driver.get("http://www.google.com"); |
| 88 | + * WebElement searchBox = driver.findElement(By.name("q")); |
| 89 | + * searchBox.sendKeys("webdriver"); |
| 90 | + * searchBox.quit(); |
| 91 | + * assertEquals("webdriver - Google Search", driver.getTitle()); |
| 92 | + * } |
| 93 | + * } |
| 94 | + * </pre></code> |
| 95 | + * |
| 96 | + * Note that unlike OperaDriver, RemoteWebDriver doesn't directly implement |
| 97 | + * role interfaces such as {@link LocationContext} and {@link WebStorage}. |
| 98 | + * Therefore, to access that functionality, it needs to be |
| 99 | + * {@link org.openqa.selenium.remote.Augmenter augmented} and then cast |
| 100 | + * to the appropriate interface. |
| 101 | + * |
| 102 | + * @see OperaDriverService#createDefaultService |
| 103 | + */ |
| 104 | +public class OperaDriver extends RemoteWebDriver |
| 105 | + implements LocationContext, WebStorage { |
| 106 | + |
| 107 | + private RemoteLocationContext locationContext; |
| 108 | + private RemoteWebStorage webStorage; |
| 109 | + |
| 110 | + /** |
| 111 | + * Creates a new OperaDriver using the {@link OperaDriverService#createDefaultService default} |
| 112 | + * server configuration. |
| 113 | + * |
| 114 | + * @see #OperaDriver(OperaDriverService, OperaOptions) |
| 115 | + */ |
| 116 | + public OperaDriver() { |
| 117 | + this(OperaDriverService.createDefaultService(), new OperaOptions()); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a new OperaDriver instance. The {@code service} will be started along with the driver, |
| 122 | + * and shutdown upon calling {@link #quit()}. |
| 123 | + * |
| 124 | + * @param service The service to use. |
| 125 | + * @see #OperaDriver(OperaDriverService, OperaOptions) |
| 126 | + */ |
| 127 | + public OperaDriver(OperaDriverService service) { |
| 128 | + this(service, new OperaOptions()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Creates a new OperaDriver instance. The {@code capabilities} will be passed to the |
| 133 | + * chromedriver service. |
| 134 | + * |
| 135 | + * @param capabilities The capabilities required from the OperaDriver. |
| 136 | + * @see #OperaDriver(OperaDriverService, Capabilities) |
| 137 | + */ |
| 138 | + public OperaDriver(Capabilities capabilities) { |
| 139 | + this(OperaDriverService.createDefaultService(), capabilities); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates a new OperaDriver instance with the specified options. |
| 144 | + * |
| 145 | + * @param options The options to use. |
| 146 | + * @see #OperaDriver(OperaDriverService, OperaOptions) |
| 147 | + */ |
| 148 | + public OperaDriver(OperaOptions options) { |
| 149 | + this(OperaDriverService.createDefaultService(), options); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Creates a new OperaDriver instance with the specified options. The {@code service} will be |
| 154 | + * started along with the driver, and shutdown upon calling {@link #quit()}. |
| 155 | + * |
| 156 | + * @param service The service to use. |
| 157 | + * @param options The options to use. |
| 158 | + */ |
| 159 | + public OperaDriver(OperaDriverService service, OperaOptions options) { |
| 160 | + this(service, options.toCapabilities()); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Creates a new OperaDriver instance. The {@code service} will be started along with the |
| 165 | + * driver, and shutdown upon calling {@link #quit()}. |
| 166 | + * |
| 167 | + * @param service The service to use. |
| 168 | + * @param capabilities The capabilities required from the OperaDriver. |
| 169 | + */ |
| 170 | + public OperaDriver(OperaDriverService service, Capabilities capabilities) { |
| 171 | + super(new DriverCommandExecutor(service), capabilities); |
| 172 | + locationContext = new RemoteLocationContext(getExecuteMethod()); |
| 173 | + webStorage = new RemoteWebStorage(getExecuteMethod()); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void setFileDetector(FileDetector detector) { |
| 178 | + throw new WebDriverException( |
| 179 | + "Setting the file detector only works on remote webdriver instances obtained " + |
| 180 | + "via RemoteWebDriver"); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public LocalStorage getLocalStorage() { |
| 185 | + return webStorage.getLocalStorage(); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public SessionStorage getSessionStorage() { |
| 190 | + return webStorage.getSessionStorage(); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public Location location() { |
| 195 | + return locationContext.location(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void setLocation(Location location) { |
| 200 | + locationContext.setLocation(location); |
| 201 | + } |
| 202 | +} |
0 commit comments