
2.5 Selenium Grid
随着对Selenium IDE和Selenium WebDriver的学习,我们已经了解了测试脚本的编写方法。接下来,将学习Selenium Grid的用法,利用这一分布式测试执行工具来提升脚本执行效率。
2.5.1 工作原理
当测试用例需要同时在多个平台和浏览器上执行时,就可以使用Selenium Grid。如图2-44所示,其整个结构是由一个Hub节点和若干个Node(代理节点)组成的。Hub用来管理各个Node的状态,并且接受远程客户端代码的调用请求,再把请求命令转发给Node来执行。使用Selenium Grid远程执行测试代码与直接调用Selenium Server是一样的,只是环境启动的方式不一样,需要同时启动一个Hub和至少一个Node。

图2-44 Selenium Grid工作原理
2.5.2 环境搭建
首先需要为所有执行测试脚本的机器准备好Selenium WebDriver环境,可参考2.4.3小节。之后就可以开始准备Selenium Grid环境了。
1.启动Hub
启动命令为:
java -jar selenium-server-standalone-x.xx.x.jar -role hub
如图2-45所示,这里使用的是selenium-server-standalone-2.42.2.jar。

图2-45 Selenium Grid——启动Hub
启动Hub的机器可以是任意一台有Java运行环境的机器,它是整个Selenium Grid的中枢节点,所有的远程测试都会经由它转发出去,之后在对应的测试机器上执行测试。
图2-46显示默认端口是4444,可在启动时通过-port参数来指定端口。若可以成功访问http://<your_hub_host>:4444/,则说明Hub已被成功启动。单击页面中的console可以查看所有节点详情(进入console的时候速度很慢,需要耐心等待一段时间),此时尚未启动节点,因此里面是空白的,没有节点信息。

图2-46 查看默认端口的Hub是否启动成功
2.启动Node
在测试机器上打开一个终端,执行以下命令,如图2-47所示。

图2-47 Selenium Grid – 指定端口启动
java -jar selenium-server-standalone-x.xx.x.jar -role node -hub http:// <your_hub_host>:4444/ grid/register -port 4000
-hub http:// <your_hub_host>:4444/grid/register用于Node注册,即当前的测试机器作为上一步Hub的Node。-port参数是可选的,用于指定Node注册时的端口,若不加-port端口号,则默认是从5555端口启动。
若要为Hub启动多个Node,要注意为每个Node分配不同的端口。
java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4000 java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4001 java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4002
3.查看Selenium Grid状态
当Hub与Node都启动成功后,可以通过Hub的Console页面查看当前Selenium Grid的状态,直接访问地址http:// <your_hub_host>:4444/grid/console。
如图2-48所示,页面显示了可用于测试的Node数量和类型,这里显示的数量与类型和启动Node时所带的配置参数有关。启动Node其实就是一个注册过程,启动时所带的参数会被Hub记录为注册信息,所以页面中所看到的信息就是Node注册信息的汇总。

图2-48 查看所有节点的注册信息
4.代码运行
当Hub和Node都已设置完毕,且WebDriver环境已搭建完成时,便可以在代码里进行调用了。下面以简单的必应搜索功能为例来说明如何调用使用4000端口的Node机器,代码如下:
package com.selenium.test; import java.net.MalformedURLException; import java.net.URL; import org.junit.*; import org.openqa.selenium.*; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class TestGrid { private String baseUrl; @Test public void testGrid() throws Exception { baseUrl = "http://cn.bing.com/"; DesiredCapabilities capability =new DesiredCapabilities(); capability.setBrowserName("firefox"); capability.setPlatform(Platform.VISTA); WebDriver driver=null; try { driver = new RemoteWebDriver(new URL("http://192.168.1.111:4000/wd/hub"),capability); //your_node_ip : port } catch (MalformedURLException e) { e.printStackTrace(); } driver.get(baseUrl); WebElement searchElement = (new WebDriverWait(driver,10)).until(new ExpectedCondition <WebElement>(){ public WebElement apply (WebDriver wd){ return wd.findElement(By.id("sb_form_q")); } }); searchElement.clear(); searchElement.sendKeys("WebDriver"); driver.findElement(By.id("sb_form_go")).click(); driver.quit(); } }