Docker deploys UI automated testing environment

1. What problems should distributed automated testing solve?

What problems should distributed automated testing solve?
1. Reduce script execution time
2. Reduce resource consumption on a single machine
3. Implement distributed compatibility testing

Key points for implementing distributed automated testing:
1. Control the parallel operation of multiple machines and resource monitoring
2. Control the execution sequence of test cases
3. Collection and merging of test reports

Looking at open source technology, take the most popular selenium\docker to practice the distributed automated testing framework.

selenium grid is one of the three major components of selenium. Its function is distributed test execution. It consists of a hub node and several node agent nodes. The hub is used to manage the registration information and status information of each agent node, and accept The remote client code calls the request, and then forwards the requested command to the agent node for execution.

Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable container, and then publish it to any popular Linux machine or Windows machine. It can also be virtualized. The container is completely Using the sandbox mechanism, there will not be any interface between them.

2. Distributed environment construction

1. Environment preparation

Prepare a Linux server (either a cloud server or a self-built Linux server).
Install docker on a Linux server. I won’t go into details about docker installation. Just follow the commands on the official website. It’s very simple.

2. Create a container

Using container technology, we can quickly build our selenium hub and node environment. In fact, the selenium official website has already made the image for us. We only need to pull it in the docker environment. The following is the image name provided by the selenium official website for us.

Pull hub image
Pull chrome browser image
Pull firefox browser image

sudo docker pull selenium/hub
sudo docker pull selenium/node-chrome
sudo docker pull selenium/node-firefox

3. Start the container

Start the docker of the hub node

-d: running container in the background
-p: 5442-5444: 4442-4444: port number one-to-one mapping, 4444 is web service
5442, 5443: node node and hub node use case communication

docker run --name hub -d -p 5442-5444:4442-4444 selenium/hub

-p: 5902:5900: The function is to access the graphical interface of the browser
vnc is a graphical interface service of Linux, remote desktop service
-e: Environment variable
SE_EVENT_BUS_HOST: Specify the ip address of the main node
SE_NODE_MAX_SESSIONS=20: Start up to 20 sessions (20 browser)

Start the docker of the hub node

sudo docker run --name node -p 5902:5900 -d -e \
SE_EVENT_BUS_HOST=82.156.178.247 -e SE_NODE_MAX_SESSIONS=20 -e \
SE_NODE_OVERRIDE_MAX_SESSIONS=true -e SE_EVENT_BUS_PUBLISH_PORT=5442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=5443 -v /dev/shm:/dev/shm selenium/node-chrome
sudo docker run --name node -p 5903:5900 -d -e \
SE_EVENT_BUS_HOST=82.156.178.247 -e SE_NODE_MAX_SESSIONS=20 -e \
SE_NODE_OVERRIDE_MAX_SESSIONS=true -e SE_EVENT_BUS_PUBLISH_PORT=5442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=5443 -v /dev/shm:/dev/shm selenium/node-firefox

4. Check the running status of the container

CONTAINER ID   IMAGE                  COMMAND                  CREATED       STATUS          PORTS                                                                                                                             NAMES
0da5ef1265e4   selenium/node-chrome   "/opt/bin/entry_poin…"   2 days ago    Up 5 minutes    0.0.0.0:5902->5900/tcp, :::5902->5900/tcp                                                                                         node
40456e0d4363   selenium/hub           "/opt/bin/entry_poin…"   2 days ago    Up 5 minutes    0.0.0.0:5442->4442/tcp, :::5442->4442/tcp, 0.0.0.0:5443->4443/tcp, :::5443->4443/tcp, 0.0.0.0:5444->4444/tcp, :::5444->4444/tcp   hub

5. Check Selenium hub and node startup status

Insert image description here

6. Start VNC Server

Connect to the container IP: PORT we started, the default password is sercet
Insert image description here

Insert image description here

At this point, our automated test running environment has been configured. Next, let’s see how to design our automated test script.

7. Test script

from selenium import webdriver

import time

# 配置信息
from selenium.webdriver.common.by import By

ds = {
    
    
    'platform': 'ANY',  # 平台(操作系统)信息
    'browserName': 'chrome',  # 配置浏览器信息,谷歌浏览器
    'version': '',
    'javascriptEnabled': True

}

driver = webdriver.Remote('http://82.156.178.247:5444', desired_capabilities=ds)

driver.get('http://www.baidu.com')

driver.find_element(By.XPATH, '//*[@id="kw"]').send_keys("中国")
driver.find_element(By.XPATH, '//*[@id="su"]').click()
time.sleep(3)

driver.quit()


Insert image description here

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/132481403