selenium和webdriver webdriver api 的时候需要注意哪些问题

君,已阅读到文档的结尾了呢~~
Selenium WebDriver基本API..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
WebDriver基本API使用(基于Java和C#)
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口Selenium VS Webdriver - 为程序员服务
Selenium VS Webdriver
追踪溯源,WebDriver 和 Selenium 本是两个自力的项目,完成机制也是不合的。那 Selenium 团队为什么会在 Selenium 2 中将两者兼并,这现实有什么意图呢?WebDriver 比 Selenium 又有什么优势呢?我们该若何选择行使 Selenium 照样 WebDriver 呢?别着急,您将在本文中找到谜底,并将意见一些 WebDriver 的根抵常识和行使方法。
为随意表述,在本文中,我们称 Selenium 2 为 WebDirver,Selenium 为 Selenium 1.x(因为 Selenium1.x 时常日指的是 Selenium RC,所以 Selenium 也指 Selenium RC)。
WebDriver 是… …?
Selenium 2,别名 WebDriver,它的次要新功用是集成了 Selenium 1.0 和 WebDriver(WebDriver 曾是 Selenium 的竞争对手)。也就是说 Selenium 2 是 Selenium 和 WebDriver 两个项目的兼并,即 Selenium 2 兼容 Selenium,它既支持 Selenium API 也支持 WebDriver API。
那 Selenium 团队为什么会将两个项目兼并呢?我们常日以为个中部份启事是 WebDriver 处置了 Selenium 存在的瑕玷(好比,可以绕过 JS 沙箱),部份启事是 Selenium 处置了 WebDriver 存在的成就(好比,支持更普遍的阅读器和编程措辞),岂论真实的启事是什么两个项目的兼并为用户供应了一个优秀的自动化测试框架。
现在让我们看看两个对象有什么具体的不合。在最先之前,我们首先看一下用 Selenium 和用 Webdriver 构建出来的测试工程是什么样的,后文会在这个根蒂根抵上论述 Webdriver 和 Selenium 的异同。
声名:因为现在 WebDriver 还在改良和优化进程傍边,所以我们以下的举例和声名都是基于版本 selenium-2.28.0 的根蒂根抵上。
构建一个 Selenium 测试工程
Selenium API 则支持更多的编程措辞,这里我们照样以 Java 为例。
图 1. Selenium 测试工程
清单 1. 行使 Selenium API 的剧本 – 登录 SmartCloud iNotes
import com.thoughtworks.selenium.DefaultS
import com.thoughtworks.selenium.S
public class SeleniumDemo {
public static void main(String[] args) throws InterruptedException {
// 树立一个 Selenium 实例
Selenium selenium = new DefaultSelenium("localhost", 4444, /
"*firefox", "https://apps./");
// 启动 selenium session
selenium.start();
// 掀开测试网页
selenium.open("/");
// 输出用户名,密码
selenium.type("//input[@id='username']", /
"autouser01@e3yunmail.");
selenium.type("//input[@id='password']", "test");
selenium.click("//input[@id='submit_form']");
// 守候直到页面泛起 Mail 链接
int count = 60;
while(count & 0){
if(selenium.isElementPresent("//a[contains(text(),'Mail')]")){
Thread.sleep(1000);
selenium.click("//a[contains(text(),'Log Out')]");
// 测试竣事后,终止 selenium session
selenium.stop();
构建一个 WebDriver 测试工程
WebDriver API 可以经由进程 Python、Ruby、Java 和 C#接见,支持垦荒人员行使他们公正的编程措辞来树立测试。这里我们以 Java 为例。首先需求准备好本人的 Eclipse 情形,并在 selenium 的官方网站下载 Selenium 2 的 Jar 包。
图 2. WebDriver 测试工程
清单 2. 行使 WebDriver API 的剧本 – 登录 SmartCoud iNotes
import org.openqa.selenium.By;
import org.openqa.selenium.WebD
import org.openqa.selenium.WebE
import org.openqa.selenium.firefox.FirefoxD
import org.openqa.selenium.support.ui.ExpectedC
import org.openqa.selenium.support.ui.WebDriverW
public class WebDriverDemo {
public static void main(String[] args) {
//树立一个 firefox driver 实例
WebDriver driver = new FirefoxDriver();
//掀开测试网址
driver.get("https://apps./");
//界说用户名和密码文本框
WebElement username=driver.findElement(By.id("username"));
WebElement password=driver.findElement(By.id("password"));
//输出用户名和密码
username.sendKeys("autouser01@e3yunmail.");
password.sendKeys("test");
//点击 login 登录
WebElement login=driver.findElement(By.id("submit_form"));
login.click();
//设置页面守候直到泛起 Mail 链接
(new WebDriverWait(driver, 500)).until(new ExpectedCondition&WebElement&(){
public WebElement apply(WebDriver dr) {
return dr.findElement(By.linkText("Mail"));
WebElement logout=driver.findElement(By.linkText("Log Out"));
logout.click();
//关闭阅读器
driver.quit();
Selenium vs WebDriver
从上述用 Selenium 和 WebDriver 构建的两个测试工程来看,WebDriver 工程在构建之后不需求其他的设置配备铺排我们即可以直接行使,这一点和 Selenium 是判然不合的。因为 Selenium 还需求安装并启动 Selenium Server 才华运转测试轨范。
其他,我们可以看出 WebDriver 是基于面向对象的 API,它更多的是从用户角度动身。反之 Selenium 供应的是基于字典的 API,用户可以很随意的看到所以支持的方法。无须置疑的是,WebDriver 供应的 API 加倍精练,对用户加倍的敌对。但从其他一个角度来看,就是用户不能很直不雅观不雅观的看到 WebDriver 供应了哪些 API,可以需求经由进程官网供应的 JavaDoc(起原:http://selenium.谷歌/svn/trunk/docs/api/java/index.html)的协助来找寻一些方法。
上面我们看下 WebDriver 支持的阅读器类型,和一些根抵操作的行使方法。
支持的阅读器
Selenium 是由一堆 JavaScript
完成的,所以只需支持 JavaScript
的阅读器 Selenium 都可以做到很好的支持,理论下去说它比 WebDriver 能支持更多的阅读器而且不需求做额定的垦荒。然则因为每一个阅读器对 JavaScript
的实行有着不合轨范的平安限制,以避免用户被歹意进击,所以 Selenium 存在一些不能处置责罚的情形,例如本机的鼠标和键盘事宜,弹出框等等。WebDriver 则经由进程不合的方法来处置 Selenium 面临的一些成就,不单单行使 JavaScript ,WebDriver 会行使任何一种适合的机制来操作阅读器。WebDriver 供应了、、、 、SafariDriver、HtmlUnitDriver,和 RemoteWebDriver 来支持不合的阅读器。其他为了支持挪动配备它供应了
和 。个中很特殊的是 HtmlUnitDriver,它的无界面完成不会理想掀开阅读器,运转速度很快,所以用户可以行使它来做一些单元测试。对用 FireFox 或 Internet Explorer 等阅读器来做测试的自动化测试用例,运转速度常日很慢,HtmlUnit Driver 无疑是可以很好地处置这个成就。
那末既然 Selenium 和 WebDriver 支持的阅读器差不多,我们现实理被选择 Selenium 照样 WebDriver 呢?虽然现在 WebDriver 还在处在垦荒和测试傍边,有些功用还存在一些瑕玷,但跟着 Webdriver 的赓续完善,它交流 Selenium 终将是除夜势所趋。所以笔者照样建议选择行使 WebDriver,假定真实 WebDriver 还不能支持你的测试责任,用 Selenium 来做一个过渡也是可以的。虽然选用不合的测试对象可以对代码珍重有一定的应战,究竟 Selenium 和 WebDriver 的 API 是不合的,幸而 WebDriver 还供应了其他一种方法。例如在笔者现在的项目中要支持 Safari,然则行使 SafariDriver 测试时异常的不波动甚至测试不能顺遂的中止。所以行使了 SeleneseCommandExecutor,这类方法也可以让 WebDriver 支持更多的阅读器并照样行使 WebDriver API,这样也会下降珍重代码的责任量。需求留意的是,这类方法需求安装并启动 Selenium Server。
清单 3. 行使 SelenseCommandExecutor 使 WebDriver 支持更多的阅读器
Capabilities capabilities = new DesiredCapabilities()
capabilities.setBrowserName( "safari" );
CommandExecutor executor = new SeleneseCommandExecutor( "http:localhost:4444/",/
"http://WebDriver driver = new RemoteWebDriver(executor, capabilities);
定位页面元素
Selenium 对元素的处置责罚是基于 Action,Object(操作,对象)的体式格式,行使 id、name,或是 XPath 来定位页面元素。当行使的是 XPath 时在某种水平上对 Internet Explorer 的测试速度有很除夜的影响,因为 IE 阅读器没有内置对 XPath 的支持,所以在 IE 上测试时会特殊慢,虽然现在已有装备处置这个成就了,虽然比不上 Firefox 的测试速度但也是有了相当除夜的提高。
WebDriver 则行使 findElement 方法来查找到页面元素,它定位页面元素可以经由进程 id、name、xpath、className、link text、CSS 等等查找。
表 1. WebDriver 查找页面元素
查找体式格式
举例 (Java)
以元素的 id 属性来定位页面元素
By.id(“username”)
以元素的 name 属性来定位页面元素
By.name(“username”)
以 XPath 来定位页面元素
By.xpath(“//*[@id="username"])
By.className
以元素的 class 属性来定位页面元素
By.className(“even-table-row”)
By.cssSelector
基于 CSS 选择器引擎定位页面元素
By.cssSelector(“#username”)
By.LinkText
By.partialLinkText
查找包括链接字串的页面元素
By.linkText(“Click Me!”)
By.partialLinkText(“ck M”)
By.TagName
以元素的标签称号查找页面元素
By.tagName(“td”)
有人研讨过,By CSS 体式格式是最快的查找体式格式,所以在做测试时,我们可以尽量行使 By.cssSelector 的体式格式。(起原:)
在 Web UI 的自动化测试中,一种罕有的不波动是页面上的元素加载时辰不固定,好比在 Ajax,或 JS 延迟加载等情形下,页面元素泛起的时辰短的几毫秒,长的几秒钟。这个时辰在读取页面元素就会一些费事。等得时辰短了话找不到页面元素,测试 fail;但等得时辰太长,又会增加测试的时辰组成效率低下。
在 Selenium 中,用户只能预算一个时辰行使 Selenium.waitForPageToLoad()或 Thread.sleep()的体式格式来守候页面加载时辰,要不就是行使本人写的 Wait 方法,好比:
清单 4. 行使 Selenium API 的剧本 – 守候页面元素加载
public static void WaitForElementPresent(String elementName)
int maxTry = 60;
if(selenium.isElementPresent(elementName))
for (int second = 0; second & maxT second++)
if (selenium.isElementPresent(elementName))
Thread.sleep(1000); //守候一秒钟
但在 Webdriver 中,它供应了 Explicit and Implicit Waits 方法,使得守候的方法加倍敏锐行使,不才面的例子就是一个封装好的守候页面加载的方法,在 20 秒钟内守候页面元素泛起,假定跨越 20 秒就会抛出超时异常:
清单 5. 行使 WebDriver API 的剧本 – 守候页面元素加载
public boolean waitForElementDisplayed(WebElement el, boolean shouldBeDisplayed) {
boolean displayed =
if (shouldBeDisplayed) {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(visibilityOfElementLocated(el));
displayed =
} catch (Exception e) {
displayed =
displayed = el.isDisplayed();
} catch (NoSuchElementException e) {
displayed =
更多关于 Explicit and Implicit Waits 的内容,请参考 http://seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits
并行测试支持
在 Web UI 自动化测试中,实行效率长短常主要的,特殊是在测试剧本比拟多的情形下。所以我们有需求支持并行化测试,并行化测试需求两个前提:
在运转测试用例端,需求支持并走运转。例如,写一个多线程来支持同时运转多个测试用例
测试用例之间,需求减除在并走运转时的依托关系。例如,行使不合的用户,以免发生发火测试冲突
在 Selenium Server 端,要可以同时掀开多个阅读器窗口,并使得各个阅读器窗口之间的测试不受烦扰,各自运转
对前提 1,我们可以本人写一个多线程来支持并走运转,支持并行的自动化测试,或行使 JUnit 或 TestNG;
对前提 2,我们在编测试用户剧本的时辰,需求斟酌到在在并行测试的时辰可以存在的测试冲突,好比写日志,测试用户等等,可以参考文章(用 STAF+Selenium 完成并行的测试化框架,起原:);
而对前提 3,我们需求借助 Selenium Grid。行使 Selenium Grid,我们可以很随意的同时在多台测试机械上和异构情形中并行地运转多个测试实例以加速 Web-app 的测试。
Selenium RC 的并行测试支持
在 Selenium 时期,我们行使的 Selenium Grid 是一个零丁的项目()。Selenium Grid 的机制是启动一个 hub,然后启动多个 Selenium RC 注册到 hub 上,当测试要求到 hub 时,hub 会将测试分发给 Selenium RC, Selenium RC 会理想的启动一个阅读器完成测试。在这笔者们对它的行使不再做过量的描写,只是想分享一个之前在测试项目中碰着的成就。我们晓得,有些情形下一些软件瑕玷可以发生发火在不合个阅读器的不合操作零星情形上,或是测试需求在特定的阅读器版本上中止,那我们外行使 Selenium Grid 的情形下,若何可以确认测试是运转在什么指定的情形上呢?在我们的项目里面是经由进程更改设置配备铺排文件 grid_configuration.xml 来完成的,例如我们有 Win7、WinXP 和 Linux,上面分离装有 Firefox17 或 Firefox18,那我们可以行使上面的设置配备铺排信息:
清单 6. 自定制的 Selenium Grid 设置配备铺排文件
port: 4444
remoteControlPollingIntervalInSeconds: 180
sessionMaxIdleTimeInSeconds: 300
environments:
"Firefox 17 on Win7"
browser: "*firefox"
"Firefox 17 on WinXP"
browser: "*firefox"
"Firefox 18 on Linux"
browser: "*firefox"
这样我们在三台需求注册 Selenium RC 的机械上运转以下敕令并指定呼应的参数:
ant -Dport=&port& -Dhost=&hostname& -DhubURL=&hub url&/
-Denvironment="Firefox 17 on Win7" launch-remote-control
个中,-Dport 是指定 Selenium RC 的端口号,-DHost 指定 Selenium RC 所在的机械的主机名,-DhubURL 指定 hub 的 URL,-Denvironment 泄漏表现注册的 Selenium RC 的阅读器,在这里我们可以交流成呼应的阅读器、版本已操作零星类型。完成注册之后,在 Selenium Grid 的掌握台中可以看到可用的 Selenium RC:
图 3. 行使自定制设置配备铺排文件的 Selenium Grid 掌握台
最初,我们在代码中指定要测试的情形,hub 就可以将测试转发到呼应的情形上去运转,这样我们就可以无邪的依照测试需求在预期的情形上面完成测试。
WebDriver 的并行测试支持
在 Selenium 2 中,Selenium Grid 被集成到了 Selenium Server 中,等于包括在 selenium-server-standalone-x-x-x.jar 包中,优点就是更精练更随意了!Selenium Grid 包括有两种脚色,hub 和 node,个中 hub 是用来领受一切的要求,并将要求分发给不合的 node;node 指的就是理想实行测试的节点,它包括 Selenium 和 WebDriver 两品种型,个中 Selenium 是兼容 Selenium 1 中的 Selenium RC。上面我们看下若何启动 hub 和 node:
启动 Selenium Grid hub:
#启动 Selenium Grid 的 hub,指定端口号 4444,最多运转 40 个测试 session
java -jar selenium-server-standalone-2.28.0.jar -role hub -maxSession 40 -port 4444
挂载 node,在缺省情形下,会分离为 Selenium 和 Webdriver 的测试引擎启动 5 个 firefox,5 个 谷歌chrome,和 1 个 internet explorer。但也可以经由进程参数的体式格式来定制启动 node 所支持的阅读器,例如:
#挂载 node,支持启动 20 个 session 的版本号为 17.0.1 的 firefox 阅读器,指定端口号为 5555,指定启动 firefox 时行使的 profile
java -jar selenium-server-standalone-2.28.0.jar -role node
-hub http://{grid_server}:4444/grid/register -port 5555 /
-firefoxProfileTemplate llinotes.profile -maxSession 20 /
-browser "browserName=firefox,version=17,platform=WINDOWS,maxInstances=20"
客户端的挪用,在客户端向 Selenium Grid 的 hub 来提交一个测试要求时,对要求 Selenium 的 node 是和行使 Selenium 1 的方法是一样的,直接行使 DefaultSelenium 就可以了:
Selenium selenium = new DefaultSelenium(“{grid_server}”, /
, “*firefox”, “https://apps./”);
对 WebDriver 的 node 的话,我们需求行使 RemoteDWebriver 和 DesiredCapabilities 来界说你想向 Selenium Grid 要求的阅读器的类型,版本号等等,例以上面的 capability 就可以婚配到browserName=firefox,version=17,platform=WINDOWS的阅读器:
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName(“firefox” );
capability.setPlatform(“WINDOWS”);
capability.setVersion(“17.0.1”);
WebDriver driver = new RemoteWebDriver(/
new URL("http://{grid_driver}:4444/wd/hub"), capability);
其实,除将 Selenium Grid 兼并到 Selenium 2 中和介入对 WebDriver 的支持之外,没有太除夜的修改,Selenium Grid 的行使方面没有太除夜的区分,可以参考官方网站获得更多信息:
WebDriver 有更精练的 API,它针对各个阅读器而垦荒,庖代了嵌入到被测 Web 运用中的 JavaScript。它与阅读器的慎密集成支持树立更初级的测试,避免了 JavaScript 平安模子致使的限制。除来自阅读器厂商的支持之外,WebDriver 还可以行使操作零星级的挪用模拟用户输出,例如鼠标和键盘操作,这些都是 Selenium 所不能比照的。
而 Selenium 可以行使任何支持 HTTP 的编程措辞,它基于 Javasript 并支持多半阅读器,但它也不是完善的,因为阅读器对 Javascript 的平安计策的增强致使某些情形下不能行使 Selenium。而且跟着 WebDriver 的慢慢完善,Selenium 可以将完整不受支持,所以选择 WebDriver 势必成为一个一定趋向。但无须置疑的是两个项目的将为用户供应了一个加倍通用的丁壮夜的 Web 测试框架。
从 Selenium RC 迁移到 WebDriver
在 Selenium 2 宣布之前,许多测试团队已最先行使 Selenium 的 Selenium RC 并构建了成熟的测试框架,那末重构并将副本的测试剧本迁移到 Selenium 2 上无疑是一项异常伟除夜的责任。幸而 Selenium 2 供应了呼应的 WebDriver 做为支持。
WebDriverBackendSelenium 为用户供应 Selenium 的完成,等于,我们已行使 Selenium API 编写的测试剧本,可以在 WebDriver 上面行使。好比我们要在 Firefox 上面中止测试,在树立 selenium 实例的时辰,可以改削为:
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "https://apps./");
编译并运转测试,会发明还会存在一些 fail 的地方,这意味着你需求从新调试代码,使已有的测试剧本完整的支持 WebDriver。上面是笔者在做移植的时辰碰着的一些成就:
waitForPageToLoad():在移植到 WebDriver 后,会 fail 到测试剧本,所以需求重写来支持页面守候
fireEvent():例照完成 mou搜索引擎优化ver 的功用,在移植后可以行使 Action 类来完成,它给用户供应了一些模拟用户交互的方法,好比 mouse_to 等
keyUp,keyDown,KeyPress:在副本的 Selenium 的代码中除夜量运用了这几个方法,在移植后不合用 sendKeys()来取代了,这也正正声名了 WebDriver 有着加倍精练的 API
在移植的时辰不合的自动化测试项目碰着的成就多是不合的,但根抵都是可以在 WebDriver 中找到呼应的处置方法。现实上,单单完成上面的步骤长短常随意疏忽使原有行使 Selenium 的代码运转在 WebDriver 上面,但完成移植还有主要的一步就是重写代码使它行使 WebDriver API,这个就比拟依托于副本的代码的设计,假定挪用 Selenium API 的方法普及于 testcase、task 层等等,那末可以就需求比拟长的时辰来完整行使 WebDriver API 了。所以一个自动化测试项目的设计也长短常主要的。
使测试剧本兼容 Selenium 和 WebDriver
既然现在 Selenium 和 WebDriver 还各有优势,我们可以无邪设计本人的自动化测试项目,使其可以凭证测试需求来选择测试对象。好比,测试 Safari,我们可以选择 Selenium,测试 Firefox,Chrome 等等就选择行使 WebDriver。这样的话,就需求笼统出加倍无邪的自动化测试框架,好比,我们可以凭证页面测试元素类型来笼统出不合的 widgets,例如 WebLink,WebText 等等,对这个元素的操作都封装在 widget 之内,那末在编写测试剧本的时辰可以很除夜水平的使测试剧本和测试对象相云集,不只可以使得测试剧本可以支持不合的测试对象,也使得在移植测试剧本的时辰可以最除夜限制的下降责任量。
经由进程上文我们可以除夜致意见 Selenium、WebDriver 和它的一些浅易用法,进展本文对您的自动化测试项目有所匡助。
原文地址:, 感谢原作者分享。
您可能感兴趣的代码WebDriver (Selenium 2) API documentation - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I can't locate an official class/method/properties type API reference for WebDriver anywhere, only the 5 minute guide and Next Step pages located at , plus a few other haphazard descriptions. Does one exist?
1,32321328
closed as off-topic by ♦
This question appears to be off-topic. The users who voted to close gave this specific reason:&Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead,
and what has been done so far to solve it.& & Martijn PietersIf this question can be reworded to fit the rules in the , please .
There are javadocs , rubydocs , python docs , .NET docs , and documentation for WebDriverJS (JavaScript) .
43.9k687140
16.7k32751
The official Python API docs are here:
They're no longer missing the classes that James mentioned in his previous comment.
There's also an unofficial set of python docs here:
which is more of a walkthrough.
16.2k16264
The Javascript Bindings are well commented, but it seems that nobody bothered to genereate some html documentation from them. Anyway, browsing the code and reading the comments is helpful:
Most helpful for me was .
7,67795079
1,61021723
Russian language documentation
Selenium documentation is available .
List of the selenium drivers together with documentation:
You can download different Browser drivers .
41.2k29190261
For the WebDriverJS, you can find the documentation in the following link:
And the following were the most relevant for me:
It is also good that you can see the code for each method...
The references to API docs are located on WebDriver: Advanced Usage page in AdvancedUserInteractions chapter of their guide mentioned by question author ()
i think many has given api link already.
But just fyi
is the link that has taught me more so just thought of sharing..
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled百度文库-信息提示
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
对不起,该文档已被删除,无法查看
4秒后,自动返回首页

我要回帖

更多关于 webdriver api中文版 的文章

 

随机推荐