title | category | layout | weight | updated |
---|---|---|---|---|
Capybara |
Ruby libraries |
2017/sheet |
-5 |
2017-08-30 |
visit articles_path
click 'Link Text'
click_button
click_link
attach_file 'Image', '/path/to/image.jpg'
fill_in 'First Name', with: 'John'
check 'A checkbox'
uncheck 'A checkbox'
choose 'A radio button'
select 'Option', from: 'Select box'
unselect
within '.classname' do
click '...'
end
within_fieldset :id do
...
end
page.has_css?('.button')
expect(page).to have_css('.button')
page.should have_css('.button')
{: .-setup}
Positive | Negative |
---|---|
has_content? |
has_no_content? |
--- | --- |
has_css? (selector) |
has_no_css? |
--- | --- |
has_xpath? (path) |
has_no_xpath? |
--- | --- |
has_link? (selector) |
has_no_link? |
--- | --- |
has_button? (selector) |
has_no_button? |
--- | --- |
has_field? (selector) |
has_no_field? |
--- | --- |
has_checked_field? (selector) |
has_unchecked_field? |
--- | --- |
has_table? (selector) |
has_no_table? |
--- | --- |
has_select? (selector) |
has_no_select? |
{: .-headers.-left-align} |
In Rspec, these also map to matchers like page.should have_content
.
expect(page).to have_button('Save')
expect(page).to have_button('#submit')
expect(page).to have_button('//[@id="submit"]')
The selector
arguments can be text, CSS selector, or XPath expression.
page.has_button?('Save')
expect(page).to have_no_button('Save')
In RSpec, you can use page.should
assertions.
expect(page).to have_no_button('Save') # OK
expect(page).not_to have_button('Save') # Bad
Use should have_no_*
versions with RSpec matchers because
should_not have_*
doesn't wait for a timeout from the driver.
expect(page).to \
{: .-setup}
have_selector '.blank-state'
have_selector 'h1#hola', text: 'Welcome'
have_button 'Save'
have_checked_field '#field'
have_unchecked_field
have_css '.class'
have_field '#field'
have_table '#table'
have_xpath '//div'
have_link 'Logout', href: logout_path
have_select 'Language',
selected: 'German'
options: ['Engish', 'German']
with_options: ['Engish', 'German'] # partial match
have_text 'Hello',
type: :visible # or :all
# alias: have_content
All matchers have these options: {: .-setup}
text: 'welcome'
text: /Hello/
visible: true
count: 4
between: 2..5
minimum: 2
maximum: 5
wait: 10
find(selector)
find_button(selector)
find_by_id(id)
find_field(selector)
find_link(selector)
locate
within '#delivery' do
fill_in 'Street', with: 'Hello'
end
within :xpath, '//article'
within_fieldset
within_table
within_frame
scope_to
find('#x').fill_in('Street', with: 'Hello')
# same as within
execute_script('$("input").trigger('change')')
evaluate_script('window.ga')
Executes JavaScript.
save_and_open_page
Opens the webpage in your browser.
page
.all('h3')
.body
.html
.source
.current_host
.current_path
.current_url
using_wait_time 10 do
...
end
drag
field_labeled
page.status_code == 200
page.response_headers
See: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Session
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.javascript_driver = :poltergeist
Use poltergeist to integrate PhantomJS.
config.before :each, :js do
page.driver.browser.url_blacklist = [
'fonts.googleapis.com',
'use.typekit.net',
'f.vimeocdn.com',
'player.vimeo.com',
'www.googletagmanager.com'
].flat_map { |domain| [ "http://#{domain}", "https://#{domain}" ] }
end
Enable inspector: true
and then:
{: .-setup}
page.driver.debug
To pause execution for a while:
page.driver.pause
accept_alert { ... }
dismiss_confirm { ... }
accept_prompt(with: 'hi') { ... }
Alternatively:
page.driver.browser.switch_to.alert.accept
page.set_rack_session(foo: 'bar')
{: .-one-column}