简体   繁体   English

Selenium 网格集线器和节点来自 Java 代码

[英]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.截至目前,我正在使用以下命令在远程机器上手动启动集线器和节点。

  • java -jar selenium-server-standalone-3.141.59.jar -role hub java -jar selenium-server-standalone-3.141.59.jar -角色集线器
  • java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -角色节点 -集线器 Z80791B3AE7002FAA88C246876

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 :脚步 :

  1. Install docker安装码头工人
  2. Save this selenium file保存这个硒文件
  3. open command promt and type : docker-compose -f docker-compose-v3.yml up .打开命令提示符并输入: docker -compose -f docker-compose-v3.yml up that's it.就是这样。
  4. if you want down, docker-compose -f docker-compose-v3.yml down .如果你想要下来, docker-compose -f docker-compose-v3.yml down

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

  1. Install docker on your remote machine, the following link details installation for various platforms: docs.docker.com/engine/install/在远程机器上安装docker ,以下链接详细介绍了各种平台的安装: docs.docker.com/engine/install/
  2. Install docker-compose on your remote machine, the following link details installation for various platforms: docs.docker.com/compose/install在远程机器上安装 docker-compose,以下链接详细介绍了各种平台的安装: docs.docker.com/compose/install
  3. Create a docker-compose.yml file with your desired configuration, the following link provides info based on what type of gris you want to set up: docker-selenium/tree/trunk#docker-compose使用您想要的配置创建一个 docker-compose.yml 文件,以下链接提供了基于您要设置的 gris 类型的信息: docker-selenium/tree/trunk#docker-compose

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:根据您运行自动化测试套件的方式,您可以通过多种方式启动、运行和关闭网格:

  1. Manually with the command noted above使用上面提到的命令手动
  2. Scheduled job (Windows scheduled service, Linux crontab job)定时作业(Windows 定时服务、Linux crontab 作业)
  3. Run it as part of a Jenkins stage作为 Jenkins 阶段的一部分运行它

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM