Docker で Selenium Grid による並列実行環境を構築
前提
- Docker インストール&設定済み
Docker 超入門 〜 Boot2Docker 導入から HelloWorld まで〜
Docker Image 取得
View Public Profile | Docker Hub Registry - Repositories of Docker Images
から selenium/hub と selenium/node-chrome-debug をとってくる
$ docker run -d -p 4444:4444 --name selenium-hub selenium/hub $ docker run -d -v /dev/urandom:/dev/random -P --link selenium-hub:hub selenium/node-chrome-debug:
でHub と Node を起動
boot2docker ip確認
$ boot2docker ip
コンテナ確認
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5cb8aa82960 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 25 hours ago Up 5 seconds 0.0.0.0:49154->5900/tcp gloomy_sinoussi d495bfc3d1dd selenium/hub:latest "/opt/bin/entry_poin 25 hours ago Up 4 minutes 0.0.0.0:4444->4444/tcp selenium-hub
ポート確認
$ docker port e5cb8aa82960 5900/tcp -> 0.0.0.0:49154
下記へアクセス
http://<boot2docker-ip>:4444/grid/console
VNCへ接続する
Macでのboot2docker は VNCクライアントが既に入っているので SafariかAlfredで下記にアクセス
vnc://<boot2docker-ip>:49154
パスワードが聞かれるので、デフォルトは「secret」を入力すると、こんな画面がでてくる。
selenium grid上で seleniium webdriver を動かしてみる
サンプルスクリプトはこんな感じ
# google.rb require 'rubygems' require 'selenium-webdriver' driver = Selenium::WebDriver.for :remote, url: 'http://<boot2docker-ip>:4444/wd/hub', desired_capabilities: :chrome driver.navigate.to "http://google.com" driver.save_screenchot "google.png" driver.quit
スクショとれてますよ〜。感動!!
ノード数を増やして、並列実行やってみる
figを使ってみる。
brew install fig
yaml形式です。
# docker-compose.yml hub: image: selenium/hub ports: - "4444" node: image: selenium/node-chrome-debug ports: - "5900" links: - hub
起動してみる
$ fig up -d
Creating seleniumgrid_hub_1...
Creating seleniumgrid_node_1...
ノード数を増やしてみる
$ fig scale node=10 Creating seleniumgrid_node_2... Creating seleniumgrid_node_3... Creating seleniumgrid_node_4... Creating seleniumgrid_node_5... Creating seleniumgrid_node_6... Creating seleniumgrid_node_7... Creating seleniumgrid_node_8... Creating seleniumgrid_node_9... Creating seleniumgrid_node_10... Starting seleniumgrid_node_2... Starting seleniumgrid_node_3... Starting seleniumgrid_node_4... Starting seleniumgrid_node_5... Starting seleniumgrid_node_6... Starting seleniumgrid_node_7... Starting seleniumgrid_node_8... Starting seleniumgrid_node_9... Starting seleniumgrid_node_10...
増えてる。増えてる。 コンテナ確認
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 848cfbfea229 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 27 seconds ago Up 21 seconds 0.0.0.0:49165->5900/tcp seleniumgrid_node_10 aefd6e22fc38 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 22 seconds 0.0.0.0:49164->5900/tcp seleniumgrid_node_9 31a5deab92f6 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 23 seconds 0.0.0.0:49163->5900/tcp seleniumgrid_node_8 9a1f2b57d2fd selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 24 seconds 0.0.0.0:49162->5900/tcp seleniumgrid_node_7 e8248a7f54f1 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 25 seconds 0.0.0.0:49161->5900/tcp seleniumgrid_node_6 3ffe25779686 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 25 seconds 0.0.0.0:49160->5900/tcp seleniumgrid_node_5 112ab5748233 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 26 seconds 0.0.0.0:49158->5900/tcp seleniumgrid_node_3 1cd0614af078 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 27 seconds 0.0.0.0:49157->5900/tcp seleniumgrid_node_2 f205fe9716a0 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 28 seconds ago Up 26 seconds 0.0.0.0:49159->5900/tcp seleniumgrid_node_4 04690c0dcea8 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 56 seconds ago Up 55 seconds 0.0.0.0:49156->5900/tcp seleniumgrid_node_1 64cecc30b7b2 selenium/hub:latest "/opt/bin/entry_poin 57 seconds ago Up 56 seconds 0.0.0.0:49155->4444/tcp seleniumgrid_hub_1 e5cb8aa82960 selenium/node-chrome-debug:latest "/opt/bin/entry_poin 26 hours ago Up About an hour 0.0.0.0:49154->5900/tcp gloomy_sinoussi d495bfc3d1dd selenium/hub:latest "/opt/bin/entry_poin 26 hours ago Up About an hour 0.0.0.0:4444->4444/tcp selenium-hub
増えてる。増えてる。
$ docker port seleniumgrid_hub_1 4444/tcp -> 0.0.0.0:49155
http://<boot2docker-ip>:49155/grid/console
へアクセス。 増えてる。増えてる。
並列実行する際は
driver = Selenium::WebDriver.for :remote, url: 'http://<boot2docker-ip>:49155/wd/hub', desired_capabilities: :chrome
にして実行すれば流れる感じっすね。
Special Thanks
Docker Hub の Selenium Hub/Node イメージ + fig で Selenium Grid 構築してみた - Thanks Driven Life
Docker で Selenium Grid の環境構築をもっと楽に! | Cybozu Inside Out | サイボウズエンジニアのブログ
View Public Profile | Docker Hub Registry - Repositories of Docker Images