-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathCapybara.rb
More file actions
242 lines (237 loc) · 6.83 KB
/
Copy pathCapybara.rb
File metadata and controls
242 lines (237 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
cheatsheet do
title 'Capybara'
docset_file_name 'Capybara'
keyword 'capybara'
source_url 'http://cheat.kapeli.com'
category do
id 'Navigation'
entry do
name 'Visit a path'
notes '
```ruby
visit \'/blog\'
```
Navigates to a particular path. Pass a string or uses one of Rails path helpers.'
end
entry do
name 'Click on'
notes '
```ruby
click_on \'Sign in\'
```
Clicks an anchor tag, button, or input with type submit. Pass a string containing the anchor text.'
end
end
category do
id 'Page Interaction and Scoping'
entry do
name 'Has CSS?'
notes '
```ruby
page.has_css?(\'nav[data-role=\"primary-navigation\"] li\',text: \'FAQ\')
```
Returns a boolean value reporting whether a specific selector is present on this page.'
end
entry do
name 'Within'
notes '
```ruby
within(\'footer\'){expect(page).to have_content(\'Copyright\')}
```
Will scope interaction to within a particular selector. Useful if you\'re looking for content in a particular area.'
end
entry do
name 'Has content?'
notes '
```ruby
page.has_content?(\'Sign in\')
```
Returns a boolean value reporting whether specific content is present on the page.'
end
entry do
name 'Find'
notes '
```ruby
page.find(\'.todos li:first-child\')
```
Returns a single Capybara::Node::Element instance from the page.'
end
entry do
name 'All'
notes '
```ruby
page.all(\'.todos li:nth-of_type(odd)\')
```
Returns an array of Capybara::Node::Element instance from the page.'
end
end
category do
id 'Page Assertions'
entry do
name 'Have CSS'
notes '
```ruby
expect(page).to have_css(\'header\')
expect(page).to have_css(\'table#records + .pagination a[rel="next"\')
```
Asserts that a certain selector is present on the page.'
end
entry do
name 'Have content'
notes '
```ruby
expect(page).to have_content(\'What are you looking for?\')
```
Asserts that certain text is present on the page.'
end
entry do
name 'Current URL path comparison'
notes '
```ruby
expect(page).to have_current_path(prefix_path)
```
'
end
end
category do
id 'Node Interactions'
entry do
name 'Click'
notes '
```ruby
find(\'article a.title\').click
```
Triggers a click on a Capybara::Element. Works with JavaScript drivers.'
end
entry do
name 'Trigger'
notes '
```ruby
find(\'input[name="post[title]"]\').trigger(\'focus\')
```
Allows triggering of custom events. Works with JavaScript drivers.'
end
entry do
name 'Visible?'
notes '
```ruby
find(\'.navigation\').visible?
```
Returns a boolean value reporting if the Capybara::Element is visible.'
end
end
category do
id 'Form Interactions'
entry do
name 'Fill in'
notes '
```ruby
fill_in \'Title\', with: \'I love Rails!\'
fill_in \'post[title]\', with: \'I love Rails!\'
```
Fills in fields for you. Pass the label text or the name of the input.'
end
entry do
name 'Choose'
notes '
```ruby
choose \'Male\'
```
Chooses a radio button. Pass the label text.'
end
entry do
name 'Check'
notes '
```ruby
check \'I accept the terms of the site\'
```
Checks a checkbox. Pass the label text.'
end
entry do
name 'Uncheck'
notes '
```ruby
uncheck \'Admin access?\'
```
Unchecks a checkbox. Pass the label text.'
end
entry do
name 'Select'
notes '
```ruby
select \'MA\', from: \'State\'
```
Selects an option from a select tag.'
end
entry do
name 'Attach file'
notes '
```ruby
attach_file \'Image\', \'path/to/image.jpg\'
```
Attaches a file.'
end
entry do
name 'Click button'
notes '
```ruby
click_button \'Create My Account\'
```
Will press a button or `input[type=\'submit\']`'
end
end
category do
id 'Debugging'
entry do
name 'Save and open page'
notes '
```ruby
save_and_open_page
```
Will save the current page (typically to `Rails.root/tmp`) and attemps to open the HTML in the default web browser.'
end
entry do
name 'Save and open screenshot'
notes '
```ruby
save_and_open_screenshot
```
Save a screenshot of the current page and open in the default image viewer.'
end
end
category do
id 'Waiting...'
entry do
name "Capybara automatically waits for asynchronous operations to complete.
When you try to find an element that isn't on the page, it waits and retries until it is there, or a timeout duration elapses.
The wait time is defined at ```Capybara.default_max_wait_time```."
notes 'Thanks to [this gist](https://gist.github.com/zhengjia/428105#gistcomment-1413436) and [this presentation](https://speakerdeck.com/timmoore/embracing-capybara)'
end
entry do
name 'Methods that wait'
notes '
- ```find(selector)```, ```find_field```, ```find_link```
- ```within(selector)(scoping)```
- ```has_selector?```, ```has_no_selector?``` & assertions
- form & link actions
- ```click_link```, ```click_button```
- ```fill_in```
- ```check/uncheck```, ```select```, ```choose```
'
end
entry do
name "Methods that don't wait"
notes '
- ```visit```
- ```current_path```
- ```all(selector)```
- ```first(selector)```
- ```execute_script```
- simple accessors: ```text```, ```value```, ```title```, etc.
'
end
end
notes <<-'END'
* Based on a [cheat sheet](https://learn.thoughtbot.com/test-driven-rails-resources/capybara.pdf) by [thoughtbot](http://thoughtbot.com/).
END
end