cheatsheets/capybara.md

338 lines
5.5 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Capybara
2015-11-24 05:06:06 +00:00
category: Ruby libraries
2017-08-30 17:43:19 +00:00
weight: -5
2020-07-04 13:33:09 +00:00
updated: 2020-06-13
tags: [Featurable]
2013-10-14 02:36:58 +00:00
---
2017-08-30 17:43:19 +00:00
### Navigating
2012-03-16 06:14:31 +00:00
visit articles_path
2017-08-30 17:43:19 +00:00
### Clicking links and buttons
2012-03-16 06:14:31 +00:00
2020-06-13 00:12:49 +00:00
```ruby
click_on 'Link Text'
click_button
click_link
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
### Interacting with forms
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
attach_file 'Image', '/path/to/image.jpg'
fill_in 'First Name', with: 'John'
```
2015-01-16 04:02:16 +00:00
2017-08-30 17:43:19 +00:00
```ruby
check 'A checkbox'
uncheck 'A checkbox'
```
2015-01-16 04:02:16 +00:00
2017-08-30 17:43:19 +00:00
```ruby
choose 'A radio button'
```
2015-01-16 04:02:16 +00:00
2017-08-30 17:43:19 +00:00
```ruby
select 'Option', from: 'Select box'
unselect
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
### Limiting
2015-01-16 04:02:16 +00:00
2017-08-30 17:43:19 +00:00
```ruby
within '.classname' do
click '...'
end
```
2015-01-16 04:02:16 +00:00
2017-08-30 17:43:19 +00:00
```ruby
within_fieldset :id do
...
end
```
2015-01-16 04:02:16 +00:00
2014-10-23 11:43:16 +00:00
## Querying
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
### Predicates
2017-08-31 09:22:58 +00:00
```ruby
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}
2017-08-30 17:43:19 +00:00
In Rspec, these also map to matchers like `page.should have_content`.
### Selectors
```ruby
expect(page).to have_button('Save')
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
expect(page).to have_button('#submit')
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
expect(page).to have_button('//[@id="submit"]')
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
The `selector` arguments can be text, CSS selector, or XPath expression.
2012-03-16 06:14:31 +00:00
2017-08-31 09:22:58 +00:00
### RSpec assertions
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
page.has_button?('Save')
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
expect(page).to have_no_button('Save')
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
In RSpec, you can use `page.should` assertions.
2015-05-05 16:00:33 +00:00
2017-08-30 17:43:19 +00:00
### About negatives
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
expect(page).to have_no_button('Save')
```
```ruby
expect(page).not_to have_button('Save')
2017-08-30 17:43:19 +00:00
```
2012-03-16 06:14:31 +00:00
The two above statements are functionally equivalent.
2017-08-30 17:43:19 +00:00
## RSpec
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
### Matchers
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
expect(page).to \
```
{: .-setup}
```ruby
have_current_path(expected_path)
2017-08-30 17:43:19 +00:00
have_selector '.blank-state'
have_selector 'h1#hola', text: 'Welcome'
2017-08-30 17:46:05 +00:00
have_button 'Save'
have_checked_field '#field'
2017-08-30 17:43:19 +00:00
have_unchecked_field
2017-08-30 17:46:05 +00:00
have_css '.class'
have_field '#field'
have_table '#table'
have_xpath '//div'
2017-08-30 17:43:19 +00:00
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
have_link 'Logout', href: logout_path
```
2012-03-16 06:14:31 +00:00
2017-08-30 17:43:19 +00:00
```ruby
have_select 'Language',
selected: 'German'
options: ['Engish', 'German']
with_options: ['Engish', 'German'] # partial match
```
2015-05-05 16:00:33 +00:00
2017-08-30 17:43:19 +00:00
```ruby
have_text 'Hello',
type: :visible # or :all
# alias: have_content
```
2015-06-10 12:24:12 +00:00
2017-08-30 17:43:19 +00:00
### Common options
2015-06-10 12:24:12 +00:00
2017-08-30 17:43:19 +00:00
All matchers have these options:
{: .-setup}
2013-08-24 07:43:10 +00:00
2017-08-30 17:43:19 +00:00
```ruby
2015-06-10 12:26:00 +00:00
text: 'welcome'
text: /Hello/
2015-06-10 12:24:12 +00:00
visible: true
count: 4
between: 2..5
minimum: 2
maximum: 5
2015-06-10 12:25:08 +00:00
wait: 10
2017-08-30 17:43:19 +00:00
```
2015-06-10 12:24:12 +00:00
2017-08-30 17:43:19 +00:00
## Other features
### Finding
```ruby
find(selector)
find_button(selector)
find_by_id(id)
find_field(selector)
find_link(selector)
locate
```
### Scoping
```ruby
within '#delivery' do
fill_in 'Street', with: 'Hello'
end
```
```ruby
within :xpath, '//article'
within_fieldset
within_table
within_frame
scope_to
```
```ruby
find('#x').fill_in('Street', with: 'Hello')
# same as within
```
### Scripting
```ruby
2019-09-12 16:36:31 +00:00
execute_script('$("input").trigger("change")')
2017-08-30 17:43:19 +00:00
evaluate_script('window.ga')
```
Executes JavaScript.
### Debugging
```ruby
save_and_open_page
```
Opens the webpage in your browser.
### Page
```ruby
page
.all('h3')
.body
.html
.source
.current_host
.current_path
.current_url
```
### AJAX
```ruby
using_wait_time 10 do
...
end
```
### Misc
drag
field_labeled
### Page object
2015-07-16 20:19:25 +00:00
2017-08-30 17:46:05 +00:00
```ruby
2015-07-16 20:19:25 +00:00
page.status_code == 200
page.response_headers
```
See: <https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Session>
2015-07-16 20:19:25 +00:00
2017-08-30 17:43:19 +00:00
### Poltergeist
2015-11-17 10:25:47 +00:00
2017-08-30 17:46:05 +00:00
```ruby
2015-11-17 10:25:47 +00:00
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.javascript_driver = :poltergeist
```
2017-08-30 17:43:19 +00:00
Use [poltergeist](https://github.com/teampoltergeist/poltergeist) to integrate PhantomJS.
2015-11-17 10:25:47 +00:00
### Blacklist
2017-08-30 17:46:05 +00:00
```ruby
2015-11-17 10:25:47 +00:00
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
```
### Debugging
Enable `inspector: true` and then:
2017-08-30 17:43:19 +00:00
{: .-setup}
2015-11-17 10:25:47 +00:00
2017-08-30 17:46:05 +00:00
```ruby
2015-11-17 10:25:47 +00:00
page.driver.debug
```
2017-08-30 17:43:19 +00:00
To pause execution for a while:
2015-11-17 10:25:47 +00:00
2017-08-30 17:46:05 +00:00
```ruby
2015-11-17 10:25:47 +00:00
page.driver.pause
```
## Selenium
### Accepting confirm() and alert()
2017-08-30 17:43:19 +00:00
```ruby
2015-11-17 10:25:47 +00:00
accept_alert { ... }
dismiss_confirm { ... }
accept_prompt(with: 'hi') { ... }
```
Alternatively:
2017-08-30 17:43:19 +00:00
```ruby
2015-11-17 10:25:47 +00:00
page.driver.browser.switch_to.alert.accept
```
### Updating session
2017-08-30 17:43:19 +00:00
```ruby
2015-11-17 10:25:47 +00:00
page.set_rack_session(foo: 'bar')
```
2015-07-16 20:19:25 +00:00
## See also
2017-08-30 17:43:19 +00:00
{: .-one-column}
2015-07-16 20:19:25 +00:00
- <https://rubydoc.info/github/jnicklas/capybara/Capybara/RSpecMatchers>
- <https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers>