| title | category | layout | weight | updated | tags | |
|---|---|---|---|---|---|---|
Capybara |
Ruby libraries |
2017/sheet |
-5 |
2020-06-13 |
|
visit articles_path
click_on 'Link Text'
click_button
click_linkattach_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'
unselectwithin '.classname' do
click '...'
endwithin_fieldset :id do
...
endpage.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') # OKexpect(page).not_to have_button('Save') # BadUse 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_contentAll matchers have these options: {: .-setup}
text: 'welcome'
text: /Hello/
visible: true
count: 4
between: 2..5
minimum: 2
maximum: 5
wait: 10find(selector)
find_button(selector)
find_by_id(id)
find_field(selector)
find_link(selector)
locatewithin '#delivery' do
fill_in 'Street', with: 'Hello'
endwithin :xpath, '//article'
within_fieldset
within_table
within_frame
scope_tofind('#x').fill_in('Street', with: 'Hello')
# same as withinexecute_script('$("input").trigger("change")')
evaluate_script('window.ga')Executes JavaScript.
save_and_open_pageOpens the webpage in your browser.
page
.all('h3')
.body
.html
.source
.current_host
.current_path
.current_urlusing_wait_time 10 do
...
enddrag
field_labeled
page.status_code == 200
page.response_headersSee: 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 = :poltergeistUse 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}" ] }
endEnable inspector: true and then:
{: .-setup}
page.driver.debugTo pause execution for a while:
page.driver.pauseaccept_alert { ... }
dismiss_confirm { ... }
accept_prompt(with: 'hi') { ... }Alternatively:
page.driver.browser.switch_to.alert.acceptpage.set_rack_session(foo: 'bar'){: .-one-column}