Please enable Javascript to view the contents

如何打包一个 Robot Framework 的 Docker 镜像

 ·  ☕ 3 分钟

为了方便 CI 集成 UI 自动化测试,需要将 Robot Framework 运行环境打包为 Docker 镜像。本篇主要内容是一些与打包过程相关的配置和脚本。

1. 打包目录结构

1
2
3
4
5
6
tree
.
├── docker-compose.yml
├── Dockerfile
├── google-chrome.repo
├── requirements_base.txt

1.1 Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FROM centos:7

ADD ./google-chrome.repo /etc/yum.repos.d/google-chrome.repo

RUN yum -y install epel-release

RUN yum -y install wget unzip python-pip google-chrome-stable

RUN google-chrome -version

RUN wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip

RUN unzip chromedriver_linux64.zip

RUN mv chromedriver /usr/bin/

RUN  mkdir /data

ADD ./requirements_base.txt /data/requirements_base.txt

RUN cd  /data

RUN chmod 777 /data

RUN  pip install -r /data/requirements_base.txt

RUN yum -y install cjkuni-ukai-fonts cjkuni-uming-fonts wqy-zenhei-fonts

WORKDIR /data

1.2 google-chrome.repo

1
2
3
4
5
6
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

1.3 requirements_base.txt

1
2
3
4
5
6
7
8
robotframework==3.0.4
robotframework-archivelibrary==0.4.0
robotframework-ftplibrary==1.6
robotframework-ride==1.5.2.1
robotframework-selenium2library==3.0.0
robotframework-seleniumlibrary==3.1.1
robotframework-sshlibrary==3.1.0
robotframework-excellibrary==0.0.2

1.4 docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: '2'

services:

    robot-framework:
      build: ./
      image: robot-framework:1.2.0
      volumes:
        - ./framework:/data
      command:  /bin/bash -c  '/data/start.sh'

2. 生成镜像并导出

为了方便生成镜像,并进行测试,这里使用 docker-compose 工具。docker-compose 提供了对 docker 镜像进行编排的功能。

编译生成镜像(由于已经编译过一次,docker 直接使用了缓存)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
docker-compose build
Building robot-framework
Step 1/15 : FROM centos:7
 ---> 5182e96772bf
Step 2/15 : ADD ./google-chrome.repo /etc/yum.repos.d/google-chrome.repo
 ---> Using cache
 ---> 170c52cb6d03
Step 3/15 : RUN yum -y install epel-release
 ---> Using cache
 ---> 16e06b65b06b
Step 4/15 : RUN yum -y install wget unzip python-pip google-chrome-stable
 ---> Using cache
 ---> fa1d529a3c79
Step 5/15 : RUN google-chrome -version
 ---> Using cache
 ---> 85d30717b2cc
Step 6/15 : RUN wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
 ---> Using cache
 ---> 478e06633003
Step 7/15 : RUN unzip chromedriver_linux64.zip
 ---> Using cache
 ---> f349532ce26a
Step 8/15 : RUN mv chromedriver /usr/bin/
 ---> Using cache
 ---> 0cda97e8015d
Step 9/15 : RUN  mkdir /data
 ---> Using cache
 ---> 1140837158be
Step 10/15 : ADD ./requirements_base.txt /data/requirements_base.txt
 ---> Using cache
 ---> 453287832315
Step 11/15 : RUN cd  /data
 ---> Using cache
 ---> 2c37da1e6d90
Step 12/15 : RUN chmod 777 /data
 ---> Using cache
 ---> 39db1f669549
Step 13/15 : RUN  pip install -r /data/requirements_base.txt
 ---> Using cache
 ---> 1d91f1cab3f6
Step 14/15 : RUN yum -y install cjkuni-ukai-fonts cjkuni-uming-fonts wqy-zenhei-fonts
 ---> Using cache
 ---> 45b4ecae1112
Step 15/15 : WORKDIR /data
 ---> Using cache
 ---> 609dde1f878e
Successfully built 609dde1f878e
Successfully tagged robot-framework:1.2.0

查看镜像列表

1
2
3
4
docker  image
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
robot-framework           1.2.0               609dde1f878e        3 weeks ago         962MB
centos                    7                   5182e96772bf        6 weeks ago         200MB

导出镜像

1
docker save -o ./robot-framework_V1.2.0 robot-framework:1.2.0

将镜像拷贝到其他机器,还可以导入镜像

1
docker load -i robot-framework_V1.2.0

3. 测试

需要准备一个 Robot Framework 的 Demo。

tree framework/
framework/
|---cases
|------ 测试用例
|---process
|------ 测试流程
|---keywords (存放关键字)
|------common (通用关键字)
|---resources(附件资源文件夹,放图片、表格、文件等)
|---global.robot(存放一些必须的全局变量)
|---locators (存放页面上一些固定的元素,进行统一管理)
|---lib (自己写的一些库 Python 库文件)
|---requirements.txt 依赖库文件
|---report 测试输出文件夹
|---start.sh 启动执行测试用例命令

在项目的根目录下,存在 start.sh 脚本,用于启动测试执行。

1
2
cat start.sh
pybot  --outputdir  report .

然后,继续使用 docker-compose 工具,创建容器,运行测试用例。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
docker-compose up
Starting robot-framework-compose_robot-framework_1 ... done
Attaching to robot-framework-compose_robot-framework_1
robot-framework_1  | ==============================================================================
robot-framework_1  | Data
robot-framework_1  | ==============================================================================
robot-framework_1  | Data.Cases
robot-framework_1  | ==============================================================================
robot-framework_1  | Data.Cases.Home
robot-framework_1  | ==============================================================================
robot-framework_1  | ????                                                              | PASS |
robot-framework_1  | ------------------------------------------------------------------------------
robot-framework_1  | ????                                                              | PASS |
robot-framework_1  | ------------------------------------------------------------------------------
robot-framework_1  | ????                                                              | PASS |
robot-framework_1  | ------------------------------------------------------------------------------
robot-framework_1  | Data.Cases.Home                                                       | PASS |
robot-framework_1  | 3 critical tests, 3 passed, 0 failed
robot-framework_1  | 3 tests total, 3 passed, 0 failed
robot-framework_1  | ==============================================================================
robot-framework_1  | Data.Cases                                                            | PASS |
robot-framework_1  | 3 critical tests, 3 passed, 0 failed
robot-framework_1  | 3 tests total, 3 passed, 0 failed
robot-framework_1  | ==============================================================================
robot-framework_1  | Data                                                                  | PASS |
robot-framework_1  | 3 critical tests, 3 passed, 0 failed
robot-framework_1  | 3 tests total, 3 passed, 0 failed
robot-framework_1  | ==============================================================================
robot-framework_1  | Output:  /data/report/output.xml
robot-framework_1  | Log:     /data/report/log.html
robot-framework_1  | Report:  /data/report/report.html
robot-framework-compose_robot-framework_1 exited with code 0

最终在本地文件夹中,可以看到执行完测试用例生成的报告。

除此,你还可以直接使用线上的镜像,跳过编译镜像的过程:

docker-compose.yml

1
2
3
4
5
6
7
8
9
version: '2'

services:

    robot-framework:
      image: shaowenchen/docker-robotframework
      volumes:
        - ./framework:/data
      command:  /bin/bash -c  '/data/start.sh'

微信公众号
作者
微信公众号