プログラミング

RubyにおけるSelenium Webdriverの概要・使用方法

webdriverではブラウザのオートメーションが可能。
UIテストなどの他、色々勝手にブラウザ操作をやってもらうのにも重宝する。

インストール

gem install selenium-webdriver

リファレンス

https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
http://seleniumhq.github.io/selenium/docs/api/rb/_index.html

element クラスメソッド

Selenium::WebDriver::Element。clickやsend_keyなどがよく使われる。

http://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Element.html

一定時間の待機

要素が出現するまで待機させることが可能

明示的待機

  wait = Selenium::WebDriver::Wait.new(timeout: 3)
  wait.until { driver.find_element(id: "cheese").displayed? }

暗黙的待機

  driver = Selenium::WebDriver.for :firefox
  driver.manage.timeouts.implicit_wait = 3 # seconds

javaScriptのダイアログ処理

You can use WebDriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.

アラートなどを処理させることができる。アラートのオートメーション妨害を防ぐことが可能。

サンプル

driver.find_element(name: 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

デバッグ

フルログを出力する
Selenium::WebDriver.logger.level = :debug

ファイルへ書き出す
Selenium::WebDriver.logger.output = 'selenium.log'

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です