kelp 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,497 @@
1
+ # web_steps.rb
2
+ #
3
+ # This file defines some generic step definitions that utilize the helper
4
+ # methods provided by Kelp. It is auto-generated by running:
5
+ #
6
+ # script/generate kelp
7
+ #
8
+ # from a Rails application. It's probably a good idea to avoid editing this
9
+ # file, since it may be overwritten by an upgraded kelp gem later. If you
10
+ # find any issues with these step definitions and think they can be improved,
11
+ # please create an issue on Github:
12
+ #
13
+ # http://github.com/wapcaplet/kelp/issues
14
+ #
15
+
16
+ require 'kelp'
17
+ World(Kelp::Attribute)
18
+ World(Kelp::Checkbox)
19
+ World(Kelp::Dropdown)
20
+ World(Kelp::Field)
21
+ World(Kelp::Helper)
22
+ World(Kelp::Navigation)
23
+ World(Kelp::Scoping)
24
+ World(Kelp::Visibility)
25
+
26
+
27
+ # Patterns shared by step definitions
28
+ I = /(?:|I )/
29
+ TEXT = /"([^\"]+)"/
30
+ REGEX = /\/([^\/]*)\//
31
+ WITHIN = /(?: within (.+))?/
32
+ ELEMENT = /(?:field|checkbox|dropdown|button)/
33
+
34
+
35
+ # ==========================
36
+ # NAVIGATION
37
+ # ==========================
38
+
39
+ # Load a given page path directly. `page_name` may be an absolute path,
40
+ # or an expression that is translated to an absolute path by your `path_to`
41
+ # function.
42
+ #
43
+ # Examples:
44
+ #
45
+ # Given I am on the home page
46
+ # When I go to my account page
47
+ # And I go to "/logout"
48
+ #
49
+ Given /^#{I}(am on|go to) (.+)$/ do |page_name|
50
+ visit path_to(page_name)
51
+ end
52
+
53
+
54
+ # Press a button on a webpage.
55
+ #
56
+ # Examples:
57
+ #
58
+ # When I press "Delete"
59
+ # And I press "Yes" within "#confirmation"
60
+ #
61
+ When /^#{I}press #{TEXT}#{WITHIN}$/ do |button, selector|
62
+ press(button, :within => selector)
63
+ end
64
+
65
+
66
+ # Click a link in a webpage.
67
+ #
68
+ # Examples:
69
+ #
70
+ # When I follow "List of stuff"
71
+ # And I follow "Next" within "navigation"
72
+ #
73
+ When /^#{I}follow #{TEXT}#{WITHIN}$/ do |link, selector|
74
+ follow(link, :within => selector)
75
+ end
76
+
77
+
78
+ # Click a link in a table row that contains the given text.
79
+ # This can be used to click the "Edit" link for a specific record.
80
+ #
81
+ # Examples:
82
+ #
83
+ # When I follow "Edit" next to "John"
84
+ #
85
+ When /^#{I}follow #{TEXT} next to #{TEXT}$/ do |link, next_to|
86
+ click_link_in_row(link, next_to)
87
+ end
88
+
89
+
90
+ # Verify that the current path name matches that of the given page.
91
+ #
92
+ Then /^#{I}should be on (.+)$/ do |page_name|
93
+ should_be_on_page(page_name)
94
+ end
95
+
96
+
97
+ # Verify that the current URL has the given query parameters.
98
+ #
99
+ # Examples:
100
+ #
101
+ # Then I should have the following query string:
102
+ # | fruit | apple |
103
+ # | color | red |
104
+ #
105
+ Then /^#{I}should have the following query string:$/ do |expected_pairs|
106
+ should_have_query(expected_pairs.rows_hash)
107
+ end
108
+
109
+
110
+ # Save the current page as an HTML file, and open it in your preferred browser.
111
+ # Requires that you have the `launchy` gem installed.
112
+ Then /^show me the page$/ do
113
+ save_and_open_page
114
+ end
115
+
116
+
117
+ # ==========================
118
+ # VISIBILITY
119
+ # ==========================
120
+
121
+
122
+ # Verify that the given string is visible on the webpage.
123
+ #
124
+ # Examples:
125
+ #
126
+ # Then I should see "Favorite color"
127
+ # But I should not see "Least favorite color"
128
+ #
129
+ Then /^#{I}should see #{TEXT}#{WITHIN}$/ do |text, selector|
130
+ should_see(text, :within => selector)
131
+ end
132
+
133
+ Then /^#{I}should not see #{TEXT}#{WITHIN}$/ do |text, selector|
134
+ should_not_see(text, :within => selector)
135
+ end
136
+
137
+
138
+ # Verify that the given regular expression matches some part of the webpage.
139
+ #
140
+ # Examples:
141
+ #
142
+ # Then I should see /Favorite (color|food)/
143
+ #
144
+ Then /^#{I}should see #{REGEX}#{WITHIN}$/ do |regexp, selector|
145
+ should_see(Regexp.new(regexp), :within => selector)
146
+ end
147
+
148
+ Then /^#{I}should not see #{REGEX}#{WITHIN}$/ do |regexp, selector|
149
+ should_not_see(Regexp.new(regexp), :within => selector)
150
+ end
151
+
152
+
153
+ # Verify the presence or absence of multiple text strings in the page,
154
+ # or within a given context.
155
+ #
156
+ # With `should see`, fails if any of the strings are missing.
157
+ # With `should not see`, fails if any of the strings are present.
158
+ #
159
+ # `items` may be a Cucumber table, or a multi-line string. Examples:
160
+ #
161
+ # Then I should see the following:
162
+ # | Apple crumble |
163
+ # | Banana cream pie |
164
+ # | Cherry tart |
165
+ #
166
+ # Then I should see the following:
167
+ # """
168
+ # Bacon & Eggs
169
+ # Biscuits & Gravy
170
+ # Hash Browns
171
+ # """
172
+ #
173
+ Then /^#{I}should see(?: the following)?#{WITHIN}:$/ do |selector, items|
174
+ should_see listify(items), :within => selector
175
+ end
176
+
177
+ Then /^#{I}should not see(?: the following)?#{WITHIN}:$/ do |selector, items|
178
+ should_not_see listify(items), :within => selector
179
+ end
180
+
181
+
182
+ # Verify that one or more table rows containing the correct values exist (or do
183
+ # not exist). Rows do not need to match exactly, and fields do not need to be
184
+ # in the same order.
185
+ #
186
+ # Examples:
187
+ #
188
+ # Then I should see table rows containing:
189
+ # | Eric | Edit |
190
+ # | John | Edit |
191
+ # And I should not see a table row containing:
192
+ # | Eric | Delete |
193
+ #
194
+ Then /^#{I}should see (?:a table row|table rows)#{WITHIN} containing:$/ do |selector, rows|
195
+ rows.raw.each do |fields|
196
+ should_see_in_same_row(fields, :within => selector)
197
+ end
198
+ end
199
+
200
+ Then /^#{I}should not see (?:a table row|table rows)#{WITHIN} containing:$/ do |selector, rows|
201
+ rows.raw.each do |fields|
202
+ should_not_see_in_same_row(fields, :within => selector)
203
+ end
204
+ end
205
+
206
+
207
+ # Verify that expected text exists or does not exist in the same row as
208
+ # some text. This can be used to ensure the presence or absence of "Edit"
209
+ # or "Delete" links, or specific data associated with a row in a table.
210
+ #
211
+ # Examples:
212
+ #
213
+ # Then I should see "Edit" next to "John"
214
+ # And I should not see "Delete" next to "John"
215
+ #
216
+ Then /^#{I}should see #{TEXT} next to #{TEXT}#{WITHIN}$/ do |text, next_to, selector|
217
+ should_see_in_same_row([text, next_to], :within => selector)
218
+ end
219
+
220
+ Then /^#{I}should not see #{TEXT} next to #{TEXT}#{WITHIN}$/ do |text, next_to, selector|
221
+ should_not_see_in_same_row([text, next_to], :within => selector)
222
+ end
223
+
224
+
225
+ # Verify that several expected text strings exist or do not exist in the same
226
+ # row as some text. Prevents multiple "should see X next to Y" calls. Similar
227
+ # to "should see a row containing", but targeted toward a specific row.
228
+ #
229
+ # Examples:
230
+ #
231
+ # Then I should see the following next to "Terry":
232
+ # | Copy |
233
+ # | Edit |
234
+ # | Delete |
235
+ #
236
+ # Then I should see the following next to "John":
237
+ # """
238
+ # Copy
239
+ # Edit
240
+ # Delete
241
+ # """
242
+ #
243
+ Then /^#{I}should see the following next to #{TEXT}#{WITHIN}:$/ do |next_to, selector, items|
244
+ should_see_in_same_row(listify(items) + [next_to], :within => selector)
245
+ end
246
+
247
+ Then /^#{I}should not see the following next to #{TEXT}#{WITHIN}:$/ do |next_to, selector, items|
248
+ should_not_see_in_same_row(listify(items) + [next_to], :within => selector)
249
+ end
250
+
251
+
252
+ # Verify that a given button is or isn't visible on the webpage.
253
+ #
254
+ # Examples:
255
+ #
256
+ # Then I should see a "Save" button
257
+ # But I should not see a "Delete" button
258
+ #
259
+ Then /^#{I}should see a #{TEXT} button#{WITHIN}$/ do |button, selector|
260
+ should_see_button(button, :within => selector)
261
+ end
262
+
263
+ Then /^#{I}should not see a #{TEXT} button#{WITHIN}$/ do |button, selector|
264
+ should_not_see_button(button, :within => selector)
265
+ end
266
+
267
+
268
+ # ==========================
269
+ # FORMS
270
+ # ==========================
271
+
272
+
273
+ # Fill in a form field with a given value.
274
+ #
275
+ # Examples:
276
+ #
277
+ # When I fill in "Favorite color" with "Green"
278
+ #
279
+ When /^#{I}fill in #{TEXT} with #{TEXT}#{WITHIN}$/ do |field, value, selector|
280
+ fill_in_field(field, value, :within => selector)
281
+ end
282
+
283
+
284
+ # Fill in a form field with a given value.
285
+ #
286
+ # Examples:
287
+ #
288
+ # When I fill in "Green" for "Favorite color"
289
+ #
290
+ When /^#{I}fill in #{TEXT} for #{TEXT}#{WITHIN}$/ do |value, field, selector|
291
+ fill_in_field(field, value, :within => selector)
292
+ end
293
+
294
+
295
+ # Fill in multiple form fields from values in a table.
296
+ # The fields may be textboxes, dropdowns/listboxes, or checkboxes.
297
+ #
298
+ # Examples:
299
+ #
300
+ # When I fill in the following:
301
+ # | First name | Alton |
302
+ # | Last name | Brown |
303
+ # | Hair | blonde |
304
+ # | Has TV show | checked |
305
+ #
306
+ When /^#{I}fill in the following#{WITHIN}:$/ do |fields, selector|
307
+ fill_in_fields(fields.rows_hash, :within => selector)
308
+ end
309
+
310
+
311
+ # Select a value from a dropdown.
312
+ When /^#{I}select #{TEXT} from #{TEXT}$/ do |value, field|
313
+ select value, :from => field
314
+ end
315
+
316
+
317
+ # Check a checkbox.
318
+ When /^#{I}check #{TEXT}$/ do |field|
319
+ check field
320
+ end
321
+
322
+
323
+ # Uncheck a checkbox.
324
+ When /^#{I}uncheck #{TEXT}$/ do |field|
325
+ uncheck field
326
+ end
327
+
328
+
329
+ # Choose a given radio button.
330
+ When /^#{I}choose #{TEXT}$/ do |field|
331
+ choose field
332
+ end
333
+
334
+
335
+ # Verify that a given field is empty or nil.
336
+ #
337
+ # Examples:
338
+ #
339
+ # Then the "First name" field should be empty
340
+ #
341
+ Then /^the #{TEXT} field#{WITHIN} should be empty$/ do |field, selector|
342
+ field_should_be_empty(field, :within => selector)
343
+ end
344
+
345
+
346
+ # Verify that a text field in a form does or doesn't contain a value.
347
+ #
348
+ # Examples:
349
+ #
350
+ # Then the "Last name" field should contain "Brown"
351
+ # But the "First name" field should not contain "Leroy"
352
+ #
353
+ Then /^the #{TEXT} field#{WITHIN} should contain #{TEXT}$/ do |field, selector, value|
354
+ field_should_contain(field, value, :within => selector)
355
+ end
356
+
357
+ Then /^the #{TEXT} field#{WITHIN} should not contain #{TEXT}$/ do |field, selector, value|
358
+ field_should_not_contain(field, value, :within => selector)
359
+ end
360
+
361
+
362
+ # Verify that a dropdown has a given value selected. This verifies the visible
363
+ # value shown to the user, rather than the value attribute of the selected
364
+ # option element.
365
+ #
366
+ # Examples:
367
+ #
368
+ # Then the "Height" dropdown should equal "Average"
369
+ #
370
+ Then /^the #{TEXT} dropdown#{WITHIN} should equal #{TEXT}$/ do |dropdown, selector, value|
371
+ dropdown_should_equal(dropdown, value, :within => selector)
372
+ end
373
+
374
+
375
+ # Verify that a dropdown includes or doesn't include the given value.
376
+ #
377
+ # Examples:
378
+ #
379
+ # Then the "Height" dropdown should include "Tall"
380
+ #
381
+ Then /^the #{TEXT} dropdown#{WITHIN} should include #{TEXT}$/ do |dropdown, selector, value|
382
+ dropdown_should_include(dropdown, value, :within => selector)
383
+ end
384
+
385
+ Then /^the #{TEXT} dropdown#{WITHIN} should not include #{TEXT}$/ do |dropdown, selector, value|
386
+ dropdown_should_not_include(dropdown, value, :within => selector)
387
+ end
388
+
389
+
390
+ # Verify that a dropdown includes or doesn't include all values in the given
391
+ # table or multiline string.
392
+ #
393
+ # Examples:
394
+ #
395
+ # Then the "Height" dropdown should include:
396
+ # | Short |
397
+ # | Average |
398
+ # | Tall |
399
+ #
400
+ # Then the "Favorite Colors" dropdown should include:
401
+ # """
402
+ # Red
403
+ # Green
404
+ # Blue
405
+ # """
406
+ #
407
+ Then /^the #{TEXT} dropdown#{WITHIN} should include:$/ do |dropdown, selector, values|
408
+ dropdown_should_include(dropdown, listify(values), :within => selector)
409
+ end
410
+
411
+ Then /^the #{TEXT} dropdown#{WITHIN} should not include:$/ do |dropdown, selector, values|
412
+ dropdown_should_not_include(dropdown, listify(values), :within => selector)
413
+ end
414
+
415
+
416
+ # Verify multiple fields in a form, optionally restricted to a given selector.
417
+ # Fields may be text inputs or dropdowns.
418
+ #
419
+ # Examples:
420
+ #
421
+ # Then the fields should contain:
422
+ # | First name | Eric |
423
+ # | Last name | Pierce |
424
+ #
425
+ Then /^the fields#{WITHIN} should contain:$/ do |selector, fields|
426
+ fields_should_contain_within(selector, fields.rows_hash)
427
+ end
428
+
429
+
430
+ # Verify that a checkbox is checked or unchecked.
431
+ # "should not be checked" and "should be unchecked" are equivalent, and
432
+ # "should be checked" and "should not be unchecked" are equivalent.
433
+ #
434
+ # Examples:
435
+ #
436
+ # Then the "I like apples" checkbox should be checked
437
+ # And the "I like celery" checkbox should not be checked
438
+ #
439
+ Then /^the #{TEXT} checkbox#{WITHIN} should be (checked|unchecked)$/ do |checkbox, selector, state|
440
+ if state == 'checked'
441
+ checkbox_should_be_checked(checkbox, :within => selector)
442
+ else
443
+ checkbox_should_not_be_checked(checkbox, :within => selector)
444
+ end
445
+ end
446
+
447
+ Then /^the #{TEXT} checkbox#{WITHIN} should not be (checked|unchecked)$/ do |checkbox, selector, state|
448
+ if state == 'unchecked'
449
+ checkbox_should_be_checked(checkbox, :within => selector)
450
+ else
451
+ checkbox_should_not_be_checked(checkbox, :within => selector)
452
+ end
453
+ end
454
+
455
+
456
+ # Verify that a checkbox in a certain table row is checked or unchecked.
457
+ # "should not be checked" and "should be unchecked" are equivalent, and
458
+ # "should be checked" and "should not be unchecked" are equivalent.
459
+ #
460
+ # Examples:
461
+ #
462
+ # Then the "Like" checkbox next to "Apple" should be checked
463
+ # And the "Like" checkbox next to "Banana" should be unchecked
464
+ #
465
+ Then /^the #{TEXT} checkbox next to #{TEXT}#{WITHIN} should be (checked|unchecked)$/ do |checkbox, next_to, selector, state|
466
+
467
+ within(:xpath, xpath_row_containing(next_to)) do
468
+ if state == 'checked'
469
+ checkbox_should_be_checked(checkbox, :within => selector)
470
+ else
471
+ checkbox_should_not_be_checked(checkbox, :within => selector)
472
+ end
473
+ end
474
+ end
475
+
476
+ Then /^the #{TEXT} checkbox next to #{TEXT}#{WITHIN} should not be (checked|unchecked)$/ do |checkbox, next_to, selector, state|
477
+
478
+ within(:xpath, xpath_row_containing(next_to)) do
479
+ if state == 'unchecked'
480
+ checkbox_should_be_checked(checkbox, :within => selector)
481
+ else
482
+ checkbox_should_not_be_checked(checkbox, :within => selector)
483
+ end
484
+ end
485
+ end
486
+
487
+
488
+ # Attach the filename at the given path to the given form field.
489
+ #
490
+ # Examples:
491
+ #
492
+ # When I attach the file "tmp/my_data.csv" to "CSV file"
493
+ #
494
+ When /^#{I}attach the file #{TEXT} to #{TEXT}$/ do |path, field|
495
+ attach_file field, File.expand_path(path)
496
+ end
497
+
data/spec/field_spec.rb CHANGED
@@ -193,17 +193,33 @@ describe Kelp::Field, "fill_in_field" do
193
193
  end
194
194
 
195
195
  context "passes when" do
196
- it "filling a single field by id" do
196
+ it "filling a single text field by id" do
197
197
  fill_in_field "first_name", "Mel"
198
198
  field_should_contain "first_name", "Mel"
199
199
  end
200
200
 
201
- it "filling a single field by label" do
201
+ it "filling a single text field by label" do
202
202
  fill_in_field "First name", "Mel"
203
203
  field_should_contain "First name", "Mel"
204
204
  end
205
205
 
206
- it "filling a single field by id within a scope" do
206
+ it "checking a checkbox" do
207
+ fill_in_field "I like cheese", "checked"
208
+ checkbox_should_be_checked "I like cheese"
209
+ fill_in_field "I like cheese", "unchecked"
210
+ checkbox_should_not_be_checked "I like cheese"
211
+ end
212
+
213
+ it "selecting from a dropdown" do
214
+ fill_in_field "Height", "Short"
215
+ dropdown_should_equal "Height", "Short"
216
+ fill_in_field "Height", "Average"
217
+ dropdown_should_equal "Height", "Average"
218
+ fill_in_field "Height", "Tall"
219
+ dropdown_should_equal "Height", "Tall"
220
+ end
221
+
222
+ it "filling a single text field by id within a scope" do
207
223
  fill_in_field_within "#person_form", "first_name", "Mel"
208
224
  field_should_contain_within "#person_form", "first_name", "Mel"
209
225
  end
@@ -216,9 +232,15 @@ describe Kelp::Field, "fill_in_field" do
216
232
  end.should raise_error(Capybara::ElementNotFound)
217
233
  end
218
234
 
235
+ it "selecting a nonexistent value from a dropdown" do
236
+ lambda do
237
+ fill_in_field "Height", "Gigantic"
238
+ end.should raise_error(Capybara::ElementNotFound)
239
+ end
240
+
219
241
  it "filling a field in the wrong scope" do
220
242
  lambda do
221
- fill_in_field_within "#other_form", "First name", "Mel"
243
+ fill_in_field_within "#preferences_form", "First name", "Mel"
222
244
  end.should raise_error(Capybara::ElementNotFound)
223
245
  end
224
246
  end
@@ -257,6 +279,24 @@ describe Kelp::Field, "fill_in_fields" do
257
279
  field_should_contain "Last name", "Brooks"
258
280
  end
259
281
 
282
+ it "filling text fields, dropdowns, and checkboxes by label" do
283
+ fill_in_fields \
284
+ "First name" => "Mel",
285
+ "Last name" => "Brooks",
286
+ "Height" => "Short",
287
+ "Weight" => "Medium",
288
+ "I like salami" => "checked",
289
+ "I like cheese" => "unchecked",
290
+ "Message" => "I can't make decisions, I'm a president!"
291
+ field_should_contain "First name", "Mel"
292
+ field_should_contain "Last name", "Brooks"
293
+ dropdown_should_equal "Height", "Short"
294
+ dropdown_should_equal "Weight", "Medium"
295
+ checkbox_should_be_checked "I like salami"
296
+ checkbox_should_not_be_checked "I like cheese"
297
+ field_should_contain "Message", "I'm a president!"
298
+ end
299
+
260
300
  it "filling a single field by id within a scope" do
261
301
  fill_in_fields_within "#person_form", "first_name" => "Mel"
262
302
  field_should_contain "first_name", "Mel", :within => "#person_form"
@@ -281,7 +321,7 @@ describe Kelp::Field, "fill_in_fields" do
281
321
 
282
322
  it "filling a field in the wrong scope" do
283
323
  lambda do
284
- fill_in_fields_within "#other_form",
324
+ fill_in_fields_within "#preferences_form",
285
325
  "First name" => "Mel"
286
326
  end.should raise_error(Capybara::ElementNotFound)
287
327
  end
@@ -289,4 +329,3 @@ describe Kelp::Field, "fill_in_fields" do
289
329
 
290
330
  end
291
331
 
292
-
@@ -41,11 +41,11 @@ describe Kelp::Navigation, "press" do
41
41
 
42
42
  context "passes when" do
43
43
  it "button exists" do
44
- press "Submit"
44
+ press "Submit message"
45
45
  end
46
46
 
47
47
  it "button exists within the scope" do
48
- press "Submit", :within => "#fields"
48
+ press "Submit message", :within => "#fields"
49
49
  end
50
50
  end
51
51
 
@@ -58,7 +58,7 @@ describe Kelp::Navigation, "press" do
58
58
 
59
59
  it "button exists but is not within the scope" do
60
60
  lambda do
61
- press "Submit", :within => "#greeting"
61
+ press "Submit message", :within => "#greeting"
62
62
  end.should raise_error(Capybara::ElementNotFound)
63
63
  end
64
64
  end
@@ -87,3 +87,46 @@ describe Kelp::Navigation, "click_link_in_row" do
87
87
  end
88
88
 
89
89
 
90
+ describe Kelp::Navigation, "should_be_on_page" do
91
+ context "passes when" do
92
+ it "current path matches the expected path" do
93
+ visit('/home')
94
+ should_be_on_page '/home'
95
+ visit('/form')
96
+ should_be_on_page '/form'
97
+ end
98
+ end
99
+
100
+ context "fails when" do
101
+ it "current path does not match the expected path" do
102
+ visit('/home')
103
+ lambda do
104
+ should_be_on_page '/form'
105
+ end.should raise_error(rspec_unexpected)
106
+ end
107
+ end
108
+ end
109
+
110
+
111
+ describe Kelp::Navigation, "should_have_query" do
112
+ before(:each) do
113
+ visit('/form')
114
+ end
115
+
116
+ context "passes when" do
117
+ it "actual and expected queries are empty" do
118
+ empty_params = {}
119
+ should_have_query empty_params
120
+ end
121
+ end
122
+
123
+ context "fails when" do
124
+ it "expected params when actual query is empty" do
125
+ lambda do
126
+ should_have_query :username => 'tony'
127
+ end.should raise_error(rspec_unexpected)
128
+ end
129
+ end
130
+ end
131
+
132
+