Selenium2.0之WebDriver学习总结(1)-1

上一篇 / 下一篇  2012-11-27 11:51:47 / 个人分类:Selenium

(一)   Selenium2.0 VS Selenium1.0

PQA/G"P2CZ#w3Cg0

我们已经有了Selenium1.0为什么还需要Selenium2.0呢?

Y_L+hQ0

Selenium1.0不能处理一下事件:

gEuZ4K2[{0

1)         本机键盘和鼠标事件51Testing软件测试网J rr"Y|iK1g

2)         同源策略XSS/HTTPS

Z x*R/w1E0

3)         弹出框,对话框(基本身份认证,自签名的证书和文件上传/下载)51Testing软件测试网3A2P$Af6H/W6[`W h

Selenium2.0有简洁的APIWebDriverWebElement对象,更好的抽象。且支持多中操作系统,多语言,多浏览器。

~*bGa!n]0

同时Selenium2.0进行了架构的调整和升级:

!h qJ iY`*D0

Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)51Testing软件测试网x0s| U(t^D#Z

Selenium1.0可以使用任何编程语言,但是有个先决条件就是必须支持HTTP库。Selenium1.0起初就是一个Javascript库,到后面引入了SeleniumRCSeleniumRC作为一个代理服务器并且发送操作命令给Selenium Corejavascript代码,且为SeleniumRC的一部分)。SeleniumRC测试程序接收指令并翻译,返回测试结果给测试程序。Selenium Coreclient API打开浏览器后就注入到浏览器中,然后Selenium Core接收测试程序的指令,解释成selenese命令,在浏览器执行。51Testing软件测试网 E7Lc m?0Y

Selenium1.0作为第一个基于javascript开源的web测试框架,迅速的得到了其他浏览器的支持。但是和任何其他大项目一样,Selenium1.0也不是完美的。正因为他完全是使用javascript写的,这也给他带来了致命的缺陷。为了防止恶意的javascript,所有浏览器都加强了对javascript的安全策略。这样势必导致了在一些特定场景无法使用Selenium1.0。作为一个大项目,随着时间的日积月累,Selenium1.0API变得越来也大,也越来也复杂,导致了都不知道更好的使用它改善它。51Testing软件测试网h bUQ-AG6C

WebDriver项目是由Simon Stewart 提出的,它是一个轻便简洁的自动化测试框架。WebDriver通过尝试不同的方法去解决Selenium1.0所面临的问题。不单单是使用javascriptWebDriver会使用任何一种更合适的机制来操作浏览器。IE通过使用C++FF通过使用javascript. in a XPCOM component51Testing软件测试网}:\ Ce*r8oq^

通过更灵活的机制去操控浏览器,那我们就能很好的绕过浏览器javascript的安全限制。当这些技术还不够用时,我们可以调用系统设备操作,尤其是当你需要一些键盘和鼠标操作时,通过这些技术,我们可以更好的模拟用户的真实浏览器操作。51Testing软件测试网Hn3e&p#@;[c

当这两个框架被合并后,一个框架的缺陷被另一个框架所弥补。WebDriver对浏览器的支持需要对应框架开发工程师做对应的开发;同样Selenium必须操作真实浏览器,但是WebDriver可以HTML unit Driver来模拟浏览器,在内存中执行用例,更加的轻便。Selenium1.0解决了自动化测试中的一些常见问题,WebDriver更好的解决了沙箱限制。WebDriver不支持并行,但是Selenium Grid解决了这个问题。

lw-tV/lQ P R+yq0

 51Testing软件测试网6h9~9ie _,b@

(二)   WebDriver

NoC%K/v'_n0

a)         WebDirver小实例

%P*B*P5J`(k8f$`0

WebDriver是一个用来进行复杂重复的web自动化测试的工具。意在提供一种比Selenium1.0更简单易学,有利于维护的API。它没有和任何测试框架进行绑定,所以他可以很好的在单元测试main方法中调用。一旦创建好一个Selenium工程,你马上会发现WebDriver和其他类库一样:它是完全独立的,你可以直接使用而不需要考虑其他配置,这个Selenium RC是截然相反的。51Testing软件测试网$e7X4N Xsbs N

下面我们就开始进入WebDriver的实际应用,首先大家下载好eclipse,还有从selenium官方网站下载selenium2.0的jar包和chromedriver的驱动文件。构建整个工程如下模式:51Testing软件测试网4t9r})bm5t}

 现在我们开始写代码:

packagedemo;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
publicclassSelenium2Example{
publicstaticvoidmain(String[]args){
//创建一个WebDriver实例
WebDriverdriver=newFirefoxDriver();
// 访问google
driver.get("http://www.google.com");
// 另一种访问方法
// driver.navigate().to("http://www.google.com");
// 找到文本框
WebElementelement=driver.findElement(By.name("q"));
// 输入搜索关键字
element.sendKeys("Selenium");
//提交表单 WebDriver会自动从表单中查找提交按钮并提交
element.submit();
// 检查页面title
System.out.println("Page title is: "+driver.getTitle());
// google查询结果是通过javascript动态呈现的.
// 设置页面等待10秒超时
(newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
publicBooleanapply(WebDriverd){
returnd.getTitle().toLowerCase().startsWith("Selenium");
}
});
// 显示查询结果title
System.out.println("Page title is: "+driver.getTitle());
//关闭浏览器
driver.quit();
}
}

'}4n1r3L5q5bp4F D)Y:{"v0

b)         介绍WebDriverDrivers51Testing软件测试网4c}q?$_ te ]2mA3F

WebDriver是测试中必须要写的关键接口的名字,但是他有多种实现,包括下面:

} |&y+@4az7m~0

HtmlUnit Driver这是目前最快,最轻巧的实施的WebDriver。顾名思义,这是基于HtmlUnit HtmlUnitJava一个WebBrowser没有图形用户界面的实现。

n)`Rj-w1Q9`0

用法

-S mGBmI!tGT S8R}1?0

WebDriver driver = new HtmlUnitDiver();

5z {5qiNH g0

例子:51Testing软件测试网|pc1cX^

packagedemo;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.htmlunit.HtmlUnitDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
publicclassDemoUseHtmlUnit{
publicstaticvoidmain(String[]args){
//创建一个WebDriver实例
WebDriverdriver=newHtmlUnitDriver();
// 访问google
driver.get("http://www.google.com");
// 找到文本框
WebElementelement=driver.findElement(By.name("q"));
// 输入搜索关键字
element.sendKeys("Selenium");
//提交表单 WebDriver会自动从表单中查找提交按钮并提交
element.submit();
// 检查页面title
System.out.println("Page title is: "+driver.getTitle());
// google查询结果是通过javascript动态呈现的.
// 设置页面等待10秒超时
(newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
publicBooleanapply(WebDriverd){
returnd.getTitle().toLowerCase().startsWith("selenium");
}
});
// 显示查询结果title
System.out.println("Page title is: "+driver.getTitle());
//关闭浏览器
driver.quit();
}
}

TAG:

 

评分:0

我来说两句

Open Toolbar