11
10

More than 5 years have passed since last update.

EC2+Sauce Labsを使ってSeleniumのテストを高速に(Rspec編)

Last updated at Posted at 2014-04-15

はじめに

  • 40minくらいでSauce Labsを使ったSelenium無料で体感できます!!
    • トライアル(無料)会員なら2時間/月の自動テストが無料で使える
    • 並列実行は2つまで
  • SauceLabsを使ってすごいなと思ったのは、設定ファイルにドバっと回したいブラウザ書くだけで様々なブラウザを使ってテストしてくれちゃうんです
  • 機材の調達や環境構築は不要!!
  • しかもテスト動画で録画してくれてるので、失敗時の現場検証もラクチン
  • 今回はサンプルなRailsアプリを用意してRspecでテストを動かしますが、wikipediaで検索するだけなのでアプリの中身は何も書きません

事前準備【5min】

  • Sauce Labsのアカウントを登録
    • メールが受信できるメールアドレスを取得しておくこと
  • EC2にsmall以上のインスタンスを立てておく

実験環境の構築&必要なものをインストール【25min】

  • EC2 Amazon Linux small instance
  • 以下はEC2にはいってないので要インストール
    • Ruby(RVM) : 1.9.3
    • Ruby on Rails : 4.1.0

RVMインストール

$ curl -L get.rvm.io | bash -s stable
$ source ~/.rvm/scripts/rvm
$ rvm install 1.9.3
$ rvm rubygems latest
$ rvm use --default 1.9.3

Railsインストール&実験用サンプルRailsアプリ作成

$ gem install rails
$ rails new sample_app --skip-bundle
  • SauceLabsにログインする
  • SauceLabsを開く
  • ① Choose your languageで言語を選択(ここではRuby)
  • ② Setting Up a Project内に従ってコードを書いていく
  • Gemfile編集
$ cd sample_app
$ vi Gemfile
Gemfile
# 以下を追加
group :test, :development do
  gem "rspec-rails", "~> 2.12"
  gem "sauce", "~> 3.1.1"
  gem "sauce-connect"
  gem "capybara", "~> 2.0.3"
  gem "parallel_tests"
  gem "execjs"
  gem "therubyracer"
end
$ sudo yum install sqlite-devel #これを入れないとsqlite3が入らないので
$ bundle install
$ gem install sauce-connect #bundle installで入らない場合はこちらも

テスト作成&実行【10min】

設定ファイルなどの編集

$ rails generate rspec:install
$ rake sauce:install:spec
  • 以下のファイルを編集
spec/sauce_helper.rb
# Use Capybara integration
require "sauce"
require "sauce/capybara"

# Set up configuration
Sauce.config do |c|
  c[:browsers] = [ 
    ["Windows 8", "Internet Explorer", "10"],             
    ["Windows 7", "Firefox", "20"],
    ["OS X 10.8", "Safari", "6"],                         
    ["Linux", "Chrome", nil]          
  ]
end
spec/spec_helper.rb
require 'capybara/rails'
require 'capybara/rspec'

Capybara.default_driver = :sauce
~/.bash_profile
export SAUCE_USERNAME=XXXXXXXXXXXXXX
export SAUCE_ACCESS_KEY=XXXXXXXXXXXXXX
  • SAUCE_USERNAME,SAUCE_ACCESS_KEYはsauceLabsのSetting up the Sauce Gem節に以下が書いてあるのでコピーして追記
$ source ~/.bash_profile

テスト作成

$ mkdir ./spec/features
  • featureディレクトリ作成
spec/features/ramen_spec.rb
require "spec_helper"

describe "Wikipedia's Ramen Page", :sauce => true do
  it "Should mention the inventor of instant Ramen" do
    visit "http://en.wikipedia.org/"
    fill_in 'search', :with => "Ramen"
    click_button "searchButton"
    page.should have_content "Momofuku Ando"
  end 
end

テスト実行

$ rake sauce:spec
 
### 以下コンソール出力
2 processes for 4 specs, ~ 2 specs per process
Starting Rails server on port 3000...
Run options: include {:sauce=>true}
Starting Rails server on port 3030...
Run options: include {:sauce=>true}
[2014-04-15 02:36:54] INFO  WEBrick 1.3.1
[2014-04-15 02:36:54] INFO  ruby 1.9.3 (2014-02-24) [x86_64-linux]
[2014-04-15 02:36:54] INFO  WEBrick::HTTPServer#start: pid=30974 port=3000
[2014-04-15 02:36:54] INFO  WEBrick 1.3.1
[2014-04-15 02:36:54] INFO  ruby 1.9.3 (2014-02-24) [x86_64-linux]
[2014-04-15 02:36:54] INFO  WEBrick::HTTPServer#start: pid=30978 port=3030
Rails server running!
[Connecting to Sauce Labs...]
Rails server running!
.[2014-04-15 02:39:00] INFO  going to shutdown ...
[2014-04-15 02:39:00] INFO  WEBrick::HTTPServer#start done.
=> Booting WEBrick
=> Rails 4.1.0 application starting in test on http://0.0.0.0:3030
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting


Finished in 2 minutes 36.8 seconds
1 example, 0 failures

Randomized with seed 11161

.[2014-04-15 02:39:19] INFO  going to shutdown ...
[2014-04-15 02:39:19] INFO  WEBrick::HTTPServer#start done.
=> Booting WEBrick
=> Rails 4.1.0 application starting in test on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting


Finished in 2 minutes 56.3 seconds
1 example, 0 failures

Randomized with seed 20677


2 examples, 0 failures

Took 216.296082459 seconds
  • テスト1つなのに時間がかかっているのは、OS/ブラウザの立ち上げで時間がかかっているだけと予想
  • 実行結果はSauceLabsからも閲覧可能
    SauceLabsResults.png

    • spec/sauce_helper.rbに書いた4つのブラウザで実行されている すげ〜〜 ^q^
  • 実行した任意の結果セッション名をクリックするとSeleniumのログや実行したテストの動画が閲覧可能

    • 無料会員のため2週間しか動画再生も有効でない(?かもしれない)ため画像を添付

SauseLabsResult2.png


~ただの宣伝~

  • 全国のSeleniumer必読
  • Selenium, SauceLabs, Travis, Jenkinsに関するノウハウ書いているのでよかったら参考にしてみてください
11
10
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
11
10