Robot FrameworkにはSelenium2Library
という、Seleniumと連携するためのライブラリがあります。
- robotframework/Selenium2Library: Web testing library for Robot Framework | GitHub
- Selenium2Library
- Selenium2Library ドキュメント和訳 — Selenium2Library ドキュメント和訳 1.0 ドキュメント
サポートされているブラウザは、ChromeやFirefox・PhantomJSなど、ひと通り揃っています。
http://robotframework.org/Selenium2Library/Selenium2Library.html#Open%20Browser
そんな中、
- Headless ChromeがChrome59から搭載される
- PhantomJSのメンテナが引退するという話があった
を目にしました。
そのため、Robot FrameworkでHeadless Chromeが使えるのかを試してみたくなりました。
Python + SeleniumでHeadless Chromeを使う記事がありましたので、今回はこれを元に書いてみようと思います。
SeleniumからHeadless Chromeを使ってみた - Qiita
2017/9/2追記
Selenium2Libraryですが、バージョン3からは SeleniumLibrary
へと名称が変更されています。詳しくはこちらに書きました。
RobotFrameworkのSelenium2Libraryの名前が、SeleniumLibraryへと変更されてた - メモ的な思考的な
なお、本文はSelenium2Libraryのままにしてあります。
2017/9/2追記ここまで
目次
環境
- Mac OS X 10.11.6
- Python 3.6.1
- Google Chrome 59.0.3071.115 (stable)
- chromedriver 2.30 (stable)
- RobotFramework 3.0.2
- Selenium2Library 1.8.1
- pipでインストール可能
pip install robotframework-selenium2library
ソースコード
上記のQiita記事をRobotFramework用に書き直しただけなので、ソースコードだけ記載します。
なお、stable Chrome 59を使っているせいか、スクリーンショットは1x1の画像のままです。Qiita記事によるとChrome 61では解消されているようです。
また、GitHubにも上げてあります。selenium2_chrome_headless_sample
ディレクトリ以下が今回のコードです。
thinkAmi-sandbox/RobotFramework-sample
2017/8/9 追記ここから
Chrome 60 + chromedriver 2.31 の場合、スクリーンショットが正しく撮影されるようになっています。
2017/8/9 追記おわり
*** Settings *** # Libraryは大文字小文字の区別があるようで、libraryとしてしまうとIDEが認識しない Library Selenium2Library *** Keywords *** GoogleでPythonを検索してスクリーンショットを撮り、結果を出力する # 以下のコードをRobot Framework風にした # http://qiita.com/orangain/items/db4594113c04e8801aad # 以下を参考に、Chromeのオプションを追加して、Chromeを起動する # https://groups.google.com/d/msg/robotframework-users/gPsiVaMo19A/cBRH7mr2BAAJ ${options} = evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys call method ${options} add_argument --headless create webdriver Chrome chrome_options=${options} # 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 press key name=q \\13 # Ajax遷移のため、適当に2秒待つ sleep 2sec # タイトルにPythonが含まれていることを確認する ${result_title} = get title should contain ${result_title} Python # スクリーンショットを撮る # stableなChrome59だと、1x1の画像しか撮れない... capture page screenshot filename=result_google_python.png # ログを見やすくするために改行を入れる log to console ${SPACE} # 検索結果を表示する # ForでElementを回したかったことから、WebElementを取得し、そのAPIを利用する # http://robotframework.org/Selenium2Library/Selenium2Library.html#Get%20Webelements @{web_elements} = get webelements css=h3 > a :for ${web_element} in @{web_elements} \ ${text} = get text ${web_element} \ log to console ${text} # 以下を参考に、WebElementからattribute(href)を取得 # https://groups.google.com/d/msg/robotframework-users/xx3KYxpDu_w/0hyulqKPKQAJ \ ${href} = call method ${web_element} get_attribute href \ log to console ${href} # ブラウザを終了する close browser *** TestCases *** GoogleでPythonを検索するテスト GoogleでPythonを検索してスクリーンショットを撮り、結果を出力する
実行結果
コンソールには以下が表示されました。HeadlessのChromeでも動作するようでした。
$ robot selenium_google.robot ============================================================================== Selenium Google ============================================================================== 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基礎講座(1 Pythonとは) - Qiita http://qiita.com/Usek/items/ff4d87745dfc5d9b85a4 【入門者必見】Pythonとは?言語の特徴やシェア、仕事市場を徹底解説 | 侍 ... http://www.sejuku.net/blog/7720 Python入門 http://www.tohoho-web.com/python/ Pythonに咬まれるな : 注意すべきセキュリティリスクのリスト | プログラミング ... http://postd.cc/a-bite-of-python/ Download Python | Python.org https://www.python.org/downloads/ 初心者でもほぼ無料で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://d.hatena.ne.jp/keyword/Python GoogleでPythonを検索するテスト | PASS | ------------------------------------------------------------------------------ Selenium Google | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed