This page looks best with JavaScript enabled

How to Package a Robot Framework Docker Image

 ·  ☕ 2 min read

To make it easier for CI to integrate UI automation tests, the Robot Framework runtime needs to be packaged as a Docker image. This post covers the configuration and scripts involved in the packaging process.

1. Packaging Directory Structure

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. Building and Exporting the Image

To make building the image and testing it easier, docker-compose is used here. docker-compose provides the ability to orchestrate Docker images.

Build the image (since it had already been built once, Docker used the cache directly)

 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

List the images

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

Export the image

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

After copying the image to another machine, you can also import it

1
docker load -i robot-framework_V1.2.0

3. Testing

You need to prepare a Robot Framework demo.

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

In the project root there is a start.sh script, used to launch the test run.

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

Then, again using docker-compose, create a container and run the test cases.

 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

Finally, in the local folder you can see the report generated after the test cases ran.

Besides that, you can also use the online image directly and skip the image build process:

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'

微信公众号
WRITTEN BY
微信公众号