分享

WebDriver拾级而上·之十一 在selenium2.0中使用selenium1.0的AP

 Icedcoffeev59g 2017-07-04

 


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

Selenium2.0提供了使用Selenium RC API的方法:


    /我用火狐浏览器作为例子

    WebDriver driver new FirefoxDriver(); 

    String baseUrl ="http://www.google.com"; 

    Selenium selenium new WebDriverBackedSelenium(driver, baseUrl);

 

    // 执行selenium命令

    selenium.open("http://www.google.com");

    selenium.type("name=q", "cheese");

    selenium.click("name=btnG");

 

    WebDriver driverInstance ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

    selenium.stop();

 

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

(1)WebDriver API写的Login脚本:

    public void login() {

        driver.switchTo().defaultContent();

        driver.switchTo().frame("mainFrame");

 

        WebElement eUsername= waitFindElement(By.id("username"));

        eUsername.sendKeys(manager@ericsson.com);

 

        WebElement ePassword= waitFindElement(By.id("password"));

        ePassword.sendKeys(manager);

 

        WebElementeLoginButton waitFindElement(By.id("loginButton"));

       eLoginButton.click();

    }

    

(2)SeleniumRC API写的Login脚本:

    public void login() {

        selenium.selectFrame("relative=top");

        selenium.selectFrame("mainFrame");

        selenium.type("username","manager@ericsson.com");

        selenium.type("password","manager");

        selenium.click("loginButton");

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多