LoginSignup
9
5

More than 5 years have passed since last update.

RubyとSeleniumによる自動テスト~スクリプト作成~

Posted at

SeleniumIDEを使ったスクリプト作成

全てのスクリプトを手作業で作ると大変なので、べーすになるものをSeleniumIDEで作成します。
 ※SeleniumIDEの使い方は割愛します

SeleniumIDEからRubyのコードをExportする

  • テストスイートの保存

SeleniumIDEのメニューから、
「ファイル」→「テストスイートをエクスポート」→「Ruby / RSpec / WebDriver」
とたどり、名前を付けて保存する。

名前は、 テストケース名と同一 にすること
(テストスイートの中で、そのファイル名で読み込む記述になっている)

これを、テストケース毎に実行する必要があってツラいです・・・
できることなら、この操作をSeleniumで自動化したいくらいですww

Exportしたコードの変更

変更点(1)

コードはExportしたままだとエラーになり、動作しません。
テストケースのソースファイル内で ${receiver}@driver に全て置換してください。

変更点(2)

上記の変更のみだと、テストケース毎にウィンドウが開くことになってしまいます。
使い方によっては、同一のウィンドウで操作を実行するようにしたい場合もあります。
(前後のテストケースで連続性があるような場合)
その場合は、この変更を行ってください。

テストケースの変更

テストケースに記述されている以下の箇所を共通化するため、全てテストケースからは削除して、テストスイートに記述します。

  • before(:each) do ~ end
  • after(:each) do ~ end
  • def element_present?(how, what) 以降に宣言されているメソッド

テストスイートの変更

before

テストスイートのソース(Exportされた状態)
require "spec/ruby"
require "spec/runner"

# output T/F as Green/Red
ENV['RSPEC_COLOR'] = 'true'

require File.join(File.dirname(__FILE__),  "checkcase009")
  
  

after

テストスイートのソース(修正後)
require 'selenium-webdriver'
# require 'pry'

# output T/F as Green/Red
ENV['RSPEC_COLOR'] = 'true'

# :firefox / :chrome / :edge / :ie
driver = Selenium::WebDriver.for :firefox
base_url = "http://example.com"

RSpec.configure do |config|
  config.before(:each) do
    @driver = driver
    @base_url = base_url
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
    puts "TestCase : #{self.class.to_s.split('::').last}"
  end

  config.after(:each) do
    unless @result.nil? then
      puts "ProcTime : #{@result}s"
      @result = nil
    end
    @verification_errors.should == []
  end

  config.after(:suite) do
    driver.quit
  end

  config.include TestHelper
end

module TestHelper
  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end

  def alert_present?()
    @driver.switch_to.alert
    true
  rescue Selenium::WebDriver::Error::NoAlertPresentError
    false
  end

  def verify(&blk)
    yield
  rescue ExpectationNotMetError => ex
    @verification_errors << ex
  end

  def close_alert_and_get_its_text(how, what)
    alert = @driver.switch_to().alert()
    alert_text = alert.text
    if (@accept_next_alert) then
      alert.accept()
    else
      alert.dismiss()
    end
    alert_text
  ensure
    @accept_next_alert = true
  end
end

  
  

変更点(3)

必要に応じて、以下のようなロジックを追記してください。

  • 画面のキャプチャを保存するロジックの追加

キャプチャを撮るタイミングの箇所に

@driver.save_screenshot([保存ファイル名])

を記述する

  • 時間計測のロジックの追加

レスポンスを計る等、時間計測の必要があるときは、テストケースの 時間を計測したい処理ロジック の箇所を下記のように変更してください。

sample.rb
 :
require 'benchmark'
 :
    result = Benchmark.realtime do
     #(時間を計測したい処理ロジック)
    end
    puts "処理時間 #{result}s"
 :

テストスクリプトの実行

コマンドプロンプトから

rspec スクリプトファイル名.rb

クロスブラウザのテスト

テストスイートの最初の方に記載のある

driver = Selenium::WebDriver.for :firefox

:firefox:chrome:ie に変更すると、それぞれのブラウザで動作させることができるはずです。
:edge もできるはずなのですが、WebDriverが対応していないとか何とかで、Rubyからはうまく動作させることができません。。。
(ブラウザが起動しますが、最初のURLの呼び出しもしてくれません)

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5