“Capybara” is a library to helps you test web applications. This article is a cheat sheet for Ruby “Capybara “
🚕 Get Started Install Rubygem capybara
and selenium-webdriver
:
$ gem install capybara $ gem install selenium-webdriver
Write sample code to capybara_sample_spec.rb
:
require 'capybara/rspec' Capybara.default_driver = :selenium feature 'Capybara Sample' do scenario 'success something' do visit 'http://example.com/' fill_in('name' , with: 'Sample User' ) click_button('goto_next' ) end end
You can execute capybara_sample_spec.rb
by RSpec:
$ rspec capybara_sample_spec.rb
😸 Click Examples click_button('button text' ) click_link('link text' ) click_on('button or link text' )
🐮 Interecting with Form Examples fill_in('some text' , with: 'text_tag_selector' ) select('some option' , with: 'select_tag_selctor' ) check('checkbox_selector' ) uncheck('checkbox_selector' ) choose('yes' ) attach_file('attach_file_selector' , '/path/to/dog.jpg' )
😼 Element Examples find(:css , 'css selector' , options) find(:xpath , 'xpath value' , option) find(:xpath , 'some xpath' ).set('some text' ) find('id_selector' , visible: false ).text find_field('id_selector' ).value find('checkbox_selector' ).checked? find('select_tag_selctor' ).selected? find('a' , text: 'next' ).visible?
🏀 Querying Examples expect(page).to have_css('#something' ) expect(page).to has_xpath('#something' ) expect(page).to has_content('#something' ) expect(page).to has_no_content('#something' ) expect(page).to has_title('#something' )
🐯 Scope Examples within("//li[@id='example']" ) do fill_in 'Full Name' , with: 'John Due' end within(:css , "li#example" ) do fill_in 'Full Name' , :with => 'John Due' end
🐝 Other Examples Capybara.using_wait_time(30 ) do expect(find('#message' )).to have_text('Complete' ) end accept_alert do click_on 'Show Alert' end dismiss_confirm do click_on 'Delete' end execute_script 'window.scrollTo(0, 900)' save_screenshot('xxx.png' ) save_and_open_page
🎂 Using Capybara without RSpec Sample code using Capybara without RSpec or other framework is as follows:
require 'capybara' Capybara.default_driver = :selenium class CapybaraSampleClass include Capybara::DSL def sample_method visit 'http://example.selenium.com/' fill_in('name' , with: 'sample user' ) end end sample = CapybaraSampleClass.new sample.sample_method
$ ruby cabybara_sample.rb
🗽 Appendix: Page Object A page object is an object-oriented class that serves as an interface to a page of your TEST.
The Page Object Design Pattern provides the following advantages:
There is a clean separation between test code and page specific code such as locators.
There is a single repository for the services or operations offered by the page.
Policy of the Page Object Design Patttern is as follows:
Create a method to abstract controlling UI
Return new Page Object if there is page transition
Not include assertion logic in Page Object
Should check successful of page transition
🤔 Special Thanks
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results !!