SeleniumHQ/docker-seleniumが公開されていたので使ってみた

リリースしたもの

環境

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
$ docker version
Client:
Version: 18.05.0-ce
API version: 1.37
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:12:05 2018
OS/Arch: darwin/amd64
Experimental: true
Orchestrator: kubernetes

Server:
Engine:
Version: 18.05.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.10.1
Git commit: f150324
Built: Wed May 9 22:20:16 2018
OS/Arch: linux/amd64
Experimental: true
Kubernetes:
Version: v1.10.3
StackAPI: v1beta1

$ docker-compose version
docker-compose version 1.21.2, build a133471
docker-py version: 3.3.0
CPython version: 3.6.4
OpenSSL version: OpenSSL 1.0.2o 27 Mar 2018
$ pip freeze
robotframework==3.0.4
robotframework-seleniumlibrary==3.1.1
selenium==3.13.0

# chromedriver --version
ChromeDriver 2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7)

Dockerfile [master]

一昔前に、ブラウザやらXvfbやら入れて構築していた時に比べると、とてもシンプルになりました

1
2
3
4
5
6
7
8
9
10
11
FROM python:3

RUN apt-get update && apt-get install -y vim git unzip libnss3 libgconf2-4
RUN pip install selenium robotframework robotframework-seleniumlibrary

RUN wget "https://chromedriver.storage.googleapis.com/2.40/chromedriver_linux64.zip" -O /usr/local/bin/chromedriver_linux64.zip && \
cd /usr/local/bin/ && unzip chromedriver_linux64.zip

# RUN mkdir -p /tests/results
# COPY ./tests /tests
WORKDIR /tests

docker-compose.yml [master]

SeleniumHQ/docker-seleniumにあるコンテナ(sele-hub,sele-chrome,sele-firefox)定義と一緒に、Robotframeworkを実行するコンテナ(4shamo)を定義します

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
version: "3"

services:
sele-hub:
container_name: sele-hub
hostname: sele-hub
image: selenium/hub:3.12.0-cobalt
networks:
sele-net:
ipv4_address: 172.19.0.2
ports:
- 4444:4444

sele-chrome:
container_name: sele-chrome
hostname: sele-chrome
image: selenium/node-chrome-debug:3.12.0-cobalt
networks:
sele-net:
ipv4_address: 172.19.0.3
ports:
- 15900:5900
depends_on:
- sele-hub
environment:
- no_proxy=localhost
- HUB_PORT_4444_TCP_ADDR=sele-hub
- HUB_PORT_4444_TCP_PORT=4444
- HUB_ENV_no_proxy=localhost

sele-firefox:
container_name: sele-firefox
hostname: sele-firefox
image: selenium/node-firefox-debug:3.12.0-cobalt
networks:
sele-net:
ipv4_address: 172.19.0.4
ports:
- 15901:5900
depends_on:
- sele-hub
environment:
- no_proxy=localhost
- HUB_PORT_4444_TCP_ADDR=sele-hub
- HUB_PORT_4444_TCP_PORT=4444
- HUB_ENV_no_proxy=localhost

4shamo:
container_name: 4shamo
hostname: 4shamo
build: .
# image: nobiki/4shamo:latest
networks:
sele-net:
ipv4_address: 172.19.0.10
command: "/bootstrap.sh"
# command: /bin/bash
volumes:
- ${PWD}/tests/:/tests
- ${PWD}/bootstrap.sh:/bootstrap.sh
depends_on:
- sele-hub
tty: true


networks:
sele-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.19.0.0/16

bootstrap.sh [master]

コンテナ起動時に実行する内容を記述

1
2
3
4
#!/bin/bash

robot -A robot.args entry.robot
# robot -A robot.args -t このテスト -t あのテスト client.robot

実行

1
2
3
4
5
$ docker-compose up -d
Creating sele-hub ... done
Creating sele-firefox ... done
Creating sele-chrome ... done
Creating 4shamo ... done

懸念

depends_onオプションをつけていますが、hubが起動してから少し時間をおかないと、Connection refusedになります(hubの起動が完了していないため)

実際に使用する際はnode側だけ実行すればいいので問題ありませんが、気になる人はbootstrap.shでsleepなんかを仕込んでやるといいかも