Selenium2.0 the use of the wedriver API to operate the page, its greatest advantage is that it does not need to install a Selenium server to run, However, the operation of the page is less convenient than the selenium1.0 Selenium RC API . Selenium2.0 provides a way to use the Selenium RC API :
1 //Webdriver implementation. Firefox is used hereas An example2Webdriver Driver =Newfirefoxdriver ();3 4 //A "Base url", used by selenium to resolve Relativeurls5String baseUrl = "http://www.google.com";6 7 //Create the Selenium implementation8Selenium Selenium =Newwebdriverbackedselenium (Driver, baseUrl);9 Ten //Perform actions with Selenium OneSelenium.open ("http://www.google.com"); ASelenium.type ("name=q", "cheese"); -Selenium.click ("name=btng"); - the //Get The underlying webdriver implementation back. This willrefer to the - //same Webdriver instance as the "Driver" Variableabove. -Webdriver driverinstance =((webdriverbackedselenium) selenium). Getunderlyingwebdriver (); - +//Finally, close thebrowser. Call stop on the Webdriverbackedselenium instance - //instead of Callingdriver.quit (). Otherwise, the JVM would continue running after + //The browser has beenclosed. ASelenium.stop ();
Using the Webdriver API and the SELENIUMRC API to write a Login script, it is clear that the latter is more straightforward to operate.
1 //Login script written by Webdriver API:2 Public voidLogin () {3 Driver.switchto (). Defaultcontent ();4Driver.switchto (). FRAME ("MainFrame");5 6Webelement eusername= waitfindelement (by.id ("username"));7 Eusername.sendkeys ([email protected]);8 9Webelement epassword= waitfindelement (by.id ("Password"));Ten Epassword.sendkeys (manager); One AWebelementeloginbutton = Waitfindelement (By.id ("Loginbutton")); - Eloginbutton.click (); - the}
1 //Login script written by SELENIUMRC API:2 Public voidLogin () {3Selenium.selectframe ("Relative=top");4Selenium.selectframe ("MainFrame");5Selenium.type ("username", "[email protected]");6Selenium.type ("Password", "manager");7Selenium.click ("Loginbutton");8 }9
Reference: selenium2.0_ Chinese help documentation. doc
Using selenium1.0 APIs in selenium2.0