在selenium2.0中使用selenium1.0的API

Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0Selenium RC API那么方便。Selenium2.0提供了使用Selenium RC API的方法:

 

 1 // You may use any WebDriver implementation. Firefox is used hereas an example
 2 WebDriver driver = new FirefoxDriver();
 3  
 4 // A "base url", used by selenium to resolve relativeURLs
 5  String baseUrl ="http://www.google.com";
 6  
 7 // Create the Selenium implementation
 8 Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
 9  
10 // Perform actions with selenium
11 selenium.open("http://www.google.com");
12 selenium.type("name=q", "cheese");
13 selenium.click("name=btnG");
14  
15 // Get the underlying WebDriver implementation back. This willrefer to the
16 // same WebDriver instance as the "driver" variableabove.
17 WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();
18  
19     //Finally, close thebrowser. Call stop on the WebDriverBackedSelenium instance
20     //instead of callingdriver.quit(). Otherwise, the JVM will continue running after
21     //the browser has beenclosed.
22     selenium.stop();

 

分别使用WebDriver APISeleniumRC API写了一个Login的脚本,很明显,后者的操作更加简单明了。

 

 1 //WebDriver API写的Login脚本:
 2     public void login() {
 3         driver.switchTo().defaultContent();
 4         driver.switchTo().frame("mainFrame");
 5  
 6         WebElement eUsername= waitFindElement(By.id("username"));
 7         eUsername.sendKeys(manager@ericsson.com);
 8  
 9         WebElement ePassword= waitFindElement(By.id("password"));
10         ePassword.sendKeys(manager);
11  
12         WebElementeLoginButton = waitFindElement(By.id("loginButton"));
13        eLoginButton.click();
14  
15     }

 

1 //SeleniumRC API写的Login脚本:
2     public void login() {
3         selenium.selectFrame("relative=top");
4         selenium.selectFrame("mainFrame");
5         selenium.type("username","manager@ericsson.com");
6         selenium.type("password","manager");
7         selenium.click("loginButton");
8 }
9      

参考:selenium2.0_中文帮助文档.doc

 

 

posted @ 2014-12-01 17:37  小彩笔的斑斓  阅读(242)  评论(0编辑  收藏  举报