Migrate Existing Selenium RC Scripts to WebDriver
From last few years, we have spent time and efforts in developing automated scripts using selenium RC and now it is not easy to convert existing RC scripts into web driver because high amount of cost required in this process.
Good thing is that WebDriver provides backward compatibility. With the help of “WebDriverBackedSelenium”, one can use the underlying WebDriver technology with Selenium-RC API. Mean to say that one can easily migrate RC scripts to web driver with few changes in its scripts.
Eg:- Below is the Selenium RC script code
public class SeleniumRCScriptSample { public static void main(String args[]) { Selenium selenium = new DefaultSelenium(“localhost”, 4444, "*firefox", "http://www.gmail.com"); //Start selenium server selenium.start(); //Launch firefox Browser selenium.open(“/”); //Maximize browser selenium.windowMaximize(); Thread.sleep(5000); selenium.type(“id=username”, “contact.automateapps@gmail.com”); selenium.type(“id=password”, “password”); selenium.click(“name=Login”); //Stop server selenium.stop(); } }
Above selenium RC code is migrated to Webdriver code
public class MigratedScriptFromRCToWebDriver { public static void main(String args[]) { // Initialize Driver WebDriver driver = new FirefoxDriver(); String baseUrl = “http://www.google.com”; // Create an instance of WebDriverBackedSelenium Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl); // Selenium RC script selenium.type(“id=username”, “contact.automateapps@gmail.com”); selenium.type(“id=password”, “password”); selenium.click(“name=Login”); //Closes the browser selenium.stop(); } }
NOTE : – One can easily migrate existing selenium RC scripts to web Driver just by writing below 2 lines.Mean to say there is no need to change each and every line of your selenium RC script. Just create an object of Firefox/chrome/IE driver and pass it as an argument in “WebDriverBackedSelenium” along with application URL under test.
WebDriver driver = new FirefoxDriver(); //It could be ChromeDriver, IEDriver etc as per your script requirement. Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
WebDriverBackedSelenium also allows you to use both RC and web driver API in same code.
Eg :- public static void main(String args[]) { // Initialize Driver. We are taking an example of firefox WebDriver driver = new FirefoxDriver(); String baseUrl = “http://www.gmail.com”; // Create an instance of WebDriverBackedSelenium Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl); // Selenium RC script selenium.type(“id=username”, “contact.automateapps@gmail.com”); selenium.type(“id=password”, “password”); selenium.click(“name=Login”); // In order to write Web driver code in a same script. You have to take webDriver implementation back. driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver(); //Now write web driver code driver.findElement(By.name("Login")).click(); //Closes the browser driver.quit(); }