为什么要使用Selenium Grid ?

  • 分布式运行大规模的Test
  • 能够通过一个中央点,很容易的运行不同OS上的不同browser
  • 最小化对Grid的维护时间,并能充分利用虚拟设备

Selenium Grid 部署与启动

Hub :总控节点,连接调用Node。 
Node: 负责执行Tests,调用浏览器。

下面以selenium-server-standalone-2.27.0.jar版本为例: 
使用这样3台机器:

  • 10.81.14.170
  • 10.81.14.180
  • 10.81.14.190

启动Hub(10.81.14.180):

java  -jar  selenium-server-standalone-2.27.0.jar -role hub

在浏览器内打开:http://10.81.14.180:4444/grid/console可以查看Hub状态。也就是说Grid默认启动端口是4444,如果想切换为其他端口,则加-port参数。比如要切换为8888:

java  -jar  selenium-server-standalone-2.27.0.jar -role hub  -port 8888

启动Node(10.81.14.170):

java -jar selenium-server-standalone-2.27.0.jar -role node -hub http://10.81.14.180:8888/grid/register

同样的,也可以使用-port切换node端口,默认端口是5555. 
此处的node节点,也可以作为一个单机的远程节点存在,并同时支持RC,WebDriver。浏览器输入http://10.81.14.180:8877/wd/hub可以看到session信息。

然后,同样的启动10.81.14.180、10.81.14.190上的Node节点。

打开浏览器http://10.81.14.180:8888/grid/console,可以看到如下的界面:

至此,Selenium Grid2已经配置成功了。

使用Grid运行Tests

Selenium Grid2是向后兼容的,同时支持RC,WebDriver。如果使用RC,即Selenium1,使用以下的方法:

Selenium selenium = new DefaultSelenium(“10.81.14.180”, 8888, “*firefox”, “http://www.baidu.com”);

使用WebDriver的话,使用以下的方法:

DesiredCapabilities capability = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://10.81.14.180:8888/wd/hub"), capability);

可以看出所有的请求都发给了Hub,然后由Hub分配给匹配的节点来执行。 
那么,Hub是如何来分配的呢?往下看

Node配置

默认,Node会启动11个浏览器实例:5 Firefox,5 Chrome, 1 Internet Explorer. 从Grid Console界面看出来,为什么每个机器上有22个实例呢?是这样的,Node为了同时支持RC与WebDriver两种协议,所以就是2*11了。把鼠标放到各个浏览器图标上,就可以看出里面的配置区别了。
内容类似:

{
"browserName": "*firefox",
"maxInstances": 5,
"seleniumProtocol": "Selenium"
}

或者

 {
"browserName": "firefox",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
}

其中,seleniumProtocol就是定义的不同协议了。

如何修改Driver配置呢?可以从启动参数里操作。

-browser browserName=firefox,version=3.6,maxInstances=5,platform=LINUX

那Node默认启动的配置是什么呢? 
由于如果从启动参数里,配置这个多东西,很难写的。因此,官方很人性化的提供了JSON文件来配置。也就是说默认启动的配置如下: 
http://code.google.com/p/selenium/source/browse/trunk/java/server/src/org/openqa/grid/common/defaults/DefaultNode.json

http://code.google.com/p/selenium/source/browse/trunk/java/server/src/org/openqa/grid/common/defaults/DefaultHub.json

如果想自定义配置,直接对json文件修改,启动时,指定配置文件就可以了。

java -jar selenium-server-standalone.jar -role hub -hubConfig hubconfig.json

仅仅就这样就行了?从博文http://www.shenyanchao.cn/blog/2012/10/12/selenium-multiple-browser-support/知道,浏览器的启动是要制定一些driver位置的,否则Node不知道怎么启动浏览器实例。因此需要进行指定:

java -jar selenium-server-standalone-2.27.0.jar -port 8877 -role node -hub http://10.81.14.180:8888/grid/register  -nodeConfig nodeconfig.json -Dwebdriver.chrome.driver="E:/selenium/chromedriver.exe" -Dwebdriver.ie.driver="E:/selenium/IEDriverServer.exe"

参考文档:
http://code.google.com/p/selenium/wiki/Grid2

客户端书写测试用例


package seleniumdemo;

import java.net.MalformedURLException;
import java.net.URL; import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver; public class EDemo2 { public static void main(String[] args) throws MalformedURLException, InterruptedException {
// RemoteWebDriver的基本使用 //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub/"), DesiredCapabilities.chrome());
driver.manage().window().maximize();
driver.get("http://www.baidu.com");
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("alert('我现在在服务器')");
Thread.sleep(2000);
driver.quit();
} }

执行脚本

其实脚本是在服务器端执行的,客户端用于发送命令。执行过程中可以看到服务器端的chrome别调用,同时cmd打印出相关的运行信息,如下 

Selenium-Grid2 配置RemoteWebDriver的更多相关文章

  1. Selenium Grid2

    简介 使用selenium-grid可以远程执行测试的代码,核心步骤:grid --> server-->chromedriver驱动 -->chrome浏览器 利用Selenium ...

  2. Python+selenium之selenium Grid2

    利用selenium grid2 keyi可以在不同的主机上建立主节点(hub)和分支节点(node),可以使主节点上的测试用例在不同的分支节点上运行.对不同的节点来说,可以搭建不同的测试环境(操作系 ...

  3. 9、Selenium grid2

    P228--Selenium Grid2 P233--Selenium Grid 工作原理 P236--Remote 应用 P246--WebDriver 驱动 driver = webdriver. ...

  4. C# selenium环境配置

    1.下载C#selenium     selenium官网:  http://www.seleniumhq.org/download/   下载后解压:     打开net35后,将里面的dll文件添 ...

  5. selenium Grid2环境搭建和基本使用

    Selenium Grid简介 利用Selenium Grid可以使主节点(hub)的测试用例在不同主机即分支点(node)运行.可以使一份测试用例在不同环境下(操作系统.浏览器)执行自动化测试.Se ...

  6. Selenium+python 配置

    1. 安装python, www.python.org. 下载最新的python,应该是32位的.注意配置环境变量. 2. 安装PIP(pip是一个以Python计算机程序语言写成的软件包管理系统). ...

  7. Python+Selenium 环境配置之Firefox,IE,Chrome几种浏览器运行

    Selenium(Webdriver)支持Firefox,IE,Chrome等多个浏览器.很多人可能装环境时遇到很多问题,下面简单聊聊如何配置测试这几个浏览器以及相关通过简单的实例来测试. 1.Fir ...

  8. 【Selenium】idea的selenium环境配置

    1.maven配置 下载地址:http://maven.apache.org/download.cgi# 下载内容:apache-maven-3.5.0-bin.zip 环境变量:M2_HOME:E: ...

  9. Selenium chrome配置代理Python版

    环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit) S ...

随机推荐

  1. java内存溢出怎么解决

    java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的原因大都出于以下原因:JVM内存过小.程序不严密,产生了过多的垃圾. 导致OutOfMemory ...

  2. 数据库面试题之PL/SQL面试题

    create table employee( id ) not null, -- 员工工号 salary ,) not null, -- 薪水 name ) not null -- 姓名 ); 第一题 ...

  3. C++ map修改指定key的value

    对于修改C++指定key的value,网上查了很多,都说直接insert就会覆盖原来的值,是否是这样的呢?  C++ Code  12345678910111213141516171819202122 ...

  4. Eclipse导入MyEclipseproject(web项目显示为java项目解决的方法)

    在直接Import MyEclipse的项目文件导入到Eclipse之后,须要在项目所放的workspace内改动引入项目文件夹下的.project文件,改动例如以下: 1.在eclipse中新建一个 ...

  5. 20分钟成功编写bootstrap响应式页面 就这么简单

    最近发现一个叫 Bootstrap 的好东西,Bootstrap 是现在最流行的响应式 CSS 框架,它以移动设备优先,能够快速适应不同设备.使用它编写响应式页面快捷.方便,而且屏蔽了浏览器差异.使用 ...

  6. spring @Transactional注解参数详解(转载)

    事物注解方式: @Transactional 当标于类前时, 标示类中所有方法都进行事物处理 , 例子: 1 @Transactional public class TestServiceBean i ...

  7. Zabbix添加web页面监控告警

    一,选择添加了web监控的主机 二,创建一个告警触发器 三,定义监控项 设置完毕假如网站down就会触发告警 怎么设置web监控以及触发告警action参考 Zabbix使用SMTP发送邮件报警并且制 ...

  8. Zabbix监控Windows主机

    一,下载zabbix-agent 下载地址:http://www.zabbix.com/downloads/3.0.0/zabbix_agents_3.0.0.win.zip 已经下载好的文件 zab ...

  9. readAsDataURL

    w http://stackoverflow.com/questions/14069421/show-an-image-preview-before-upload <input type=&qu ...

  10. LeetCode_Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...