hrk1l2x

1 minute read

Capybara basic

env

  • RSpec 3.3.2
  • Capybara 2.5.0

define browser size

feature "xxx", js: true do
  scenario "xxx", js: true do
    visit 'https://xxx.com'
    def ensure_browser_size(width = 1300, height = 800)
      Capybara.current_session.driver.browser.manage.window.resize_to(width, height)
    end
    ensure_browser_size if Capybara.current_driver == :selenium
    ...
  end
end

exec JavaScript on Capybara

require "selenium-webdriver"
require 'capybara/rspec'
require 'pry'

RSpec.configure do |config|
config.include Capybara::DSL
end

# Firefox
Capybara.default_driver = :selenium

# Chrome call by "Capybara.current_driver = :selenium_chrome"
Capybara.register_driver :selenium_chrome do |app|
  # http://code.google.com/p/chromedriver/downloads/list
  # 配置 /usr/local/bin/chromedriver
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

# switch test browser
Capybara.current_driver = :selenium

# test environment
Capybara.app_host = ''

feature "xxx" do
  scenario "xxx", js: true do
    page.execute_script "alert('xxx');"
  end
end

exec Capybara with no basic authorization

Capybara.app_host = 'http://username:password@xxx.com'
comments powered by Disqus