Docker+Selenium Grid运行UI自动化

简介

使用Selenium Grid可以分布式运行UI自动化测试,可以同时启动多个不同的浏览器,也可以同时启动同一个浏览器的多个session。这里使用Docker Compose来同时启动不同浏览器的容器和Selenium Grid,只需一条命令就把自动化运行环境部署好了。

前提条件

已经在Linux服务器安装好docker,docker compose

编写Docker Compose

在Linux服务器下创建docker-compose.yaml文件
docker-compose.yaml文件内容如下:

# To execute this docker-compose yml file use `docker-compose -f docker-compose.yaml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose.yaml down`
version: "3"
services:
  chrome:
    image: selenium/node-chrome:latest
    container_name: selenium-chrome
    shm_size: 2gb
    ports:
      - "7900:7900"
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  edge:
    image: selenium/node-edge:latest
    container_name: selenium-edge
    shm_size: 2gb
    ports:
      - "7901:7900"
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:latest
    container_name: selenium-firefox
    shm_size: 2gb
    ports:
      - "7902:7900"
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:latest
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

注意这里面各个浏览器映射的端口是不一样的,分别是:7900,7901,7902,后面查看自动化运行时要进入对应端口号的VNC。

启动容器

在docker-compose.yaml目录下运行如下docker compose命令,从而后台启动所有容器:

docker compose up -d

示例代码

"""
@File :test_baidu.py
@Auth : 灵枢
@Time : 12/29/2022
@Desc :
"""
from selenium import webdriver
from selenium.webdriver import FirefoxOptions, ChromeOptions, EdgeOptions
from selenium.webdriver.common.by import By
import time


def test_baidu_chrome():
    options = FirefoxOptions()
    remote_url = "http://:4444/wd/hub"
    chrome_driver = webdriver.Remote(remote_url, options=options)
    chrome_driver.maximize_window()
    chrome_driver.get("https://www.baidu.com")
    chrome_driver.find_element(By.ID, "kw").send_keys("galen2016")
    chrome_driver.find_element(By.ID, "su").click()
    time.sleep(3)
    chrome_driver.quit()


def test_baidu_in_Edge():
    options = EdgeOptions()
    remote_url = "http://:4444/wd/hub"
    edge_driver = webdriver.Remote(remote_url, options=options)
    edge_driver.maximize_window()
    edge_driver.get("https://www.baidu.com")
    edge_driver.find_element(By.ID, "kw").send_keys("galen2016")
    edge_driver.find_element(By.ID, "su").click()
    time.sleep(3)
    edge_driver.quit()


if __name__ == '__main__':
    test_baidu_chrome()
    test_baidu_in_Edge()

查看Selenium Grid

在浏览器输入http://:< the Linux server IP >:4444/ui,进入selenium grid, 查看浏览器的版本,启动了多少个session。
Docker+Selenium Grid运行UI自动化_第1张图片

在VNC查看自动化的运行

比如要看Edge浏览器的运行情况,在docker-compose.yaml里对它设置的端口是7902,然后在浏览器输入http://< the Linux server IP >:7902/ 进入VNC,然后输入VNC默认的密码:secret

这时运行自动化代码,就可以在VNC看到正在运行的自动化了:

你可能感兴趣的:(自动化测试,Docker,selenium,docker,selenium,自动化)