其他分享
首页 > 其他分享> > 使用docker做UI自动化测试

使用docker做UI自动化测试

作者:互联网

在devops模式下让我们不得不对CICD进行重视,如何在服务容器化的部署方式下引入自动化测试即是关键。

目标:docker+selenium+python完成分布式UI自动化

基本思路:

        ①测试运行的浏览器环境是关键,能支持多线程分布式测试非selenium grid不可,试试开源容器镜像 dosel/zalenium

        ②单独给测试代码做1个容器,使用dockerfile构建成镜像,注意:提前准备私有仓库的基础镜像,基础镜像就包括我们所需的必要依赖

        ③从自动化测试容器里剥离出allure服务,使用开源容器镜像frankescobar/allure-docker-service/ui能更优美的查看allure报告

接下来开工:

1. 在基础镜像上简单编译

构建自动化测试代码的镜像

FROM xxxxxxxxxxx
ADD . autotest
ENV INSTALL_PATH /autotest
WORKDIR $INSTALL_PATH
COPY . .
EXPOSE 8000
RUN pip install -r requirements.txt
CMD ["/bin/bash"]
#CMD python run.py

2. 编写docker-compose.yaml部署浏览器运行环境容器、allure容器、自动化测试容器

# yaml配置
version: '3'
services:
  autotest:
    build: ./
    volumes:
      - ${PWD}/report/allure_result:/autotest/report/allure_reslut
    links:
      - zalenium:zalenium
    depends_on:
      - zalenium
    cap_add:
      - ALL
    entrypoint: >
      /bin/sh -c '
      sleep 6;
      echo "start autotest";
      python run.py;
      '
    container_name: autotest
    stdin_open: true
    tty: true
    privileged: true

  zalenium:
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /tmp/videos:/home/seluser/videos
    ports:
      - 4444:4444
    image:  dosel/zalenium
    container_name: zalenium
    privileged: true
    stdin_open: true
    tty: true
    command:
      - start
  allure:
    image: "frankescobar/allure-docker-service"
    environment:
      CHECK_RESULTS_EVERY_SECONDS: 1
      KEEP_HISTORY: 1
    ports:
      - "5050:5050"
    volumes:
      - ${PWD}/report/allure_result:/app/allure-results
      - ${PWD}/report/allure_html:/app/default-report
    user: root
  allure-ui:
    image: "frankescobar/allure-docker-service-ui"
    environment:
      ALLURE_DOCKER_PUBLIC_API_URL: "http://127.0.0.1:5050"
      ALLURE_DOCKER_PUBLIC_API_URL_PREFIX: ""
    ports:
      - "5252:5252"

3、docker-compose up启动,启动即运行测试代码

........

autotest | E AssertionError: 页面没有找到:验证码登s录 autotest | autotest | base/Basepage.py:107: AssertionError autotest | ------------------------------ Captured log call ------------------------------- autotest | ERROR musen:handle_decorator.py:77 用例--test_verfiycodelogin--描述:验证码登录,错误手机号---断言失败 autotest | DEBUG musen:handle_decorator.py:78 用例信息:{'title': '验证码登录,错误手机号', 'mobile': 'xxxxxxxxx', 'code': 'xxxxxx', 'exp': '验证码登s录'} autotest | INFO musen:handle_decorator.py:79 【预期VS实际】: 页面没有找到:验证码登s录 autotest | testcase/test_login.py ⨯ 100% ██████████ zalenium | 07:14:10.303 [http://172.28.0.7:40001] INFO d.z.e.z.p.DockerSeleniumRemoteProxy - Session bd51363c-7830-44e2-a182-11be08141fa3 completed. Node should shutdown soon... autotest | =========================== short test summary info ============================ autotest | FAILED testcase/test_login.py::TestLogin::test_verfiycodelogin[验证码登录,错误手机号] autotest | autotest | Results (28.11s): autotest | 1 passed autotest | 1 failed autotest | - testcase/test_login.py:24 TestLogin.test_verfiycodelogin[验证码登录,错误手机号] allure_1 | Detecting results changes for PROJECT_ID: default allure_1 | Automatic Execution in Progress for PROJECT_ID: default... allure_1 | Creating history on results directory for PROJECT_ID: default ... allure_1 | Copying history from previous results... allure_1 | Creating executor.json for PROJECT_ID: default allure_1 | Generating report for PROJECT_ID: default autotest exited with code 0

4、查看allure报告----访问localhost:5252

写在最后

还在朝着使用k8s批量执行测试用例学习中~~~

(备注:k8s的视频看起来头大)

 

标签:zalenium,autotest,py,allure,UI,自动化,test,docker
来源: https://www.cnblogs.com/zhayi/p/14281876.html