以前、Robot Framework + Selenium2Libraryで、静的HTMLをテストしてみました。
Robot Framework + Selenium2Libraryで、静的HTMLをテストする - メモ的な思考的な
その時、
<img id="img_200" src="/static/image/a.png" alt="あの字" title="画像タイトル">
というHTMLに対し、
page should contain image jquery=img[alt='あの字']
というテストコードを書きました。
しかし、実際にはそのテストコードは静的ファイルが存在するかまではチェックしていないため、
<!--存在する画像ファイル--> <img id="img_200" src="/static/image/200.png" alt="存在します" title="画像タイトル"> <!--存在しない画像ファイル--> <img id="img_404" src="/static/image/404.png" alt="存在しません" title="画像タイトル2">
というHTMLがあり、実際には
となっていた場合でも、
page should contain image id=img_404 element should be visible id=img_404
というテストがパスしてしまいます。
静的ファイルが本当にあるのかを確認する方法を探したところ、stackoverflowに robotframework-requests
を使う方法が書かれていました。内部で requests
を使っているようです。
- selenium webdriver - Check whether image file exists, Robot-Framework, Selenium2Library - Stack Overflow
- RequestsKeywords | robotframework-requests
- bulkan/robotframework-requests: Robot Framework keyword library wrapper for requests
そのため、今回 robotframework-requests
を試してみます。
2017/9/2追記
Selenium2Libraryですが、バージョン3からは SeleniumLibrary
へと名称が変更されています。詳しくはこちらに書きました。
RobotFrameworkのSelenium2Libraryの名前が、SeleniumLibraryへと変更されてた - メモ的な思考的な
なお、本文はSelenium2Libraryのままにしてあります。
2017/9/2追記ここまで
目次
環境
- Mac OS X 10.11.6
- Python 3.6.2
- Google Chrome 60.0.3112.78 (stable)
- ChromeDriver 2.31 (stable)
- RobotFramework 3.0.2
- robotframework-requests 0.4.7
- Selenium2Library 1.8.1dev1
- Selenium 3.4.3
- Bottle 0.12.13
- Webアプリ用
実装
GitHubにあるREADMEのUsageに使い方があるため、それに従って実装してみます。
SettingsにLibraryを追加
今回、Selenium2Library
も使うため、2つをSettingsに追加します。
*** Settings *** Library Selenium2Library Library RequestsLibrary
imgタグのsrc属性から、URLを取得
Get Element Attribute
キーワードを使って、imgタグのsrc属性から対象の静的ファイルのURLを取得します。
Get Element Attribute | Selenium2Library
${src404} = get element attribute id=img_404@src log to console ${src404} # => http://localhost:8084/static/image/404.png
セッションオブジェクトを作成
requestsと同様、 requests.Session
オブジェクトを作成します。
Create Session | RequestsKeywords
今回は任意のセッション名とURLのみ指定していますが、他にもCookieなどいろいろと指定できますので、詳しくはドキュメントを見てください。
# 第一引数は任意のセッション名、第二引数はURL create session not-found-session ${src404}
画像のURLをGET
stackoverflowにはGet
キーワードが書かれています。
しかし、
- ドキュメントでは
Deprecated
- 実行すると、
Deprecation Warning: Use Get Request in the future
ワーニング
ですので、キーワード Get Request
を代わりに使います。
Get Request | RequestsKeywords
なお、上記のstackoverflowによると、URLは URL is relative to the session URL
とのことなので、今回も同じように /
を指定します。
${response404} = get request not-found-session / log to console ${response404} #=> <Response [404]>
ステータスコードの検証
上記の通り、Get Requestキーワードの戻り値はResponseオブジェクトになります。
そのため、requestsのResponseオブジェクトの属性 status_code
を使って、ステータスコードを検証します。
status_code - class requests.Response | Developer Interface — Requests 2.18.3 documentation
なお、検証で使うキーワードは Should Be Equal As Integers
と Should Be Equal As Strings
、どちらでも同じ結果になります。
should be equal as integers ${response404.status_code} 404 should be equal as strings ${response404.status_code} 404
以上で、ファイルが存在しないことを確認できました。
なお、静的ファイルが存在することを確認するには、
${src200} = get element attribute id=img_200@src create session found-session ${src200} ${response200} = get request found-session / should be equal as strings ${response200.status_code} 200
となります。
ソースコード
GitHubに上げました。selenium2_library_sample/tests/
の中にあるselenium_requests_test.robot
ファイルが今回のテストファイルです。
thinkAmi-sandbox/RobotFramework-sample