On Github bryan-zake / extension_slides
Created and Presented by Reid Fu, Jimmy Kang, Chris Phillips, Jen Risch, and Bryan Zake
Capybara.default_driver =>: rack_test # Change default driver Capybara.default_driver = :selenium # Temporarily change the driver Capybara.current_driver = :webkit #Run some tests! Capybara.use_default_driver
Uses QtWebKit to start rendering engineSignificantly faster than Selenium
gem install capybara-webkit Capybara.javascript-driver = :webkit
Uses PhantomJS a headless WebkitFast and native support for various web standards
#Overide selenium to use chrome Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end #Switch browsers effortlessly ruby Capybara.current_driver = :selenium_chrome #Configure :rack_test headers Capybara.register_driver :rack_test do |app| Capybara::RackTest::Driver.new(app, :headers => {'HTTP_USER_AGENT' => 'Capybara'}) end
#Executes commands within specific part of page within(selector) do ... end #Returns window or tab reference window_opened_by do ... end
require 'capybara' session = Capybara::Session.new :selenium session.visit 'www.google.com' session.click_link_or_button 'Sign in' session.fill_in 'Email', :with => 'your email' session.click_button 'Next' session.fill_in 'Password', :with => 'your password' session.click_button 'Sign in' #Replace with assert puts 'ha, I win'
#Examples of tools for interacting with form elements fill_in('Name', :with => 'John') choose('A radio button') check('A checkbox') uncheck('A checkbox') attach_file('Image', '/path/to/image.jpg') select('Option', :from => 'Select Box')
#Examples of page querying for element existence and manipulation page.has_selector?('table tr') page.has_selector?(:xpath, '//table/tr') page.has_xpath?('//table/tr') page.has_css?('table tr.foo') page.has_content?('foo')
#Examples of finding specific elements find_field('Name').value find_link('Hello', :visible => :all).visible? find_button('Send').click find(:xpath, "//table/tr").click find("#overlay").find("h1").click all('a').each { |a| a[:href] }
#Accept or dismiss alert messages by wrapping in a block: accept_alert do click_link('Show Alert') end #Also accept or dismiss a confirmation in this way: dismiss_confirm do click_link('Show Confirm') end
#Accept or dismiss prompts and provide text for the response: accept_prompt(with: 'Response') do click_link('Show Prompt') end #Return and access the prompt message: message = accept_prompt(with: 'Response') do click_link('Show Prompt') end expect(message).to eq('Prompt');
#Take a snapshot and look at it: save_and_open_page #Retrieve the current state of the DOM as a string print page.html #Save a screenshot: page.save_screenshot('screenshot.png') #Save and open a screenshot: save_and_open_screenshot
#When exact is false, it allows substring matches #Also matches "Test Case" Capybara.exact = true click_link("Test Case") #Only matches "Test" Capybara.exact = false click_link("Test")
gem install 'minitest-rails-capybara' #Put in Gemfile gem "minitest-rails" group :test do gem "minitest-rails-capybara" end #Put in test/test_helper.rb in rails app require minitest/rails/capybara $bundle install
$ rails generate minitest:feature CanAccessIndex $ vim test/features/can_access_index_test.rb require "test_helper" class CanAccessIndexTest < Capybara::Rails::TestCase test "sanity" do visit root_path assert_content page, "Hello World" refute_content page, "Goobye All!" end end
$ rails generate minitest:feature CanAccessIndex --spec $ vim test/features/can_access_index_test.rb #Generates a feature test using the Capybara spec DSL require "test_helper" feature "Can Access Index" do scenarion "has content" do visit root_path page.must_have_content "Home#index" end end
https://github.com/jnicklas/capybara
http://www.rubydoc.info/github/jnicklas/capybara/master
http://blowmage.com/minitest-rails-capybara/