2
2

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.

RSpec setup

Posted at
  1. $ rails new memlog // .railsrcで bundle installはskip
  2. https://gist.github.com/saiwaki/b4bf543977f1bbf89793
  3. $ bundle install
  4. $ rails g rspec:install
  5. $ guard init // Guardfile作成
  6. .rspecファイル => --format documentation
  7. rails_helper.rb 編集
  8. spec/features 作成
  9. spec/support 作成

rails_helper.rb

rails_helper.rb
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'turnip'
require 'turnip/capybara'
# Capybara
Capybara.server_port = 57124
Capybara.default_wait_time = 10
Capybara.javascript_driver = ENV['DRIVER'] ? ENV['DRIVER'].to_sym : :webkit
Capybara.default_driver = ENV['DRIVER'] ? ENV['DRIVER'].to_sym : :webkit
# FactoryGirl
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

support/capybara_drivers.rb

=begin
Stopped to use 'poltergeist' for headless test.
because I can't resolve below.

Poltergeist detected another element with CSS selector 'html body.phone div.modal-backdrop.fade.in' at this position. It may be overlapping the element you are trying to interact with. If you don't care about overlapping elements, try using node.trigger('click').
=end

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.register_driver :safari do |app|
  Capybara::Selenium::Driver.new(app, browser: :safari)
end

Capybara.register_driver :firefox do |app|
  Capybara::Selenium::Driver.new(app, browser: :firefox)
end

Capybara.register_driver :ie do |app|
  Capybara::Selenium::Driver.new app,
    browser: :remote,
    url: 'http://10.0.1.8:4444/wd/hub',
    desired_capabilities: :internet_explorer
end

Capybara.register_driver :iphone do |app|
  Capybara.default_wait_time = 30
  Capybara::Selenium::Driver.new app,
    browser: :remote,
    url: "http://127.0.0.1:4723/wd/hub",
    desired_capabilities: {
      browserName: 'iphone',
      platform: 'Mac',
      version: '6.1',
      app: 'safari'
    }
end

Capybara.register_driver :ipad do |app|
  Capybara.default_wait_time = 30
  Capybara::Selenium::Driver.new app,
    browser: :remote,
    url: "http://127.0.0.1:4723/wd/hub",
    desired_capabilities: {
      browserName: 'ipad',
      platform: 'Mac',
      version: '6.1',
      app: 'safari',
      deviceOrientation: 'landscape'
    }
end

Capybara.register_driver :android do |app|
  Capybara.default_wait_time = 30
  Capybara::Selenium::Driver.new app,
    browser: :remote,
    url: "http://127.0.0.1:4723/wd/hub",
    desired_capabilities: {
      browserName: 'android',
      platform: 'Mac',
      version: '2.3',
      app: 'safari',
      deviceOrientation: 'landscape'
    }
end

Capybara.register_driver :phone do |app|
  Capybara::Webkit::Driver.new(app)
end

Capybara.register_driver :tablet do |app|
  Capybara::Webkit::Driver.new(app)
end

if %i(iphone ipad phone tablet ios android).include?(Capybara.javascript_driver)
  $el = {
    edit_title: '#edit-page-title-phone',
    edit_body: '#edit-page-body-phone',
    edit_save: '#edit-page-save-phone'
  }
  RSpec.configure do |config|
    config.before :each do

    end
  end
else
  $el = {
    edit_title: '#edit-page-title',
    edit_body: '#edit-page-body',
    edit_save: '#edit-page-save'
  }
end

support/wait_until.rb

def wait_until(time=Capybara.default_wait_time)
  require "timeout"
  Timeout.timeout(time) do
    sleep(0.1) until value = yield
    value
  end
end

def wait_until_visible(selector, time=Capybara.default_wait_time)
  wait_until(time) do
    expect(page).to have_selector(selector, visible: true)
  end
end

def wait_and_find_css(selector, time=Capybara.default_wait_time)
  wait_until(time) do
    page.has_css?(selector)
  end
  find(selector)
end
alias wait_and_find wait_and_find_css

def wait_and_find_xpath(selector, time=Capybara.default_wait_time)
  wait_until(time) do
    page.has_xpath?(selector)
  end
  find(:xpath, selector)
end

support/window.rb

def within_window(name)
  sleep 0.5
  original = current_window
  use_window(name)
  yield
ensure
  use_window(original)
end
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?