capybara_test_helpers 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +46 -42
- data/lib/capybara_test_helpers/assertions.rb +8 -1
- data/lib/capybara_test_helpers/matchers.rb +2 -0
- data/lib/capybara_test_helpers/rspec.rb +1 -1
- data/lib/capybara_test_helpers/version.rb +1 -1
- data/lib/generators/test_helper/test_helper_generator.rb +2 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e4f6226edd7ecb46f2d8376f129d26e8ee0156ed0df1fd1083881a224489da8
|
4
|
+
data.tar.gz: da483ed245cd972f66b291e31c5345bf9b451c8f26c866284d7f35892043a4b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66ee90ceda7582b79b2ffc905f875422ad7122a3a9a29c589b9df2bd63417ee81c94f6fa66c2d26568ff8cb01e835c8dcc30e4e7970e6ece174376941f07ac39
|
7
|
+
data.tar.gz: c75546cc4a3ea671b22ccf685fba84a2767f3d0ac2e652b214f144eb28ea0a284b8f38198596b03c487f81d49bea0e447ea608b0deeaf42bf2f19d46a3fcae5a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
Capybara Test Helpers
|
3
3
|
<p align="center">
|
4
4
|
<a href="https://github.com/ElMassimo/capybara_test_helpers/actions"><img alt="Build Status" src="https://github.com/ElMassimo/capybara_test_helpers/workflows/build/badge.svg"/></a>
|
5
|
-
<a href="https://inch-ci.org/github/ElMassimo/capybara_test_helpers"><img alt="Inline docs" src="https://inch-ci.org/github/ElMassimo/capybara_test_helpers.svg"/></a>
|
6
5
|
<a href="https://codeclimate.com/github/ElMassimo/capybara_test_helpers"><img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/capybara_test_helpers/badges/gpa.svg"/></a>
|
7
6
|
<a href="https://codeclimate.com/github/ElMassimo/capybara_test_helpers"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/capybara_test_helpers/badges/coverage.svg"/></a>
|
8
7
|
<a href="https://rubygems.org/gems/capybara_test_helpers"><img alt="Gem Version" src="https://img.shields.io/gem/v/capybara_test_helpers.svg?colorB=e9573f"/></a>
|
@@ -14,8 +13,9 @@ Capybara Test Helpers
|
|
14
13
|
an opinionated library built on top of [capybara], that encourages good testing
|
15
14
|
practices based on encapsulation and reuse.
|
16
15
|
|
17
|
-
Write tests that everyone can understand, and leverage your Ruby skills to keep
|
16
|
+
Write tests that everyone can understand, and leverage your Ruby skills to keep them __easy to read and easy to change__.
|
18
17
|
|
18
|
+
[docs]: https://capybara-test-helpers.netlify.app/
|
19
19
|
[capybara]: https://github.com/teamcapybara/capybara
|
20
20
|
[capybara dsl]: https://github.com/teamcapybara/capybara#the-dsl
|
21
21
|
[capybara querying]: https://github.com/teamcapybara/capybara#querying
|
@@ -27,7 +27,7 @@ Write tests that everyone can understand, and leverage your Ruby skills to keep
|
|
27
27
|
[testing_robots]: https://jakewharton.com/testing-robots/
|
28
28
|
[page_objects]: https://martinfowler.com/bliki/PageObject.html
|
29
29
|
[rspec_injection]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/spec/system/cities_spec.rb#L7
|
30
|
-
[rspec_global_injection]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/spec/support/default_test_helpers.rb#L8
|
30
|
+
[rspec_global_injection]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/spec/system/support/default_test_helpers.rb#L8
|
31
31
|
[cucumber_injection]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/features/step_definitions/city_steps.rb#L3
|
32
32
|
[example app]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app
|
33
33
|
[capybara_test_helpers_tests]: https://github.com/ElMassimo/capybara_test_helpers/blob/master/spec
|
@@ -37,6 +37,8 @@ Write tests that everyone can understand, and leverage your Ruby skills to keep
|
|
37
37
|
[rails_integration]: https://github.com/ElMassimo/capybara_test_helpers/commit/c512e39987215e30227dad45e775480bc1348325
|
38
38
|
[cucumber_integration]: https://github.com/ElMassimo/capybara_test_helpers/commit/68e20cb40ba409c50f88f8b745eb908fb067a0aa
|
39
39
|
|
40
|
+
Check out the [documentation site][docs] for more information.
|
41
|
+
|
40
42
|
## Why? 🤔
|
41
43
|
|
42
44
|
[`capybara`][capybara] is a great library for integration tests in Ruby,
|
@@ -124,65 +126,63 @@ from Cucumber to RSpec.
|
|
124
126
|
You can define a test helper by subclassing `Capybara::TestHelper`, which has
|
125
127
|
full access to the Capybara DSL.
|
126
128
|
|
127
|
-
When using Rails, you can generate a test helper by running:
|
128
|
-
|
129
|
-
rails g test_helper users
|
130
|
-
|
131
129
|
```ruby
|
132
|
-
class
|
130
|
+
class CitiesTestHelper < Capybara::TestHelper
|
131
|
+
use_test_helpers(:form, :table)
|
132
|
+
|
133
133
|
# Selectors: Semantic aliases for elements, a useful abstraction.
|
134
134
|
SELECTORS = {
|
135
|
-
el: 'table.
|
136
|
-
form: '.user-form',
|
137
|
-
submit_button: [:button, type: 'submit'],
|
135
|
+
el: 'table.cities',
|
138
136
|
}
|
139
137
|
|
140
138
|
# Getters: A convenient way to get related data or nested elements.
|
141
|
-
def row_for(
|
142
|
-
within {
|
139
|
+
def row_for(city)
|
140
|
+
within { table.row_for(city.name) }
|
143
141
|
end
|
144
142
|
|
145
143
|
# Actions: Encapsulate complex actions to provide a cleaner interface.
|
146
|
-
def add(
|
147
|
-
click_on('
|
148
|
-
|
144
|
+
def add(**args)
|
145
|
+
click_on('New City')
|
146
|
+
save_city(**args)
|
147
|
+
yield(form) if block_given?
|
149
148
|
end
|
150
149
|
|
151
|
-
def edit(
|
152
|
-
row_for(
|
153
|
-
|
150
|
+
def edit(city, with:)
|
151
|
+
row_for(city).click_on('Edit')
|
152
|
+
save_city(**with)
|
154
153
|
end
|
155
154
|
|
156
|
-
def delete(
|
157
|
-
row_for(
|
158
|
-
accept_confirm
|
155
|
+
def delete(city)
|
156
|
+
accept_confirm { row_for(city).click_on('Destroy') }
|
159
157
|
end
|
160
158
|
|
161
159
|
private \
|
162
|
-
def
|
163
|
-
within
|
164
|
-
fill_in
|
165
|
-
|
166
|
-
submit_button.click
|
160
|
+
def save_city(name:)
|
161
|
+
form.within {
|
162
|
+
fill_in 'Name', with: name
|
163
|
+
form.save
|
167
164
|
}
|
168
165
|
end
|
169
166
|
|
170
|
-
# Assertions:
|
171
|
-
def
|
172
|
-
|
173
|
-
|
167
|
+
# Assertions: Check on element properties, used with `should` and `should_not`.
|
168
|
+
def have_city(name)
|
169
|
+
within { have(:table_row, { 'Name' => name }) }
|
170
|
+
end
|
171
|
+
|
172
|
+
# Background: Helpers to add/modify/delete data in the database or session.
|
173
|
+
def given_there_is_a_city(name)
|
174
|
+
City.create!(name: name)
|
174
175
|
end
|
175
176
|
end
|
176
177
|
```
|
177
178
|
|
178
|
-
|
179
|
-
or [call `use_test_helpers` in Cucumber step definitions][cucumber_injection].
|
179
|
+
When using Rails, you can generate a test helper by running:
|
180
180
|
|
181
|
-
|
181
|
+
$ rails g test_helper users
|
182
182
|
|
183
183
|
### Writing a Test with Helpers ✅
|
184
184
|
|
185
|
-
You can find [this working example](https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/spec/
|
185
|
+
You can find [this working example](https://github.com/ElMassimo/capybara_test_helpers/blob/master/examples/rails_app/spec/system/cities_spec.rb) and more in the [example app] and the [Capybara tests][capybara_test_helpers_tests].
|
186
186
|
|
187
187
|
```ruby
|
188
188
|
require 'rails_helper'
|
@@ -216,9 +216,16 @@ RSpec.describe 'Cities', test_helpers: [:cities] do
|
|
216
216
|
end
|
217
217
|
```
|
218
218
|
|
219
|
+
To make the test helper available you can use the [`test_helpers` option][rspec_injection]
|
220
|
+
in a `describe`, `context` or `scenario` as seen above.
|
221
|
+
|
222
|
+
When using Cucumber, you may call [`use_test_helpers`][cucumber_injection] in the step definitions.
|
223
|
+
|
224
|
+
Finally, for test helpers that you expect to use very often, you can [`use_test_helpers`][rspec_global_injection] in an RSpec helper module to make them available globally.
|
225
|
+
|
219
226
|
## DSL 🛠
|
220
227
|
|
221
|
-
A documentation website with the full API and examples is
|
228
|
+
A [documentation website][docs] with the full API and examples is [now available][docs] :shipit:
|
222
229
|
|
223
230
|
Every single method in the [Capybara DSL] is available inside test helpers, as
|
224
231
|
well as the [built-in RSpec matchers][rspec matchers].
|
@@ -228,8 +235,8 @@ well as the [built-in RSpec matchers][rspec matchers].
|
|
228
235
|
You can encapsulate locators for commonly used elements to avoid hardcoding them
|
229
236
|
in different tests.
|
230
237
|
|
231
|
-
As a result, if the
|
232
|
-
|
238
|
+
As a result, if the UI changes there are less places that need to be updated in
|
239
|
+
the tests 😃
|
233
240
|
|
234
241
|
```ruby
|
235
242
|
class FormTestHelper < BaseTestHelper
|
@@ -327,13 +334,10 @@ they are both available because we are using an existing assertion.
|
|
327
334
|
#### Advanced Assertions ⚙️
|
328
335
|
|
329
336
|
Sometimes built-in assertions are not enough, and you need to use an expectation
|
330
|
-
directly. Test helpers provide
|
331
|
-
technique described in this post][positive and negative assertions] that you
|
337
|
+
directly. Test helpers provide [`to_or` and `not_to` methods][positive and negative assertions] that you
|
332
338
|
can use to implement an assertion that you can use with `should` or `should_not`.
|
333
339
|
|
334
340
|
```ruby
|
335
|
-
# frozen_string_literal: true
|
336
|
-
|
337
341
|
class CurrentPageTestHelper < BaseTestHelper
|
338
342
|
# Getters: A convenient way to get related data or nested elements.
|
339
343
|
def fullscreen?
|
@@ -93,7 +93,14 @@ module CapybaraTestHelpers::Assertions
|
|
93
93
|
CapybaraTestHelpers.define_helper_method(self, method_name, target: :page, assertion: true, inject_test_helper: false)
|
94
94
|
end
|
95
95
|
|
96
|
-
|
96
|
+
# Public: Allows to call have_selector with a shorter syntax.
|
97
|
+
def have(*args, **kwargs, &filter)
|
98
|
+
if args.first.is_a?(Integer)
|
99
|
+
::RSpec::CollectionMatchers::Have.new(*args, **kwargs)
|
100
|
+
else
|
101
|
+
have_selector(*args, **kwargs, &filter)
|
102
|
+
end
|
103
|
+
end
|
97
104
|
|
98
105
|
# Public: Allows to check on any input value asynchronously.
|
99
106
|
def have_value(expected_value, **options)
|
@@ -26,7 +26,7 @@ RSpec.configure do |config|
|
|
26
26
|
}
|
27
27
|
|
28
28
|
# Allow injecting test helpers in a feature or scenario.
|
29
|
-
|
29
|
+
types.each do |type|
|
30
30
|
config.include(CapybaraTestHelpers::DependencyInjection, type: type)
|
31
31
|
config.before(:each, type: type, &inject_test_helpers)
|
32
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara_test_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -38,7 +38,8 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
41
|
+
description: Write tests that everyone can understand, and leverage your Ruby skills
|
42
|
+
to keep them easy to read and easy to change.
|
42
43
|
email:
|
43
44
|
- maximomussini@gmail.com
|
44
45
|
executables: []
|