3 category: Ruby libraries
14 ### Clicking links and buttons
22 ### Interacting with forms
25 attach_file 'Image', '/path/to/image.jpg'
26 fill_in 'First Name', with: 'John'
35 choose 'A radio button'
39 select 'Option', from: 'Select box'
46 within '.classname' do
52 within_fieldset :id do
62 page.has_css?('.button')
63 expect(page).to have_css('.button')
64 page.should have_css('.button')
68 | Positive | Negative |
70 | `has_content?` | `has_no_content?` |
72 | `has_css?` _(selector)_ | `has_no_css?` |
74 | `has_xpath?` _(path)_ | `has_no_xpath?` |
76 | `has_link?` _(selector)_ | `has_no_link?` |
78 | `has_button?` _(selector)_ | `has_no_button?` |
80 | `has_field?` _(selector)_ | `has_no_field?` |
82 | `has_checked_field?` _(selector)_ | `has_unchecked_field?` |
84 | `has_table?` _(selector)_ | `has_no_table?` |
86 | `has_select?` _(selector)_ | `has_no_select?` |
87 {: .-headers.-left-align}
89 In Rspec, these also map to matchers like `page.should have_content`.
94 expect(page).to have_button('Save')
98 expect(page).to have_button('#submit')
102 expect(page).to have_button('//[@id="submit"]')
105 The `selector` arguments can be text, CSS selector, or XPath expression.
110 page.has_button?('Save')
114 expect(page).to have_no_button('Save')
117 In RSpec, you can use `page.should` assertions.
122 expect(page).to have_no_button('Save') # OK
125 expect(page).not_to have_button('Save') # Bad
128 Use `should have_no_*` versions with RSpec matchers because
129 `should_not have_*` doesn't wait for a timeout from the driver.
141 have_current_path(expected_path)
142 have_selector '.blank-state'
143 have_selector 'h1#hola', text: 'Welcome'
145 have_checked_field '#field'
154 have_link 'Logout', href: logout_path
158 have_select 'Language',
160 options: ['Engish', 'German']
161 with_options: ['Engish', 'German'] # partial match
166 type: :visible # or :all
167 # alias: have_content
172 All matchers have these options:
192 find_button(selector)
202 within '#delivery' do
203 fill_in 'Street', with: 'Hello'
208 within :xpath, '//article'
216 find('#x').fill_in('Street', with: 'Hello')
223 execute_script('$("input").trigger("change")')
224 evaluate_script('window.ga')
235 Opens the webpage in your browser.
253 using_wait_time 10 do
266 page.status_code == 200
267 page.response_headers
270 See: <https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Session>
275 Capybara.register_driver :poltergeist do |app|
276 Capybara::Poltergeist::Driver.new(app, :inspector => true)
278 Capybara.javascript_driver = :poltergeist
281 Use [poltergeist](https://github.com/teampoltergeist/poltergeist) to integrate PhantomJS.
286 config.before :each, :js do
287 page.driver.browser.url_blacklist = [
288 'fonts.googleapis.com',
292 'www.googletagmanager.com'
293 ].flat_map { |domain| [ "http://#{domain}", "https://#{domain}" ] }
299 Enable `inspector: true` and then:
306 To pause execution for a while:
314 ### Accepting confirm() and alert()
318 dismiss_confirm { ... }
319 accept_prompt(with: 'hi') { ... }
325 page.driver.browser.switch_to.alert.accept
331 page.set_rack_session(foo: 'bar')
337 - <https://rubydoc.info/github/jnicklas/capybara/Capybara/RSpecMatchers>
338 - <https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers>