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

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");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值