Selenium 2 入门-2
上一篇 / 下一篇 2012-12-21 15:55:01 / 个人分类:Selenium
OperaDriver
依赖性51Testing软件测试网
f#@/fa1c
cl#f(H<dependency> <groupId>com.opera</groupId> <artifactId>operadriver</artifactId> <version>0.7.3</version> </dependency> |
2V/_;r6f4@8BX)P"]}051Testing软件测试网i H.w8U-|b2UY9~
需要额外配置以实现在 iPhone 或者 Android 浏览器模拟器中运行测试。
!b2gu d,`0使用 Selenium 2 可以构建比前节内容更加复杂的测试。在本节中,您将测试到,GitHub 主页的顶部导航共有 5 个列表项:Signup and Pricing、Explore GitHub、Features、Blog 以及 Login。图 1展示了 Github 主页。
I DH!Ce,S08w*vc'AG,l{;H.l0c0图 1. Github 主页51Testing软件测试网/D PGpxS+K5sJa
7t Delm g![051Testing软件测试网;s8Vt`?/B
查看顶部导航的 HTML 代码,如 清单 10所示。
9V9xk.uJ051Testing软件测试网J_:\9UUP"_!KU清单 10. 顶部导航的 HTML 代码
&d7N0Tb!^:fu:k0
<html> <head> ... </head> <body class="logged_out env-production"> <div id="main"> <div id="header" class="true"> ... <div class="topsearch"> <ul class="nav logged_out"> <li class="pricing"> <a href="https://github.com/plans">Signup and Pricing</a> </li> <li class="explore"> <a href="https://github.com/explore">Explore GitHub</a> </li> <li class="features"> <a href="https://github.com/features">Features</a> </li> <li class="blog"> <a href="https://github.com/blog">Blog</a> </li> <li class="login"> <a href="https://github.com/login">Login</a> </li> </ul> </div> ... </div> ... </div> ... </body> </html> |
T#M/PV,G9eE/U0u]0可利用 WebDriver API(从 HTML 代码内部)检索您需要测试的元素。findElement()
和findElements()
方法会返回公共接口WebElement
的一个实例或者一组实例。WebElement
可以以一种清晰的、面向对象的方式应用于页面中的所有元素。在
API 中可以使用许多不同的策略来定位 UI 元素。这些策略由传递至findElement()
和findElements()
方法的不同类型参数所代表。清单 11展示了应用抽象类By
的各个方法来实现不同策略的示例。
G2|e{0Ya0清单 11. 使用
findElement()
方法51Testing软件测试网|Uv%ZY+X)`}0D/V(e"cWebElement element1 = webdriver.findElement(By.id("header")); WebElement element2 = webdriver.findElement(By.name("name")); WebElement element3 = webdriver.findElement(By.tagName("a")); WebElement element4 = webdriver.findElement(By.xpath("//a[@title='logo']")); WebElement element5 = webdriver.findElement(By.cssSelector(".feautures")); WebElement element6 = webdriver.findElement(By.linkText("Blog")); WebElement element7 = webdriver.findElement(By.partialLinkText("Ruby")); WebElement element8 = webdriver.findElement(By.className("login")); |
'w.Ar:~&p0使用清单 11中的一个策略,就可以开始编写测试来检索第一个元素:UL
标记中LI
标记带有nav
类。清单 12使用了 Xpath
(By.xpath()
)。51Testing软件测试网 _Aq^X:Z"L'J1N
!M5a,v Zw)G$XL a0清单 12. Xpath51Testing软件测试网f*n8vZxS
List<WebElement> webElements = webdriver.findElements(By .xpath("//ul[@class='nav logged_out']/li")); |
51Testing软件测试网4bJ8n8N'O{ b+oo7@9M
清单 13使用 CSS 选择器 (By.cssSelector()
) 来检索LI
标记。51Testing软件测试网
u-^;_rb hP t
清单 13. CSS 选择器
L.?)[1c KZ7u RC2F P"Ua0
List<WebElement> webElements = webdriver.findElements(By .cssSelector("ul.nav li")); |
.A.J*c7x:u0
9@7g[KY0此时,可在所检索的项数量上生成断言,如 清单 14所示。
rJ/Q|@D t${-E$pj051Testing软件测试网~DES]bj~ S清单 14. 项数量上的断言51Testing软件测试网-X!|;k4?K @0@rK'M&|
Assert.assertEquals(5, webElements.size()); |
xz L/~6m7E)A
|0前面的步骤验证了LI
标记数量等于 5。51Testing软件测试网/eD3Kqxvp
U0Wt[%J0下一步是检索每个LI
标记中的每个锚点(A
标记)。清单
15展示了如何在第一个LI
中获取锚点。此用例使用了 tagName (By.tagName()
)
策略。
lH,a_D$Z?Y0清单 15. 检索第一个
LI 标记
中的A
锚点51Testing软件测试网E~,zr4_
kn;J,h3LFWebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); |
9D2N?T3rF'{L6hn051Testing软件测试网:A.x t2F:C/[U6?
您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。
f6S x3YX1T#x(O,m9zf051Testing软件测试网{T%~pU3c清单 16. 检索
LI
标记中的所有锚点51Testing软件测试网 wD9sa y~:Rb!VWebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); WebElement anchor2 = webElements.get(1).findElement(By.tagName("a")); WebElement anchor3 = webElements.get(2).findElement(By.tagName("a")); WebElement anchor4 = webElements.get(3).findElement(By.tagName("a")); WebElement anchor5 = webElements.get(4).findElement(By.tagName("a")); |
v8K:C.Zk8U~N w z0
.l+ec'P&w U'[:\0I0在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了getText()
方法。清单 17展示了完整的测试方法,以及测试底部的断言。
清单 17. 完成测试
4w0UD v9VV0
@Test public void test() { WebDriver webdriver = new FirefoxDriver(); webdriver.get("https://github.com"); List<WebElement> webElements = webdriver.findElements(By .xpath("//ul[@class='nav logged_out']/li")); Assert.assertEquals(5, webElements.size()); // Retrieve the anchors WebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); WebElement anchor2 = webElements.get(1).findElement(By.tagName("a")); WebElement anchor3 = webElements.get(2).findElement(By.tagName("a")); WebElement anchor4 = webElements.get(3).findElement(By.tagName("a")); WebElement anchor5 = webElements.get(4).findElement(By.tagName("a")); // Assertions Assert.assertEquals("Signup and Pricing", anchor1.getText()); Assert.assertEquals("Explore GitHub", anchor2.getText()); Assert.assertEquals("Features", anchor3.getText()); Assert.assertEquals("Blog", anchor4.getText()); Assert.assertEquals("Login", anchor5.getText()); webdriver.quit(); } |
F7b+TC-T#K(z0启动这一测试后,将会打开一个新的 Firefox 窗口,该窗口将会保持开启,直到执行完所有的断言。
@'fU R#uolV0回页首51Testing软件测试网;F\ZD7u5NS.s
Kih.m6I}+h#x0使用 Selenium Grid 2 进行远程测试
'F(AG3\-B7Z G0d.qN|051Testing软件测试网/FD'@/rr7tQ0m可在 Selenium 2 中进行本地或远程测试。要实现远程运行,该测试需要使用名为RemoteWebDriver
的WebDriver
接口的特定实现。您可以指定浏览器采用DesiredCapabilities
类运行。清单
18显示了相关示例。
qr1{F.ic0Nr0清单 18.
RemoteWebDriver
和DesiredCapabilities
类n7l6Zxj0
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setBrowserName("firefox"); capabilities.setVersion("7"); capabilities.setPlatform("MAC"); WebDriver webdriver = new RemoteWebDriver(capabilities); |
Ww_|hq{ wt051Testing软件测试网ppt2p a L$L+u7w
借助DesiredCapabilities
类,您可以指定浏览器的名称、平台和浏览器版本。您还可指定浏览器支持的其他功能。51Testing软件测试网-bbCPsj
?g&w*mlMb?/y0如果想要远程执行结构化测试,并运行多个浏览器(并且可能是不同的虚拟机),Selenium Grid 提供了很好的解决方案。51Testing软件测试网 pcC(az&bS.H
51Testing软件测试网D^0~)Ls$drfU uSelenium Grid 2 提供了基础设施,其中每个节点代表了不同浏览器将自身注册到 hub 当中。单数的测试将会调用一个 hub,它负责将每个请求分配到正确的浏览器。Hub 和节点可以运行在不同的虚拟机当中。51Testing软件测试网q#QIT9V'@%W
51Testing软件测试网9C@\ WIq*{要实现远程测试,则需要在您将要使用的每台机器上下载 selenium-server-standalone-<version>.jar。要在机器上安装 hub,转到您下载 JAR 所在的文件夹,并运行 清单 19中的命令。
Ptr7Xx;Y0Fqn3tb051Testing软件测试网`N7c2c5^SU.OB'z清单 19. 启动 hub51Testing软件测试网P'Q A/_ q8lF
java -jar selenium-server-standalone-2.9.0.jar ?role hub |
}^8yP^jHa0您可在 http://localhost:4444/grid/console 访问 Grid 2 控制台,其中会列出所有可用的节点。要注册一个节点,仅需运行一个命令,如 清单 20所示。51Testing软件测试网d3OS7Dy*[
51Testing软件测试网 d/ayrhG I8m清单 20. 在 hub 中注册的节点51Testing软件测试网7EuOp9z6E
java -jar selenium-server-standalone-2.9.0.jar -role webdriver ?hub http://localhost:4444/grid/register -port 5556 |
:pC}4u.}{_;i1X0在默认情况下,清单 20中的命令注册了 7 个浏览器:5 个 Firefox 实例、1 个 Chrome 实例以及一个 Internet Explorer 实例。您可以在特定的端口上定位一个特定浏览器,如 清单 21所示。
X#i6sxQG~N#U"My0wFxjV5jS;j0清单 21. 在 hub 上注册的 Firefox 7 实例51Testing软件测试网2_q.lgRh
java -jar selenium-server-standalone-2.9.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,version=14,platform=MAC |
*px#s.J/q0X0b0注册完一些浏览器之后,Selenium Grid 2 控制台会变成如 图 2显示的样子。
ad,M3n.K2[3e[w0/uW|~w"GF6sJ0图 2. Selenium Grid 2 控制台视图51Testing软件测试网 IJC4hMz
;y AN#W_7Id*x|051Testing软件测试网/bk-L _@)Y-i)F
要使用网格,则需要在测试用例中指定 hub 的 URL 和所要控制的浏览器。清单 22展示了RemoteWebDriver
类构造函数接受了 hub 的 URL 以及定义特定浏览器的DesiredCapabilities
实例。51Testing软件测试网0C&U T-i.] `
清单 22.
RemoteWebDriver
实例化Apo+aD_5q2T0
DesiredCapabilities capability = new DesiredCapabilities(); capability.setBrowserName("chrome"); capability.setVersion("14"); capability.setPlatform(Platform.MAC); WebDriver webdriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability); |
{wOc;]:r0
!}J&A&]&kV1z0在本用例中,hub 将会启动与 Chrome 版本 14(之前已在清单 21中注册的)相关联的节点。
:Q?rh%F O0%\|hLG+@i0Selenium Grid 2 还向后兼容 Selenium 1。您可以在 hub 中注册 Selenium 1 RC 节点(这是 hub 中 Selenium 1 基础设施的一部分),如 清单 23所示。51Testing软件测试网-i3_*V;r,V.t+Fl]
*}(b$rm S0iX|0清单 23. Selenium 1 RC 节点注册51Testing软件测试网 AdV/\-x
java ?jar selenium-server-standalone-2.9.0.jar -role rc ?hub http://localhost:4444/grid/register -port 5557 |
6Vl+J1ya K0
NZ&_2OP0将测试从 Selenium 1 迁移到 Selenium 2
\V#BuH|*^)y.S)L051Testing软件测试网5`m3}!`,S7HR wH如果需要将编写好的测试从 Selenium 1 转移到 Selenium 2,那么这种转移会相当平滑。Selenium 1 API 保留在新的 API 中,使得 Selenium 2 能够完全向后兼容。51Testing软件测试网'JNc"xB;iV"N$re
#E)}L~
Q-Pp0将测试从 Selenium 1 转移到 Selenium 2 非常简单,这要归功于WebDriverBackedSelenium
类。它将一个WebDriver
的实例和 URL
作为测试的参数,并且返回一个 Selenium 实例。清单 24展示了与清单 16相同示例,但是使用集成到
Selenium 2 中的 Selenium 1 API。
清单 24. 将 Selenium 1 集成到 Selenium 251Testing软件测试网8S2f;`f/qI(g
@Test public void test() { String url = "https://github.com"; WebDriver webdriver = new FirefoxDriver(); webdriver.get(url); Selenium selenium = new WebDriverBackedSelenium(webdriver, url); selenium.open(url); // Get the number of LIs Number lis = selenium.getXpathCount("//ul[@class='nav logged_out']/li"); Assert.assertEquals(5, lis.intValue()); // Retrieve the text inside the anchors String anchor1Text = selenium.getText("//ul[@class='nav logged_out']/li[1]/a"); String anchor2Text = selenium.getText("//ul[@class='nav logged_out']/li[2]/a"); String anchor3Text = selenium.getText("//ul[@class='nav logged_out']/li[3]/a"); String anchor4Text = selenium.getText("//ul[@class='nav logged_out']/li[4]/a"); String anchor5Text = selenium.getText("//ul[@class='nav logged_out']/li[5]/a"); Assert.assertEquals("Signup and Pricing", anchor1Text); Assert.assertEquals("Explore GitHub", anchor2Text); Assert.assertEquals("Features", anchor3Text); Assert.assertEquals("Blog", anchor4Text); Assert.assertEquals("Login", anchor5Text); webdriver.quit(); } |
?d,b5b,T\1BJZ0Selenium 2 更加关注开发者。它具有比 Selenium 1 更清晰的 API,正如getText()
和getXpathCount()
方法签名所证明的一样。Selenium 2 API 还具有更好的面向对象的特点。例如,不允许您处理 UI
元素对象,仅允许处理字符串。51Testing软件测试网}Q*d#[Ms#Gi
结束语51Testing软件测试网2St4H Pz6Y"mzS
)`Zs8GK8U3X0Selenium 2 标志着浏览器内部自动测试的一个演变过程。它继承了 Selenium 1 与 WebDriver 最优秀的部分,并提供了与多个浏览器的紧密集成。新的 API 更符合开发人员的要求,提供了面向对象的方法,并提供了不同的模型用于编写测试。此外,Selenium 2 还:
5r2_pZ,vA0- 克服与相同原始策略相关联的限制。
- 为弹出窗口提供更好的支持。
- 有效控制本机的键盘与鼠标的交互。
Selenium Grid 的新版本(包括在 Selenium 2 中)使得远程加载测试更加方便。Selenium 2 能够向后兼容 Selenium 1,因此升级到新版本轻而易举。Selenium 2 能够帮助确保应用程序按需工作。51Testing软件测试网q%Gf&Gw g
51Testing软件测试网)TYapL:VNTAG:
不要让那些真正对你好的人,慢慢的从你的生活中消失,无论爱情还是友情,都需要用心经营。
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 3338782
- 日志数: 1640
- 建立时间: 2011-12-07
- 更新时间: 2019-12-24