Home > Software design >  Selenium Grid Hub and Node from Java Code
Selenium Grid Hub and Node from Java Code

Time:11-12

I'm using Selenium Hub and Node setup for my automation suite. 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 -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register

I want to remove this manual process. Is it possible to start and stop hub and register node with java code?

CodePudding user response:

Yes. please use docker, it is very easy to setup.

Steps :

  1. Install docker
  2. Save this selenium file
  3. open command promt and type : docker-compose -f docker-compose-v3.yml up. that's it.
  4. if you want 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"

CodePudding user response:

You can run a Selenium Grid on your remote machine using docker & docker-compose

  1. Install docker on your remote machine, the following link details installation for various platforms: 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
  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

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):

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.

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

When the Selenium Grid is no longer required, execute the following command: 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)
  3. Run it as part of a Jenkins stage

The options are open.

One more bit of advice, selenium.dev frequently update their images with the latest security patches (circa ~ every 2 weeks). 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.

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#/

  • Related