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


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

Selenium2.0提供了使用SeleniumRCAPI的方法:

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

WebDriverdriver=newFirefoxDriver();

StringbaseUrl="http://www.google.com";

Seleniumselenium=newWebDriverBackedSelenium(driver,baseUrl);

//执行selenium命令

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

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

selenium.click("name=btnG");

WebDriverdriverInstance=((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

selenium.stop();

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

(1)WebDriverAPI写的Login脚本:

publicvoidlogin(){

driver.switchTo().defaultContent();

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

WebElementeUsername=waitFindElement(By.id("username"));

eUsername.sendKeys(manager@ericsson.com);

WebElementePassword=waitFindElement(By.id("password"));

ePassword.sendKeys(manager);

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

eLoginButton.click();

}

(2)SeleniumRCAPI写的Login脚本:

publicvoidlogin(){

selenium.selectFrame("relative=top");

selenium.selectFrame("mainFrame");

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

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

selenium.click("loginButton");

}

优质内容筛选与推荐>>
1、python使用HTMLTestRunner.py生成测试报告
2、Python学习:前端基础之CSS
3、清明听雨
4、ORM
5、[联赛可能考到]图论相关算法——COGS——联赛试题预测


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号