10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-04-15

はじめに

テスト作成&実行

設定ファイルなどの編集

$ rails new sample_app2 --skip-bundle
  • サンプルRailsアプリ作成
Gemfile
group :test, :development do
  gem 'sauce', '~> 3.1.1'
  gem 'sauce-connect'
  gem 'parallel_tests'
  gem "capybara", "~> 2.0.3"
  gem "cucumber-rails", :require => false
  gem "sauce-cucumber", :require => false
  gem "rake", "10.1.0"
  gem "execjs"
  gem "therubyracer"
  gem "database_cleaner"
end
$ bundle install
$ rails generate cucumber:install
$ rake sauce:install:features
features/support/sauce_helper.rb
require "sauce"
require 'sauce/capybara'
require "sauce/cucumber"

Capybara.default_driver = :sauce

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

テスト作成

features/ramen.feature
Feature: Finding ramen

@selenium
Scenario: I should be able to find delicious Ramen on Wikipedia 
  When I go to Wikipedia.com
  • 今回のテストもwikipediaでラーメンを検索するだけw
features/step_definitions/ramen.rb
When(/^I go to Wikipedia.com$/) do
  visit "http://en.wikipedia.org/"
  fill_in 'search', :with => "Ramen"
  click_button "searchButton"
  page.has_content?('Ramen')
end

テスト実行

$ export SAUCE_USERNAME=XXXXXXXXXXXXXX
$ export SAUCE_ACCESS_KEY=XXXXXXXXXXXXXX

$ bundle exec cucumber
Using the default profile...
Feature: Finding ramen

  @selenium
  Scenario: I should be able to find delicious Ramen on Wikipedia # features/ramen.feature:4
[Connecting to Sauce Labs...]
    When I go to Wikipedia.com                                    # features/step_definitions/ramen.rb:2
    When I go to Wikipedia.com                                    # features/step_definitions/ramen.rb:2
    When I go to Wikipedia.com                                    # features/step_definitions/ramen.rb:2
    When I go to Wikipedia.com                                    # features/step_definitions/ramen.rb:2

1 scenario (1 passed)
1 step (1 passed)
3m37.807s
  • 結果はRspecのときのようにコンソール出力とブラウザから動画で閲覧できます
  • 実行コマンドの引数などでブラウザ指定などしなくて済むので楽ですね

おまけ

  • bundle exec cucumberを実行したら以下のようなエラーが発生したが、これはSeleniumでおなじみなタイムアウトのようです
$ bundle exec cucumber
Using the default profile...
Feature: Finding ramen

  @selenium
  Scenario: I should be able to find delicious Ramen on Wikipedia # features/ramen.feature:4
[Connecting to Sauce Labs...]
 Sauce Connect failed to connect after 90 seconds (RuntimeError)
 /home/ec2-user/.rvm/gems/ruby-1.9.3-p545/gems/sauce-connect-3.1.0/lib/sauce/connect.rb:82:in `wait_until_ready'
 /home/ec2-user/.rvm/gems/ruby-1.9.3-p545/gems/sauce-3.1.3/lib/sauce/utilities/connect.rb:16:in `start'
 /home/ec2-user/.rvm/gems/ruby-1.9.3-p545/gems/sauce-cucumber-3.1.1/lib/sauce/cucumber.rb:70:in `around_hook'
 /home/ec2-user/.rvm/gems/ruby-1.9.3-p545/gems/sauce-cucumber-3.1.1/lib/sauce/cucumber.rb:155:in `Around'

非Railsアプリでのテスト作成・実行

  • 基本的に本ページの「テスト作成&実行 」節で書いたファイルのとおり
  • 以下の構成になっていればよい
  • Gemfile
  • fearures/xxx.feature
  • fearures/support/sauce_helper.rb
  • features/step_definitions/xxx_steps.rb
  • 手を加えるのはfearures/support/sauce_helper.rb内に「include Capybara::DSL」を加えるだけ
  • テスト実行も同じ方法

~ただの宣伝~

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?