---
title: "如何使用Selenium-Grid"
author: "changfengfu"
date: "2013-11-26T10:24:55Z"
tags: []
categories: ["行业应用"]
slug: "selenium-grid"
draft: false
lastmod: "2013-11-26T10:24:55Z"
id: 1980765
url: "https://changfengfu.iteye.com/blog/1980765"
---

前面说到了Selenium-Server的前2种启动方式，现在再看看剩下的2种启动方式。

**3、作为hub启动**

**\[java]** [view plain](http://blog.csdn.net/five3/article/details/9415067 "view plain")[copy](http://blog.csdn.net/five3/article/details/9415067 "copy")

 

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

**参数说明：**  
\-role：hub  
启动一个hub服务，等待webdriver客户端进行注册和请求。默认启动端口是4444，默认接收注册的地址为：http://localhost:4444/grid/register

-hubConfig：\[filename]  
一个符合selenium grid2规则的json格式的hub配置文件，样例如下：\[其实这个就是selenium-grid的默认使用的配置文件]

**\[java]** [view plain](http://blog.csdn.net/five3/article/details/9415067 "view plain")[copy](http://blog.csdn.net/five3/article/details/9415067 "copy")

 

01. {
02. "host": null,
03. "port": 4444,
04. "newSessionWaitTimeout": -1,
05. "servlets" : \[],
06. "prioritizer": null,
07. "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
08. "throwOnCapabilityNotPresent": true,
09. "nodePolling": 5000,
10. "cleanUpCycle": 5000,
11. "timeout": 300000,
12. "browserTimeout": 0,
13. "maxSession": 5
14. }

-throwOnCapabilityNotPresent: \[true|false]  
默认为true，如果为true则hub只有在当前有测试代理注册的情况下才会接受测试请求；如果为false则如果当前没有代理注册也会接受请求保存到队列直到有代理注册为止。

-capabilityMatcher:xxx  
一个实现了CapabilityMatcher接口的类，默认指向org.openqa.grid.internal.utils.DefaultCapabilityMatcher；该类用于实现grid在分布测试任务到对应代理时所使用的匹配规则，如果想要更精确的测试分配规则，那么就注册一个自己定义的匹配类。

-prioritizer:XXXclass  
一个实现了Prioritizer接口的类。设置grid执行test任务的优先逻辑；默认为null，先来先执行。

-port：xxx  
指定hub监听的端口

-host：ip\_or\_host  
指定hub机的ip或者host值

-newSessionWaitTimeout:XXX  
默认-1，即没有超时；指定一个新的测试session等待执行的间隔时间。即一个代理节点上前后2个测试中间的延时时间，单位为毫秒。

-servlets: XXXserlet  
在hub上注册一个新的serlet，访问地址为/grid/admin/XXXserlet

-browserTimeout:  
浏览器无响应的超时时间

-grid1Yml:  
一个符合grid1规则的yml文件，仅适用于grid1

**4、作为webdriver或者node启动**

**\[java]** [view plain](http://blog.csdn.net/five3/article/details/9415067 "view plain")[copy](http://blog.csdn.net/five3/article/details/9415067 "copy")

 

1. java -jar selenium-server-standalone.jar -role wd -hub http://localhost:4444/grid/register
2. java -jar selenium-server-standalone.jar -role rc -hub http://localhost:4444/grid/register
3. java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register

**参数说明：**  
\-role: \[node|wd|rc]  
为node值时表示注册的RC可以支持selenium1、selenium2两种方式的测试请求，推荐；  
为wd值时表示注册的RC仅支持selenium2的webdriver方式的测试请求，遗留；  
为rc值时表示注册的RC仅支持selenium1方式的测试请求，遗留。

-hub：url\_to\_hub  
url\_to\_hub值为hub启动的注册地址，默认为ttp://ip\_for\_hub:4444/grid/register；具体的根据你启动hub时的参数所对应。  
该选项包含了-hubHost和-hubPort两个选项

-hubHost：ip\_or\_host  
指定hub机的ip或者host值

-host：ip\_or\_host  
同-hubHost选项

-hubPort：xxx  
指定hub机的监听端口

-port：XXX  
同-hubPort选项

-registerCycle：xxx  
代理节点自动重新注册的周期，单位毫秒；适应于重启了hub时不需要重启所有的代理节点。

-nodePolling:XXX  
hub检查代理节点的周期

-unregisterIfStillDownAfter:XXX  
单位毫秒，设定代理节点在无响应多长时间后hub才会注销代理节点注册信息；默认1分钟

-nodeTimeout：xxx  
客户端的无心跳超时时间

-maxSession：xx  
一个代理节点可以同时启动的浏览器最大数量，即session数量

-cleanupCycle:XXX  
代理节点检查超时的周期

-nodeConfig: json\_file  
一个符合selenium grid2规则的json格式的node配置文件，样例

**\[java]** [view plain](http://blog.csdn.net/five3/article/details/9415067 "view plain")[copy](http://blog.csdn.net/five3/article/details/9415067 "copy")

 

01. {
02. "capabilities":
03. [
04. {
05. "browserName": "\*firefox",
06. "maxInstances": 5,
07. "seleniumProtocol": "Selenium"
08. },
09. {
10. "browserName": "\*googlechrome",
11. "maxInstances": 5,
12. "seleniumProtocol": "Selenium"
13. },
14. {
15. "browserName": "\*iexplore",
16. "maxInstances": 1,
17. "seleniumProtocol": "Selenium"
18. },
19. {
20. "browserName": "firefox",
21. "maxInstances": 5,
22. "seleniumProtocol": "WebDriver"
23. },
24. {
25. "browserName": "chrome",
26. "maxInstances": 5,
27. "seleniumProtocol": "WebDriver"
28. },
29. {
30. "browserName": "internet explorer",
31. "maxInstances": 1,
32. "seleniumProtocol": "WebDriver"
33. }
34. ],
35. "configuration":
36. {
37. "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
38. "maxSession": 5,
39. "port": 5555,
40. "host": ip,
41. "register": true,
42. "registerCycle": 5000,
43. "hubPort": 4444,
44. "hubHost": ip
45. }
46. }

-servlets: XXXserlet  
在node上注册一个新的serlet，访问地址为/grid/admin/XXXserlet

-proxy: 代理类  
默认指向org.openqa.grid.selenium.proxy.DefaultRemoteProxy；用于代表节点的代理

-browserTimeout:  
浏览器无响应的超时时间

-browser:browserName=firefox,version=3.6,platform=LINUX  
设置代理节点的注册信息，这些信息同样可以在配置文件里设置;该参数可以使用多次用以同时注册多个浏览器信息。  
可以设置的信息有：

**\[java]** [view plain](http://blog.csdn.net/five3/article/details/9415067 "view plain")[copy](http://blog.csdn.net/five3/article/details/9415067 "copy")

 

1. browserName=firefox
2. version=3.6
3. platform=LINUX
4. maxInstances=5
5. firefox\_binary=/home/myhomedir/firefox36/firefox
6. chrome\_binary="C:\\program file\\chrome\\chrome.exe"

 

**注：**

在node方式启动的命令中，除了上面列出的选项外，还可以附加独立RC方式启动时所支持的选项；因为以node方式启动时，除了可以和hub通信外，还包含了独立RC服务器的所有功能。