Selenium学习笔记(六)——Selenium Grid

什么是Selenium Grid

Selenium Grid是Selenium专门用于在不同浏览器、操作系统和机器上并行运行的组件。
Selenium Grid使用hub-nodes的结构,你可以在hub上启动测试,不过测试则会在不同的机器node上执行。
hub and nodes

什么时候使用Selenium Grid

  • 需要在不同浏览器、操作系统和机器上同时运行测试时
  • 节省测试时间

如何配置Selenium Grid

Step1. 下载Selenium Server的jar包到两台能够通讯的机器上。
(Hub)Machine A 192.168.1.102
(Node)Machine B 192.168.1.111。
Step2. 启动hub端。
运行命令: java -jar selenium-server-standalone-3.4.0.jar -role hub
hub
Step3. 验证hub状态。
在hub和node上是否能访问hub控制中心:http://192.168.1.102:4444/grid/console
Step4. 启动并注册Node到Hub。
运行命令: java Dwebdriver.ie.driver=C:/drivers/IEDriverServer.exe -jar selenium-server-standalone-3.4.0.jar -role webdriver - hub http://192.168.1.102:4444/grid/register -port 5566(node的端口可以选择任意未被占用端口,而浏览器驱动变量必须在这里提供,不能在代码中设置)
Step5. 检查是否注册成功。
打开hub的配置页面,能看到注册的nodes时,我们就已经完成了一个简单的Grid的配置:
这里写图片描述

编写Grid适用的测试脚本

编写Grid适用的测试脚本需要用到两个类:
DesiredCapabilites用于设置我们需要自动化的浏览器和系统类型;
RemoteWebDriver用于设置测试在哪些node上运行。

DesiredCapabilites

根据在hub的配置页面中可以看到注册nodes中支持的浏览器,把鼠标放在想运行的浏览器图标上可以看到设置信息。根据信息设置DesiredCapabilities:

DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
capability.setBrowserName("internet explorer");
capability.setPlatform(Platform.VISTA);
RemoteWebDriver

以node的URL和DesiredCapabilites对象为参数创建RemoteWebDriver对象。

WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.4:5566/wd/hub"), capability);
部分代码
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
// 避免IE安全设置里,各个域的安全级别不一致导致的错误 
// Caused by: org.openqa.selenium.NoSuchSessionException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setBrowserName("internet explorer");
capability.setPlatform(Platform.VISTA);
WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.111:5566/wd/hub"), capability);
String baseUrl = "http://www.baidu.com/";
driver.get(baseUrl);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值