Capybara Exemplos de configuração Com cucumber-rails Com cucumber sem Rails rails generate cucumber:install -- capybara require 'capybara/cucumber' Capybara.app = MyRackApp Nos steps do cucumber When /I sign in/ do Tags para uso de JS @javascript Capybara.javascript_driver = :culerity Tags disponívels @javascript @selenium @culerity @rack_test Utilizando com RSpec require 'capybara/rspec' describe "the signup process", :type => :request do before :each do User.make(:email =>, :password => 'caplin') it "signs me in" do
DSL do Capybara feature "Signing up" do background do User.make(:email =>, :password => 'caplin') scenario "Signing in with correct credentials" do fill_in 'Password', :with => 'caplin' Isto é apenas uma forma de utilizar o RSpec. Habilitando o driver JS feature é um alias para describe, :type => :request background é um alias para before' scenario é um alias para it/specify describe 'some stuff which requires js', :js => true do it 'will use the default js driver' it 'will switch to one specific driver', :driver => :celerity Junto com Test::Unit class ActionDispatch::IntegrationTest include Capybara::DSL Capybara com qualquer aplicação Rack Capybara.app = MyRackApp Selecionando o Driver Capybara.default_driver = :selenium Capybara.current_driver = :rack_test # Este é o padrão se não mudarmos para outro na linha acima Capybara.use_default_driver #volta para o driver padrão A DSL
Navegando visit('/projects') visit(post_comments_path(post)) current_path.should == post_comments_path(post) Clicando em links e botões click_link('id-of-link') click_link('link Text') click_button('save') click_on('link Text') # clica em um link ou botão click_on('button Value') Interagindo com formulários fill_in('first Name', :with => 'John') fill_in('password', :with => 'Seekrit') fill_in('description', :with => 'Really Long Text') choose('a Radio Button') check('a Checkbox') uncheck('a Checkbox') attach_file('image', '/path/to/image.jpg') select('option', :from => 'Select Box') Verificando o conteúdo da página page.has_selector?('table tr') page.has_selector?(:xpath, '//table/tr') page.has_no_selector?(:content) page.has_xpath?('//table/tr') page.has_css?('table tr.foo') page.has_content?('foo') page.has_text?('foo') Verificando o conteúdo da página com RSpec page.should have_selector('table tr') page.should have_selector(:xpath, '//table/tr') page.should have_no_selector(:content) page.should have_xpath('//table/tr') page.should have_css('table tr.foo') page.should have_text('foo') page.should have_no_text('foo') page.html.should match /<span>/i Buscando find_field('first Name').value find_link('hello').visible? find_button('s').click find(:xpath, "//table/tr").click find("#overlay").find("h1").click all('a').each { a a[:href] } find('#navigation').click_link('home') find('#navigation').should have_button('sign out') Definindo escopos within("li#employee") do Rodando Javascript page.execute_script("$('body').empty()") result = page.evaluate_script('4 + 4'); within(:xpath, "//li[@id='employee']") do within_fieldset('employee') do within_table('employee') do
Debug save_and_open_page Mais exemplos Utilizando a DSL em qualquer lugar require 'capybara' require 'capybara/dsl' Capybara.default_driver = :culerity module MyModule include Capybara::DSL def login! within("//form[@id='session']") do Chamando servidores remotos visit('http://www.google.com') Capybara.current_driver = :selenium Capybara.app_host = 'http://www.google.com' visit('/') Capybara sem o servidor Rack interno Capybara.run_server = false Session require 'capybara' session = Capybara::Session.new(:culerity, my_rack_app) session.within("//form[@id='session']") do session. session.fill_in 'Password', :with => 'password' session. Seletores XPath within(:xpath, '//ul/li') { } find(:xpath, '//ul/li').text find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value Seletores padrão Capybara.default_selector = :xpath find('//ul/li').text
Seletores personalizados Capybara.add_selector(:id) do xpath { id XPath.descant[XPath.attr(:id) == id.to_s] } Capybara.add_selector(:row) do xpath { num ".//tbody/tr[#{num}]" } Capybara.add_selector(:flash_type) do css { type "#flash.#{type}" } Adicionando drivers Capybara.register_driver :selenium do app Capybara::Selenium::Driver.new(app, :browser => :chrome) Capybara.register_driver :selenium_chrome do app Capybara::Selenium::Driver.new(app, :browser => :chrome) find(:id, 'post_123') find(:row, 3) find(:flash_type, :notice) Parte do Livro de Rails 3.1 de Rodrigo Urubatan Ferreira Jardim, guia de referência rápida publicado originalmente em http://www.urubatan.com.br