|
| 1 | +--- |
| 2 | +title: "Backing Selenium with WebDriver" |
| 3 | +linkTitle: "Emulations" |
| 4 | +weight: 3 |
| 5 | +description: > |
| 6 | + The Java and .NET versions of Selenium 2 provided implementations of the original Selenium API |
| 7 | +--- |
| 8 | +(Previously located: https://github.com/SeleniumHQ/selenium/wiki/Selenium-Emulation) |
| 9 | + |
| 10 | +## Backing Selenium with WebDriver |
| 11 | +The Java and .NET versions of WebDriver provide implementations of the existing Selenium API. In Java, it is used like so: |
| 12 | + |
| 13 | +``` |
| 14 | +// You may use any WebDriver implementation. Firefox is used here as an example |
| 15 | +WebDriver driver = new FirefoxDriver(); |
| 16 | +
|
| 17 | +// A "base url", used by selenium to resolve relative URLs |
| 18 | +String baseUrl = "http://www.google.com"; |
| 19 | +
|
| 20 | +// Create the Selenium implementation |
| 21 | +Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl); |
| 22 | +
|
| 23 | +// Perform actions with selenium |
| 24 | +selenium.open("http://www.google.com"); |
| 25 | +selenium.type("name=q", "cheese"); |
| 26 | +selenium.click("name=btnG"); |
| 27 | +
|
| 28 | +// And get the underlying WebDriver implementation back. This will refer to the |
| 29 | +// same WebDriver instance as the "driver" variable above. |
| 30 | +WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver(); |
| 31 | +``` |
| 32 | + |
| 33 | +## Pros |
| 34 | + |
| 35 | +* Allows for WebDriver and Selenium to live side-by-side. |
| 36 | +* Provides a simple mechanism for a managed migration from the existing Selenium API to WebDriver's. |
| 37 | +* Does not require the standalone Selenium RC server to be run |
| 38 | + |
| 39 | +## Cons |
| 40 | + |
| 41 | +* Does not implement every method |
| 42 | + * But we'd love feedback! |
| 43 | +* Does also emulate Selenium Core |
| 44 | + * So more advanced Selenium usage (that is, using "browserbot" or other built-in Javascript methods from Selenium Core) may need work |
| 45 | +* Some methods may be slower due to underlying implementation differences |
| 46 | +* Does not support Selenium's "user extensions" (_i.e._, user-extensions.js) |
| 47 | + |
| 48 | +### Notes |
| 49 | +After creating a `WebDriverBackedSelenium` instance with a given Driver, one does not have to call `start()` - as the creation of the Driver already started the session. At the end of the test, `stop()` should be called **instead** of the Driver's `quit()` method. |
| 50 | + |
| 51 | +This is more similar to WebDriver's behaviour - as creating a Driver instance starts a session, yet it has to be terminated explicitly with a call to `quit()`. |
| 52 | + |
| 53 | +## Backing Selenium with RemoteWebDriver |
| 54 | +Starting with release 2.19, `WebDriverBackedSelenium` can be used from any language supported by WebDriver and Selenium. |
| 55 | + |
| 56 | +For example, in Python: |
| 57 | +``` |
| 58 | +driver = RemoteWebDriver(desired_capabilities = DesiredCapabilities.FIREFOX) |
| 59 | +selenium = DefaultSelenium('localhost', '4444', '*webdriver', 'http://www.google.com') |
| 60 | +selenium.start(driver = driver) |
| 61 | +``` |
| 62 | + |
| 63 | +Provided you keep a reference to the original WebDriver and Selenium objects you created, you can use even the two APIs interchangeably. The magic is the "`*webdriver`" browser name passed to the Selenium instance, and that you pass the WebDriver instance when calling `start()`. |
| 64 | + |
| 65 | +In languages where DefaultSelenium doesn't have `start(driver)`, you can connect the WebDriver and Selenium objects together yourself, by supplying the WebDriver session ID to the Selenium object. |
| 66 | + |
| 67 | +For example, in C#: |
| 68 | +``` |
| 69 | +
|
| 70 | +RemoteWebDriver driver = new RemoteWebDriver(DesiredCapabilities.Firefox()); |
| 71 | +string sessionId = (string) driver.Capabilities.GetCapability("webdriver.remote.sessionid"); |
| 72 | +DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*webdriver", "http://www.google.com"); |
| 73 | +selenium.Start("webdriver.remote.sessionid=" + sessionId); |
| 74 | +``` |
| 75 | + |
| 76 | +## Backing WebDriver with Selenium |
| 77 | + |
| 78 | +WebDriver doesn't support as many browsers as Selenium does, so in order to provide that support while still using the webdriver API, you can make use of the `SeleneseCommandExecutor` It is done like this: |
| 79 | + |
| 80 | +``` |
| 81 | +Capabilities capabilities = new DesiredCapabilities() |
| 82 | +capabilities.setBrowserName("safari"); |
| 83 | +CommandExecutor executor = new SeleneseCommandExecutor("http:localhost:4444/", "http://www.google.com/", capabilities); |
| 84 | +WebDriver driver = new RemoteWebDriver(executor, capabilities); |
| 85 | +``` |
| 86 | + |
| 87 | +There are currently some major limitations with this approach, notably that `findElements` doesn't work as expected. Also, because we're using Selenium Core for the heavy lifting of driving the browser, you are limited by the Javascript sandbox. |
0 commit comments