启动中心
通过在命令行执行以下命令,可以启动一个使用默认设置的中心。所有平台可用,包括 Windows Linux, 或 MacOs 。
java -jar selenium-server-standalone-2.21.0.jar -role hub
我们将在接下来的章节中解释各个参数。注意,你可能需要修改上述命令中 jar 包的版本号,这取决于你使用的 selenium-server 的版本。
启动节点
通过在命令行执行以下命令,可以你懂一个使用默认设置的节点。
java -jar selenium-server-standalone-2.21.0.jar -role node -hub http://localhost:4444/grid/register
该操作假设中心是使用默认设置启动的。中心用于监听请求使用的默认端口号为 4444,这就是为什么端口 4444 被用于中心 url 中。同时“localhost”假定你的节点和中心运行在同一台机器上。对于新手来说,这是最简单的方式。如果要在两台不同的机器上运行中心和节点,只需要将“localhost”替换成中心所在机器的 hostname 即可。
警告: 确保运行中心和节点的机器均已关闭防火墙,否则你将看到一个连接错误。
配置 Selenium-Grid
默认配置
JSON 配置文件
通过命令行选项配置
中心配置
通过指定 -role hub 即以默认设置启动中心:
java -jar selenium-server-standalone-2.21.0.jar -role hub
你将看到以下日志输出:
Jul 19, 2012 10:46:21 AM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid server
2012-07-19 10:46:25.082:INFO:osjs.Server:jetty-7.x.y-SNAPSHOT
2012-07-19 10:46:25.151:INFO:osjsh.ContextHandler:started o.s.j.s.ServletContextHandler{/,null}
2012-07-19 10:46:25.185:INFO:osjs.AbstractConnector:Started SocketConnector@0.0.0.0:4444
指定端口
中心默认使用的端口是 4444 。这是一个 TCP/IP 端口,被用于监听客户端,即自动化测试脚本到 Selenium-Grid 中心的连接。如果你电脑上的另一个应用已经占用这个接口,或者你已经启动了一个 Selenium-Server,你将看到以下输出:
10:56:35.490 WARN - Failed to start: SocketListener0@0.0.0.0:4444
Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
如果看到这个信息,你可以关掉在使用端口 4444 的进程,或者告诉 Selenium-Grid 使用一个别的端口来启动中心。-port 选项用于修改中心的端口:
java -jar selenium-server-standalone-2.21.0.jar -role hub -port 4441
即使已经有一个中心运行在这台机器上,只要它们不使用同一个端口,就能正常工作。
你可能想知道哪个进程使用了 4444 端口,这样你就可以让中心使用这个默认端口。使用以下命令可以查看你机器上所有运行程序使用的端口:
netstat -a
Unix/Linux, MacOs 和 Windows 均支持此命令,只是在 Windows 中 -a 参数为必须的。基本上,你需要显示进程 id 和端口。在 Unix 中,你可以通过管道 “grep” 输出那些你关心的端口相关的条目。
节点配置
时间参数
获取命令行帮助
Selenium-Server 提供了一个可选项列表,每个选项都有一个简短的描述。目前(2012夏),命令行帮助还有一些奇怪,但是如果你知道如何去找、如何解读信息会对你很有帮助。
Selenium-Server 提供了两种不同的功能,Selenium-RC server 和 Selenium-Grid。它们是两个不同的团队编写的,所以每个功能的命令行帮助被放置在不同的地方。因此,对于新手来说,在初次使用任意一个功能时,帮助都不是那么显而易见。
如果你仅传递一个 -h 选项,你将看到 Selenium-RC Server 的可选项而不是 Selenium-Grid 的。
java -jar selenium-server-standalone-2.21.0.jar -h
上述代码将显示 Selenium-RC server 选项。如果你想看到 Selenium-Grid 的命令行帮助,你需要先使用 -hub 或 -node 选项告诉 Selenium-Server 你想看的是关于 Selenium-Grid 的,然后再追加 -h 选项。
java -jar selenium-server-standalone-2.21.0.jar -role node -h
对于这个问题,你还可以给 -role node 传递一个垃圾参数:
java -jar selenium-server-standalone-2.21.0.jar -role node xx
你将先看到 “INFO...” 和一个 “ERROR”,在其后你将看到 Selenium-Grid 的命令行选项。我们没有列出这个命令的所有输出,因为它实在太长了,这个输出的最初几行看起来如下:
Jul 19, 2012 10:10:39 AM org.openqa.grid.selenium.GridLauncher main INFO: Launching a selenium grid node org.openqa.grid.common.exception.GridConfigurationException: You need to specify a hub to register to using -hubHost X -hubPort 5555. The specified config was -hubHost null -hubPort 4444 at org.openqa.grid.common.RegistrationRequest.validate(RegistrationRequest.java:610) at org.openqa.grid.internal.utils.SelfRegisteringRemote.startRemoteServer(SelfRegisteringRemote.java:88) at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:72) Error building the config :You need to specify a hub to register to using -hubHost X -hubPort 5555. The specified config was -hubHost null -hubPort 4444 Usage : -hubConfig: (hub) a JSON file following grid2 format. -nodeTimeout: (node) <XXXX> the timeout in seconds before the hub automatically ends a test that hasn't had aby activity than XX sec.The browser will be released for another test to use.This typically takes care of the client crashes. |
常见错误
Unable to acess the jarfile
Unable to access jarfile selenium-server-standalone-2.21.0.jar
无论是启动中心还是节点都有可能产生这个错误。这意味着 java 无法找到 selenium-server jar 包。你需要从 selenium-server-XXXX.jar 文件存放在目录运行命令或者指定 jar 包的完整路径。