Run test cases on selenium grid on docker

In this artical we are going to cover these 2 scenarios:

1. Run test cases on selenium-hub-nodes with/without debug mode inside of docker, and hub will assign test cases automaticlly.

2. Run test cases manually on multi selenium-standalone containers with/without debug inside of docker, you will need to assign test cases to different selenium-standalone containers.

 

Prerequisite:

  • I'm on Windows 10 Enterprise version, which support Docker.
  • Use docker-compose commands to launch docker images, base on yml file. 

To launch docker images, just run the following command in powsershell, it will read the config from the "docker-compose.yml" file from the same folder path you from the command at. 

docker-compose up -d

To terminate docker images, just run the following command in powershell:

docker-compose down
  • User VNC: need to download VNC binary and put it in a folder you want, then add the folder path to your OS environment path.

If I rename the vnc binary file to vnc.exe, just run the following command in powsershell to open a vnc window that connected to a container (Given all ports and containers are available).

vnc localhost:5902

Scenario 1:

In this scenario, we will lanuch 1 selenium hub and 2 selenium nodes(both chrome nodes).

In the test code, we just use the URL of hub as RemoteWebDriver,  and the hub will assign test cases automaticlly.

This is the docker-compose.yml file: use "docker-compose up -d" to read this yml file and launch containser.

# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: "3"
services:
  hub:
    image: selenium/hub:3.141.59
    container_name: hub
    ports:
      - "4444:4444"
    healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:4444"]
        interval: 2s
        timeout: 10s
        retries: 5
  chrome1:
    image: selenium/node-chrome:3.141.59
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub
      - VNC_NO_PASSWORD=1
    ports:
      - 4445:4444
  chrome2:
    image: selenium/node-chrome:3.141.59
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub
      - VNC_NO_PASSWORD=1
    ports:
      - 4446:4444

This is a simple test cases shows you which remote url to use:

    class Test
    {
        [Test]
        public void Test1()
        {
            var dc = new ChromeOptions();
            var driver = new RemoteWebDriver(new Uri(" http://localhost:4444/wd/hub"), dc);

            driver.Navigate().GoToUrl("http://www.google.com");

            Thread.Sleep(TimeSpan.FromSeconds(10));

            driver.Quit();
        }
    }

 

Scenario 2:

In this scenario, we will lanuch 3 selenium standalone containers, you can manually assign test cases to any of the 3.

This is the docker-compose.yml file: use "docker-compose up -d" to read this yml file and launch containser.

In this yml file, we use standalone-chrome-debug image so that we can use vnc to connect to the container and see what's going on there. Since the VNC default port is 5900 and we map it out to 5901, 5901 and 5903 port for 3 different containers, we just need to run "vnc localhost 5901" to open a vnc viewer window to see what's going on inside of containers.

# To execute this docker-compose yml file use `docker-compose -f <file_name> up`
# Add the `-d` flag at the end for detached execution
version: "3"
services:
  chrome1:
    image: selenium/standalone-chrome-debug:3.141.59
    volumes:
      - /dev/shm:/dev/shm
    environment:
      - VNC_NO_PASSWORD=1
    ports:
      - 4445:4444
      - 5901:5900
  chrome2:
    image: selenium/standalone-chrome-debug:3.141.59
    volumes:
      - /dev/shm:/dev/shm
    environment:
      - VNC_NO_PASSWORD=1
    ports:
      - 4446:4444
      - 5902:5900
  chrome3:
    image: selenium/standalone-chrome-debug:3.141.59
    volumes:
      - /dev/shm:/dev/shm
    environment:
      - VNC_NO_PASSWORD=1
    ports:
      - 4447:4444
      - 5903:5900

After executing above, if you run "docker ps" command, you will see 3 containers are launched:

 

This is a simple test cases shows you run 3 different test cases on 3 different selenium containers.

We can see that 3 different test cases are running on 4445, 4446 and 4447 3 different port, which stands for 3 different containers.

Now these 3 test cases are running one by one, but we can refactor the driver init later to make them running in parallel. 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章