[英]Selenium Grid Hub and Node from Java Code
I'm using Selenium Hub and Node setup for my automation suite.我正在为我的自动化套件使用 Selenium 集线器和节点设置。 As of now I am starting hub and node manually on remote machine with below commands.
截至目前,我正在使用以下命令在远程机器上手动启动集线器和节点。
I want to remove this manual process.我想删除这个手动过程。 Is it possible to start and stop hub and register node with java code?
是否可以使用 java 代码启动和停止集线器和注册节点?
Yes.是的。 please use docker, it is very easy to setup.
请使用docker,它很容易设置。
Steps :脚步 :
this will internally create hub and node and attach the node to the hub.这将在内部创建集线器和节点并将节点附加到集线器。 you can simply start using the node by this "http://localhost:4444/wd/hub"
你可以简单地通过这个“http://localhost:4444/wd/hub”开始使用节点
You can run a Selenium Grid on your remote machine using docker & docker-compose您可以使用 docker 和 docker-compose 在远程机器上运行 Selenium Grid
Based on your current config it will be as simple as the following ( Note: The example is using Selenium Grid v4 which also works with a Selenium 3 project, but if you wish you can also substitute Selenium Grid v3 if that is your preference):根据您当前的配置,它会像以下一样简单(注意:该示例使用 Selenium Grid v4,它也适用于 Selenium 3 项目,但如果您愿意,如果您愿意,也可以替换 Selenium Grid v3):
version: "3"
services:
selenium-hub:
image: selenium/hub:4.0.0
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
chrome:
image: selenium/node-chrome:latest
container_name: chrome
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
Lets say you just call the above file docker-compose.yml
, place that in a directory of your choice.假设您只需调用上述文件
docker-compose.yml
,将其放在您选择的目录中。
Advise that you start up the grid in detached mode, so it runs in the background.建议您以分离模式启动网格,以便它在后台运行。 From the directory where the file resides, execute the following command:
docker-compose up -d
从文件所在目录执行以下命令:
docker-compose up -d
When the Selenium Grid is no longer required, execute the following command: docker-compose down
当不再需要 Selenium Grid 时,执行以下命令:
docker-compose down
Based on how you run your automated test suite you can get the grid up and running & taken down in many ways:根据您运行自动化测试套件的方式,您可以通过多种方式启动、运行和关闭网格:
The options are open.选项是开放的。
One more bit of advice, selenium.dev frequently update their images with the latest security patches (circa ~ every 2 weeks).还有一点建议,selenium.dev 经常用最新的安全补丁更新他们的镜像(大约每 2 周一次)。 To ensure you have the latest version of the image where vulnerabilities have been remediated you can use the following command when the grid is not up:
docker-compose pull
.为确保您拥有已修复漏洞的最新版本的映像,您可以在网格未启动时使用以下命令:
docker-compose pull
。
Carry on using your existing configuration to your current setup for execution of tests.继续将现有配置用于当前设置以执行测试。
To access the Selenium Grid v4 console view via your browser, go to: http://<server-ip/name>:4444/ui/index.html#/要通过浏览器访问 Selenium Grid v4 控制台视图,请访问: http://<server-ip/name>:4444/ui/index.html#/
I have a simple solution, use shell script for starting hub and node我有一个简单的解决方案,使用 shell 脚本启动集线器和节点
java -jar selenium-server-standalone-3.141.59.jar -role hub
Save it as startHub.sh将其保存为 startHub.sh
java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register
and save it as startNode.sh并将其保存为 startNode.sh
in java program use在 java 程序中使用
String[] cmd = { "sh", "filepath/startNode.sh"};
p = Runtime.getRuntime().exec(cmd);
and for Node respectively, to view output of node and hub use -log
for both commands like give below和节点分别查看节点和集线器的 output 使用
-log
两个命令,如下所示
java -jar selenium-server-standalone-3.141.59.jar -role hub -log nohup.txt &
& will make script run in background &将使脚本在后台运行
Hope it helps希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.