今まで、
など、Mac上で動作するものに対してテストコードを書いてきました。
そんな中、VMのWindowsのInternet Explorerを使ってテストする機会がありました。
そこで、
- MacにRobotFrameworkのテストコードを置く
- Macから指示を出して、VMのWindowsのInternet Explorerでテストを実行する
- テスト結果ログやスクリーンショットはMacに保存する
を行うよう、SeleniumGrid + RobotFramework + SeleniumLibraryを使ってテストしてみたため、その時のメモを残します。
なお、Selenium Gridについては以下が詳しいです。
» Selenium Gridで複数の実機ブラウザで自動テスト TECHSCORE BLOG
目次
- 環境
- Mac側のSelenium Gridの環境構築
- VMのWindows10側のSelenium Gridの環境構築
- Robot Frameworkのテストコードを作成
- テストの実行
- 参考
- ソースコード
環境
今回は
という構成にしました。
なお、Selenium Grid自体は、HubとNodeを一つの端末にしても動作します。ターミナルが3つ必要になるくらいです。(Hub用、Node用、テスト実行用)
Mac
- Mac OS X 10.11.6
- Python 3.6.2
- RobotFramework 3.0.2
- SeleniumLibrary 3.0.0b1
- PythonライブラリのSelenium 3.3.1
- Java 1.8.0_144
- VMware Fusion 8.5.8
- IPアドレス 192.168.10.103
Windows
- Windows10
- Java 1.8.0_144
- Internet Explorer 11
- IPアドレス 192.168.10.107
- VMホストとはブリッジ接続
Mac側のSelenium Gridの環境構築
Selenium Grid用のjarファイルのダウンロード
後日AppiumでもSelenium Grid環境を構築するため、Seleniumは3.3.1を使います (現時点の最新は3.5.3)。
PythonライブラリのバージョンとSelenium Gridのバージョンが異なる場合エラーが起きることがあるため、同じバージョンのものをダウンロードします。
今回は過去のバージョンのため、ダウンロードページのprevious releasesより selenium-server-standalone-3.3.1.jar
をダウンロードします。
ダウンロードしたzipを展開し、出てきたjarファイルをテスト用ディレクトリに置きます。
Selenium Grid Host用の設定ファイルを作成
コマンドラインに設定内容を記述してもよいのですが、今回は設定ファイルを使います。
今回使用するMacにはVM用のネットワークアダプタなど、IPが振られているネットワークアダプタが複数あります。何も設定しないと、LANのネットワークアダプタが使われないことがあります。その場合、Selenium Grid NodeからHubへ接続できず動作しません。
そのため、今回の設定ファイルには、HubのIPアドレスを記載します。
hub_config.json
{ "host": "192.168.10.103" }
Selenium Grid Hubの起動
Macのターミナルを開き、jarファイルのあるディレクトリで以下を実行します。
$ java -jar selenium-server-standalone-3.3.1.jar -role hub -hubConfig hub_config.json 21:54:58.285 INFO - Selenium build info: version: '3.3.1', revision: '5234b32' ... 21:54:59.918 INFO - Nodes should register to http://192.168.10.103:4444/grid/register/ 21:54:59.918 INFO - Selenium Grid hub is up and running
Hubが起動しました。
http://192.168.10.103:4444/grid/console
へアクセスすると、以下のようになりました。
VMwareのゲストOSのIP設定をブリッジへと変更
もし、VMwareの設定画面にネットワークアダプタがない場合は、新規に追加します。
その上で、ネットワークアダプタの設定を Macを共有
から ブリッジされたネットワーキング > 自動検出
へと変更します。
これでMacとゲストOSのWindowsのIPアドレスが別となりました *1。
VMのWindows10側のSelenium Gridの環境構築
jarとIEDriverServerのダウンロードと配置
NodeとなるWindowsにも、Hubと同じバージョンのjarファイルおよびIEDriverServerを任意のディレクトリに用意します。
今回用意したものは
- selenium-server-standalone-3.3.1.jar
- Hubと同じバージョンのもの
- IEDriverServer.exe
- IEDriverServer_Win32_3.3.0.zipを展開したもの
- Windows10のOSは64bitだが、Downloadsページに32bit IEが推奨されていたため、32bit版のものを使った
- Internet Explorer 用 WebDriver (Windows)
を同じディレクトリに入れます。
なお、jarと同じディレクトリに置けば、IEDriverServer.exeには特にPATHを通さなくても動作しました。
Selenium Grid Node用の設定ファイルを作成
Internet Explorerを動作させるための設定ファイルを記載します。
なお、Selenium3からは configuration
というキーを付けるとエラーとなります。
Selenium 3.0.1 Chrome Node configuration - Stack Overflow
また、設定ファイルのデフォルト値は以下に記載されています。 https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultNodeWebDriver.json
今回はその差分を記載します。
node.json
{ "capabilities": [ { "browserName": "internet explorer", "platform": "WINDOWS", "maxInstances": 1, "cleanSession": true , "version": "11", "browser-version": "11", "seleniumProtocol": "WebDriver" } ], "hub": "http://192.168.10.103:4444/grid/register", "host": "192.168.10.107", "port": 5054, "register": true }
Internet Explorerの設定を変更
SeleniumでInternet Explorerを使う場合、いくつかの設定が必要になります。
今回は以下のWikiにあるRequired Configurationに従い、設定を行います。
Required Configuration - InternetExplorerDriver · SeleniumHQ/selenium Wiki
設定内容は
- インターネットオプションのセキュリティタブにある、
保護モードを有効にする
にチェックを入れる - インターネットオプションの詳細設定にある
拡張保護モードを有効にする
の チェックを外す- 32bit OS (いわゆるmodernIEなど)の場合、拡張保護モードが存在しないため、設定は不要
- 32bit の Windows 7 / Windows Server 2008 R2 では拡張保護モードの設定項目がなく、拡張保護機能を対応しません。(IE10 & IE11 : 拡張保護モードの実態 – Japan IE Support Team Blog)
- 32bit OS (いわゆるmodernIEなど)の場合、拡張保護モードが存在しないため、設定は不要
- IEを起動し、ズームを100%にする
- それ以外の場合、エラーで動作しない
- selenium - InternetExplorerDriver Zoom Level Error - Stack Overflow
- Windows10の場合、IEのバージョンは11なので、レジストリに以下を追加する
です。
Selenium Grid Nodeの起動
コマンドプロンプトから起動します。
>java -jar selenium-server-standalone-3.3.1.jar -role node -nodeConfig node.json 22:00:16.200 INFO - Selenium build info: version: '3.3.1', revision: '5234b32' 22:00:17.466 INFO - Registering the node to the hub: http://192.168.10.103:4444/grid/register 22:00:17.497 INFO - The node is registered to the hub and ready to use
Nodeが起動し、Hubに登録されました。
この結果、Macのコンソールには
21:59:07.276 INFO - Registered a node http://192.168.10.107:5054 22:00:13.005 INFO - I/O exception (java.net.SocketException) caught when processing request to {}->http://192.168.10.107:5054: Connection reset 22:00:13.006 INFO - Retrying request to {}->http://192.168.10.107:5054 22:00:17.512 INFO - Registered a node http://192.168.10.107:5054 22:00:17.512 WARN - Cleaning up stale test sessions on the unregistered node http://192.168.10.107:5054
と表示されます。
また、Selenium Grid HubのConsoleをブラウザで開くと
に変わりました。
Robot Frameworkのテストコードを作成
今回もGoogleでPythonを検索するテストコードになります。
以前と異なる点は
- Create Webdriverキーワードで、ブラウザ名を
Remote
にする command_executor
に、Selenium GridのHubの待ち受けURLを指定desired_capabilities
に、Selenium GridでIEを動かすためのDesiredCapabilitiesを指定
となります。
なお、Remoteに対するDesiredCapabilitiesの書き方は以下が参考になりました。 robotframework - Opening Chrome browser in Android device using Robot Framework script and chromedriver? - Stack Overflow
internet_explorer.robot
*** Settings *** # Libraryは大文字小文字の区別があるようで、libraryとしてしまうとIDEが認識しない Library SeleniumLibrary *** Keywords *** GoogleでPythonを検索してスクリーンショットを撮り、結果を出力する ${caps} = create dictionary browserName=internet explorer platform=WINDOWS create webdriver Remote command_executor=http://192.168.10.103:4444/wd/hub desired_capabilities=${caps} # Googleのトップ画面を開く go to https://www.google.co.jp/ # タイトルにGoogleが含まれていることを確認する ${page_title} = get title should contain ${page_title} Google # 検索後を入力して送信する input text name=q Python # Robot FrameworkではEnterキーは\\13になる # https://github.com/robotframework/Selenium2Library/issues/4っz1 press key name=q \\13 # Ajax遷移のため、適当に2秒待つ sleep 2sec # タイトルにPythonが含まれていることを確認する ${result_title} = get title should contain ${result_title} Python # スクリーンショットを撮る capture page screenshot filename=result_google_python.png # ログを見やすくするために改行を入れる log to console ${SPACE} # 検索結果を表示する @{web_elements} = get webelements css=h3 > a :for ${web_element} in @{web_elements} \ ${text} = get text ${web_element} \ log to console ${text} \ ${href} = call method ${web_element} get_attribute href \ log to console ${href} # ブラウザを終了する close browser *** TestCases *** GoogleでPythonを検索するテスト GoogleでPythonを検索してスクリーンショットを撮り、結果を出力する
テストの実行
準備が整ったため、Macのターミナルよりテストを実行します。
$ robot internet_explorer.robot
を実行すると、VMのWindowsでIE11が起動し、GoogleでPythonを検索します。
テストが進むにつれMacのターミナルに
============================== Internet Explorer ============================== GoogleでPythonを検索するテスト Python - ウィキペディア https://ja.wikipedia.org/wiki/Python Pythonとは - python.jp https://www.python.jp/about/ Python チュートリアル — Python 3.6.1 ドキュメント https://docs.python.jp/3/tutorial/index.html 【入門者必見】Pythonとは?言語の特徴やシェア、仕事市場を徹底解説 | 侍 ... http://www.sejuku.net/blog/7720 Pythonとは?何に使えるの?Pythonの特徴や使い道を…|Udemy メディア https://udemy.benesse.co.jp/development/python.html Pythonに関する12300件の投稿 - Qiita https://qiita.com/tags/Python Python基礎講座(1 Pythonとは) - Qiita http://qiita.com/Usek/items/ff4d87745dfc5d9b85a4 初心者でもほぼ無料でPythonを勉強できるコンテンツ10選 - paiza開発日誌 http://paiza.hatenablog.com/entry/2015/04/09/%E5%88%9D%E5%BF%83%E8%80%85%E3%81%A7%E3%82%82%E3%81%BB%E3%81%BC%E7%84%A1%E6%96%99%E3%81%A7Python%E3%82%92%E5%8B%89%E5%BC%B7%E3%81%A7%E3%81%8D%E3%82%8B%E3%82%B3%E3%83%B3%E3%83%86%E3%83%B3%E3%83%8410 Python入門 http://www.tohoho-web.com/python/ Pythonとは - はてなキーワード - はてなダイアリー http://d.hatena.ne.jp/keyword/Python GoogleでPythonを検索するテスト | PASS |
と表示されていきます。
ターミナルにPASSが表示されたところで、VMのWindowsのIE11が閉じます。
また、実行ディレクトリに、IEの画面のスクリーンショットも保存されています。
*画像は一部を切り抜いています。
以上より、
ができました。
参考
- Download Selenium Grid and Setup With Multiple Browsers
- robotframework - Robot framework Parallel test execution on two different machines - Stack Overflow
- RobotFrameworkでOpen Browserキーワードを使う場合
ソースコード
GitHubに上げました。 selenium_grid_sample/internet_explorer.robot
ファイルが今回のテストファイルです。
thinkAmi-sandbox/RobotFramework-sample: Robot Framewrok samples
なお、 hub_config.json
と node.json
も含めています。もし使う場合は、 node.json
をSelenium Grid Nodeに渡し、IPアドレスなどを変更すると良いです。
また、 selenium-server-standalone-3.3.1.jar
と IEDriverServer.exe
は含めていないため、それらはダウンロードしてください。