Selenium 2 入门-2

上一篇 / 下一篇  2012-12-21 15:55:01 / 个人分类:Selenium

清单 9.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"]}0
51Testing软件测试网iH.w8U-|b2UY9~

需要额外配置以实现在 iPhone 或者 Android 浏览器模拟器中运行测试

!b2gu d,`0

Pf1LoEP+x+w I051Testing软件测试网&mF'^:Jy#L A8D#s

使用 Selenium 2 进行测试

N G:i:~Z3^"g051Testing软件测试网.aG1K8t'y uX%L

使用 Selenium 2 可以构建比前节内容更加复杂的测试。在本节中,您将测试到,GitHub 主页的顶部导航共有 5 个列表项:Signup and Pricing、Explore GitHub、Features、Blog 以及 Login。图 1展示了 Github 主页。

I DH!Ce,S0
8w*vc'AG,l{;H.l0c0图 1. Github 主页51Testing软件测试网/D P GpxS+K5sJa
Github 主页
7t Delmg![0
51Testing软件测试网;s8Vt `?/B

查看顶部导航的 HTML 代码,如 清单 10所示。

9V9xk.u J051Testing软件测试网J_:\9UUP"_!K U
清单 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>
51Testing软件测试网`1?C ^h#y

T#M/PV,G9eE/U0u]0可利用 WebDriver API(从 HTML 代码内部)检索您需要测试的元素。findElement()findElements()方法会返回公共接口WebElement的一个实例或者一组实例。WebElement可以以一种清晰的、面向对象的方式应用于页面中的所有元素。在 API 中可以使用许多不同的策略来定位 UI 元素。这些策略由传递至findElement()findElements()方法的不同类型参数所代表。清单 11展示了应用抽象类By的各个方法来实现不同策略的示例。

!W9NQ*D n`Lo7Wt0
G2|e{0Ya0清单 11. 使用findElement()方法51Testing软件测试网|Uv%ZY+X)`}0D/V(e"c
WebElement 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"));
51Testing软件测试网)~Y;M"P}$dfe TD

'w.A r:~&p0使用清单 11中的一个策略,就可以开始编写测试来检索第一个元素:UL标记中LI标记带有nav类。清单 12使用了 Xpath (By.xpath())。51Testing软件测试网 _Aq^X:Z"L'J1N


!M5a,vZw)G$XL a0清单 12. Xpath51Testing软件测试网f*n8vZxS
List<WebElement> webElements = webdriver.findElements(By 
            .xpath("//ul[@class='nav logged_out']/li"));
51Testing软件测试网'o(M{ K8l Z%i d8~
51Testing软件测试网4bJ8n8N'O{ b+oo7@9M

清单 13使用 CSS 选择器 (By.cssSelector()) 来检索LI标记。51Testing软件测试网 u-^;_rb hP t

51Testing软件测试网 f9|1s SF[
清单 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$p j051Testing软件测试网~DES]bj~ S
清单 14. 项数量上的断言51Testing软件测试网-X!|;k4?K @0@rK'M&|
Assert.assertEquals(5, webElements.size());
51Testing软件测试网 l{Q^1~.Q#Ky

xz L/~6m7E)A |0前面的步骤验证了LI标记数量等于 5。51Testing软件测试网/eD3Kqxvp

U0Wt[%J0下一步是检索每个LI标记中的每个锚点(A标记)。清单 15展示了如何在第一个LI中获取锚点。此用例使用了 tagName (By.tagName()) 策略。

?y3i^ vk!}N0
lH,a_D$Z?Y0清单 15. 检索第一个LI 标记中的A锚点51Testing软件测试网E ~,zr4_ kn;J,h3LF
WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));

9D2N?T3rF'{L6hn0
51Testing软件测试网:A.x t2F:C/[U6?

您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。

f6S x3YX1T#x(O,m9zf051Testing软件测试网{T%~pU3c
清单 16. 检索LI标记中的所有锚点51Testing软件测试网 wD9sa y~:Rb!V
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"));

v8K:C.Zk8U~ N w z0

.l+ec'P&w U'[:\0I0在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了getText()方法。清单 17展示了完整的测试方法,以及测试底部的断言。

+Y-q5a'Jfw7t1Tr051Testing软件测试网 g_Wn Q
清单 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(); 

 }
51Testing软件测试网,a:}j/U-h HUf%F(\^/E

F7b+TC-T#K(z0启动这一测试后,将会打开一个新的 Firefox 窗口,该窗口将会保持开启,直到执行完所有的断言。

@'fUR#uolV0

Kih.m6I}+h#x0使用 Selenium Grid 2 进行远程测试

'F(AG3\-B7Z G0d.qN|051Testing软件测试网/FD'@/rr7tQ0m

可在 Selenium 2 中进行本地或远程测试。要实现远程运行,该测试需要使用名为RemoteWebDriverWebDriver接口的特定实现。您可以指定浏览器采用DesiredCapabilities类运行。清单 18显示了相关示例。

E[#PI EYl0
qr1{F.ic0Nr0清单 18.RemoteWebDriverDesiredCapabilities
n7l6Zxj0
DesiredCapabilities capabilities = new DesiredCapabilities(); 
 capabilities.setBrowserName("firefox"); 
 capabilities.setVersion("7"); 
 capabilities.setPlatform("MAC"); 
 WebDriver webdriver = new RemoteWebDriver(capabilities);

Ww_|hq{ wt0
51Testing软件测试网ppt2p a L$L+u7w

借助DesiredCapabilities类,您可以指定浏览器的名称、平台和浏览器版本。您还可指定浏览器支持的其他功能。51Testing软件测试网-bbCPsj

?g&w*mlMb?/y0如果想要远程执行结构化测试,并运行多个浏览器(并且可能是不同的虚拟机),Selenium Grid 提供了很好的解决方案。51Testing软件测试网pc C(az&b S.H

51Testing软件测试网D^0~)L s$drfU u

Selenium 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
51Testing软件测试网FX0x$t+S

}^8yP^jHa0您可在 http://localhost:4444/grid/console 访问 Grid 2 控制台,其中会列出所有可用的节点。要注册一个节点,仅需运行一个命令,如 清单 20所示。51Testing软件测试网d3OS7Dy*[

51Testing软件测试网 d/ayr hGI8m
清单 20. 在 hub 中注册的节点51Testing软件测试网7EuOp9z6E
java -jar selenium-server-standalone-2.9.0.jar 
 -role webdriver ?hub http://localhost:4444/grid/register -port 5556
51Testing软件测试网-ix!Y8o Z%i

:pC}4u.}{_;i1X0在默认情况下,清单 20中的命令注册了 7 个浏览器:5 个 Firefox 实例、1 个 Chrome 实例以及一个 Internet Explorer 实例。您可以在特定的端口上定位一个特定浏览器,如 清单 21所示。

X#i6sxQG~N#U"My0
wFxjV5jS;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
51Testing软件测试网xm!_z'L} |

*px#s.J/q0X0b0注册完一些浏览器之后,Selenium Grid 2 控制台会变成如 图 2显示的样子。

ad,M3n.K2[3e[w0
/uW|~w"GF6sJ0图 2. Selenium Grid 2 控制台视图51Testing软件测试网 IJC4hM z
Grid 2 控制台视图样例
;y AN#W_7Id*x|0
51Testing软件测试网/bk-L _ @)Y-i)F

要使用网格,则需要在测试用例中指定 hub 的 URL 和所要控制的浏览器。清单 22展示了RemoteWebDriver类构造函数接受了 hub 的 URL 以及定义特定浏览器的DesiredCapabilities实例。51Testing软件测试网0C&U T-i.] `

51Testing软件测试网4Uo'~mc!uih
清单 22.RemoteWebDriver实例化
A po+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$rmS0iX|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

O| F9m(JV0

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。

M{+\6N/K%ng8Vx051Testing软件测试网3UTtkj'c(Q
清单 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(); 
 }
51Testing软件测试网f D"yGR`0g

?d,b5b,T\1BJZ0Selenium 2 更加关注开发者。它具有比 Selenium 1 更清晰的 API,正如getText()getXpathCount()方法签名所证明的一样。Selenium 2 API 还具有更好的面向对象的特点。例如,不允许您处理 UI 元素对象,仅允许处理字符串。51Testing软件测试网}Q*d#[ Ms#Gi


%\%y2g&i5U-NA051Testing软件测试网 QW!MY&\9d3B

结束语51Testing软件测试网2St4HPz6Y"mzS

)`Zs8G K8U3X0Selenium 2 标志着浏览器内部自动测试的一个演变过程。它继承了 Selenium 1 与 WebDriver 最优秀的部分,并提供了与多个浏览器的紧密集成。新的 API 更符合开发人员的要求,提供了面向对象的方法,并提供了不同的模型用于编写测试。此外,Selenium 2 还:

5r2_pZ,vA0
  • 克服与相同原始策略相关联的限制。
  • 为弹出窗口提供更好的支持。
  • 有效控制本机的键盘与鼠标的交互。
51Testing软件测试网 ?3m|!n{v4j

Selenium Grid 的新版本(包括在 Selenium 2 中)使得远程加载测试更加方便。Selenium 2 能够向后兼容 Selenium 1,因此升级到新版本轻而易举。Selenium 2 能够帮助确保应用程序按需工作51Testing软件测试网q%Gf&Gw g

51Testing软件测试网)TYapL:VN

TAG:

 

评分:0

我来说两句

Open Toolbar