Memo

メモ > 技術 > 開発: Selenium > Ruby版Selenium環境をWindowsに構築

■Ruby版Selenium環境をWindowsに構築
WebのUIテスト自動化 - Seleniumを使ってみる http://qiita.com/edo_m18/items/ba7d8a95818e9c0552d9 Selenium WebDriver + Ruby でWeb関係の自動化 http://ntkg.hatenablog.com/entry/2016/06/08/225241 Webブラウザの自動操作 (Selenium with Rubyの実例集) https://www.qoosky.io/techs/71dd2d67ea 全国のSeleniumer必読 http://qiita.com/oh_rusty_nail/items/b8ba525d31ea7c522856 RubyでSeleniumを使ってスクレイピング http://qiita.com/tomerun/items/9cb81d7a98150ff22f53 Selenium WebDriver + Ruby でWeb関係の自動化 http://ntkg.hatenablog.com/entry/2016/06/08/225241 まずはインストール
C:\Users\refirio>gem install selenium-webdriver ERROR: Could not find a valid gem 'selenium-webdriver' (>= 0), here is why: Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/latest_specs.4.8.gz) gem source --add https://rubygems.org/ gem source --remove http://rubygems.org/ gem source https://rubygems.org/ gem install "selenium-webdriver"
↑すべてダメだった selenium-webdriverをインストールできない
C:\Users\refirio>gem install "selenium-webdriver" --source http://rubygems.org" Fetching: rubyzip-1.2.0.gem (100%) Successfully installed rubyzip-1.2.0 Fetching: ffi-1.9.14-x64-mingw32.gem (100%) Successfully installed ffi-1.9.14-x64-mingw32 Fetching: childprocess-0.5.9.gem (100%) Successfully installed childprocess-0.5.9 Fetching: websocket-1.2.3.gem (100%) Successfully installed websocket-1.2.3 Fetching: selenium-webdriver-3.0.3.gem (100%) Successfully installed selenium-webdriver-3.0.3 Parsing documentation for childprocess-0.5.9 Installing ri documentation for childprocess-0.5.9 Parsing documentation for ffi-1.9.14-x64-mingw32 Installing ri documentation for ffi-1.9.14-x64-mingw32 Parsing documentation for rubyzip-1.2.0 Installing ri documentation for rubyzip-1.2.0 Parsing documentation for selenium-webdriver-3.0.3 Installing ri documentation for selenium-webdriver-3.0.3 Parsing documentation for websocket-1.2.3 Installing ri documentation for websocket-1.2.3 Done installing documentation for childprocess, ffi, rubyzip, selenium-webdriver, websocket after 7 seconds WARNING: Unable to pull data from 'https://rubygems.org/': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/latest_specs.4.8.gz) 5 gems installed
↑「--source http://rubygems.org"」を指定するとインストールできた
C:\localhost\home\ruby\public_html\selenium>irb irb(main):001:0> require "selenium-webdriver" => true irb(main):002:0> driver = Selenium::WebDriver.for :firefox Selenium::WebDriver::Error::WebDriverError: Unable to find Mozilla geckodriver. Please download the server from https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH. More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.
↑一つずつコマンドを指定してみると、ドライバが必要だと言われた https://github.com/mozilla/geckodriver/releases から geckodriver-v0.11.1-win64.zip をダウンロード 展開して作成された geckodriver.exe を C:\selenium に配置し、PATH を登録
require "selenium-webdriver" # Firefox用のドライバを使う driver = Selenium::WebDriver.for :firefox # Googleにアクセス driver.navigate.to "http://google.com" # `q`というnameを持つ要素を取得 element = driver.find_element(:name, 'q') # `Hello WebDriver!`という文字を、上記で取得したinput要素に入力 element.send_keys "Hello WebDriver!" # submitを実行する(つまり検索する) element.submit # 表示されたページのタイトルをコンソールに出力 puts driver.title # 3秒待つ(サンプル) sleep 3 # テストを終了する(ブラウザを終了させる) driver.quit
コマンドラインから実行すると、Firefoxが立ち上がった
C:\localhost\home\ruby\public_html\selenium>ruby selenium.rb Google
テストサイトを開き、管理者ページにログインし、スクリーンショットを撮影するサンプル
require "selenium-webdriver" # Firefox用のドライバを使う driver = Selenium::WebDriver.for :firefox # テストサイトにアクセス driver.navigate.to "http://localhost/~test/levis-members/" # 「管理者用」リンクをクリック driver.find_element(:xpath, '//ul[1]/li[3]/a[1]').click # 1秒待つ sleep 1 # ログインフォームに、ユーザ名とパスワードを入力 element = driver.find_element(:name, 'username') element.send_keys "admin" element = driver.find_element(:name, 'password') element.send_keys "1234" # ログインする element.submit # 表示されたページのタイトルをコンソールに出力 puts driver.title # スクリーンショットを保存 driver.save_screenshot "ss/admin.png" # 3秒待つ sleep 3 # テストを終了する(ブラウザを終了させる) driver.quit
何故か要素を参照できない…という場合、「sleep 1」のように待ってから参照するといい nokogiriを扱う準備としてXPathを学ぶ http://d.hatena.ne.jp/riocampos+tech/20130824/p1

Advertisement