capybara-lockstep 0.7.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +15 -1
- data/CHANGELOG.md +60 -20
- data/Gemfile.lock +2 -4
- data/README.md +146 -140
- data/capybara-lockstep.gemspec +4 -0
- data/lib/capybara-lockstep/capybara_ext.rb +4 -4
- data/lib/capybara-lockstep/configuration.rb +26 -8
- data/lib/capybara-lockstep/errors.rb +1 -1
- data/lib/capybara-lockstep/helper.js +21 -50
- data/lib/capybara-lockstep/lockstep.rb +24 -5
- data/lib/capybara-lockstep/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 271ed22154f61a70a3961eb2bd2a8a7d29430dfb37107b9f1176257e8efab320
|
4
|
+
data.tar.gz: 6ba0d7a5c7e8bff779ee2a9ac2d6d32829374041dff41d7bddd9cbd19627a468
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24f098f75fc9093065cf4210eed7fab3b8f1ea69d49d6e328f195f92affe41c262fa92daecbf1ee8247dba8bcad1d3d5c0941766bd3889416be6aedf79f3e354
|
7
|
+
data.tar.gz: ea3debf0f2e4178dbc200c84a98aae87a614914de889769d10ed1cc2e311f83e93de460e2b1a4c163b756dc331838164bfe7ae8f77f676833df93a984a2fda49
|
data/.github/workflows/test.yml
CHANGED
@@ -7,9 +7,13 @@ on:
|
|
7
7
|
pull_request:
|
8
8
|
branches:
|
9
9
|
- master
|
10
|
+
workflow_dispatch:
|
11
|
+
branches:
|
12
|
+
- master
|
10
13
|
jobs:
|
11
14
|
test:
|
12
15
|
runs-on: ubuntu-20.04
|
16
|
+
timeout-minutes: 3
|
13
17
|
strategy:
|
14
18
|
fail-fast: false
|
15
19
|
matrix:
|
@@ -24,6 +28,12 @@ jobs:
|
|
24
28
|
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
25
29
|
steps:
|
26
30
|
- uses: actions/checkout@v2
|
31
|
+
- name: Install Chrome
|
32
|
+
uses: browser-actions/setup-chrome@latest
|
33
|
+
- name: Show Chrome version
|
34
|
+
run: chrome --version
|
35
|
+
- name: Install ChromeDriver
|
36
|
+
uses: nanasess/setup-chromedriver@master
|
27
37
|
- name: Install ruby
|
28
38
|
uses: ruby/setup-ruby@v1
|
29
39
|
with:
|
@@ -33,4 +43,8 @@ jobs:
|
|
33
43
|
gem install bundler:2.2.15
|
34
44
|
bundle install --no-deployment
|
35
45
|
- name: Run tests
|
36
|
-
|
46
|
+
uses: nick-invision/retry@v2
|
47
|
+
with:
|
48
|
+
timeout_seconds: 30
|
49
|
+
max_attempts: 3
|
50
|
+
command: bundle exec rake spec
|
data/CHANGELOG.md
CHANGED
@@ -2,32 +2,72 @@ All notable changes to this project will be documented in this file.
|
|
2
2
|
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
4
4
|
|
5
|
+
## 1.1.1 - 2022-03-16
|
5
6
|
|
6
|
-
|
7
|
+
- Activate rubygems MFA
|
7
8
|
|
8
|
-
|
9
|
+
## 1.1.0
|
9
10
|
|
10
|
-
-
|
11
|
+
- Stop handling of `[data-initializing]` attribute. Apps that have late initialization after the `load` event can just use `CapybaraLockstep.startWork()`.
|
12
|
+
- Remove useless tracking of interaction events like `"click"` or `"focus"`. If such an event handler would start an AJAX request, it is already tracked.
|
13
|
+
- On apps with Unpoly 0.x, wait for one more task after `DOMContentLoaded`. Please upgrade to Unpoly 1.x or 2.x, as this logic will be removed in a year or so.
|
11
14
|
|
12
|
-
|
15
|
+
## 1.0.0
|
13
16
|
|
14
|
-
-
|
17
|
+
- First stable release.
|
18
|
+
- Replace option `Capybara::Lockstep.config` (`true`, `false`) with a more refined option `.mode` (`:auto`, `:manual`, `:off`)
|
15
19
|
|
16
|
-
## 0.7.0
|
20
|
+
## 0.7.0
|
17
21
|
|
18
|
-
|
22
|
+
- Ruby 3 compatibility.
|
23
|
+
- Fix logging.
|
19
24
|
|
20
|
-
|
21
|
-
- add gemika for tests with github actions
|
22
|
-
- add Ruby 3 support
|
25
|
+
## 0.6.0
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
## 0.
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
- Synchronize around `evaluate_script` and `execute_script`.
|
28
|
+
- Improve logging.
|
29
|
+
|
30
|
+
## 0.5.0
|
31
|
+
|
32
|
+
- Allow developer to signal custom async work.
|
33
|
+
- Option to wait additional tasks, to handle legacy promise implementations.
|
34
|
+
- Debugging log can be enabled during a running test.
|
35
|
+
- Also wait for images and iframes.
|
36
|
+
|
37
|
+
## 0.4.0
|
38
|
+
|
39
|
+
- Don't fail the test when synchronization times out.
|
40
|
+
- Capybara::Lockstep.debug = true will now also enable client-side logging on the browser's JavaScript console.
|
41
|
+
- Always wait at least for `Capybara.default_max_wait_time`.
|
42
|
+
|
43
|
+
## 0.3.2
|
44
|
+
|
45
|
+
- Delay synchronization when an alert is open (instead of failing)
|
46
|
+
|
47
|
+
|
48
|
+
## 0.3.1
|
49
|
+
|
50
|
+
- Fix typo in log message
|
51
|
+
|
52
|
+
## 0.3.0
|
53
|
+
|
54
|
+
- Rework entire waiting logic to be lazy.
|
55
|
+
- There is now a single method `Capybara::Lockstep.synchronize` (no distinction between awaiting "initialization" and "idle").
|
56
|
+
|
57
|
+
## 0.2.3
|
58
|
+
|
59
|
+
- When we cannot wait for browser idle due to an open alert, wait before the next Capybara synchronize
|
60
|
+
|
61
|
+
## 0.2.2
|
62
|
+
|
63
|
+
- Fix incorrect data in gemspec.
|
64
|
+
|
65
|
+
|
66
|
+
## 0.2.1
|
67
|
+
|
68
|
+
- Internal changes.
|
69
|
+
|
70
|
+
|
71
|
+
## 0.2.0
|
72
|
+
|
73
|
+
- Initial release.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
capybara-lockstep (
|
4
|
+
capybara-lockstep (1.1.1)
|
5
5
|
activesupport (>= 3.2)
|
6
6
|
capybara (>= 2.0)
|
7
7
|
ruby2_keywords
|
@@ -44,10 +44,8 @@ GEM
|
|
44
44
|
rake
|
45
45
|
jasmine-core (3.6.0)
|
46
46
|
mini_mime (1.1.0)
|
47
|
-
mini_portile2 (2.5.1)
|
48
47
|
minitest (5.14.4)
|
49
|
-
nokogiri (1.11.3)
|
50
|
-
mini_portile2 (~> 2.5.0)
|
48
|
+
nokogiri (1.11.3-x86_64-linux)
|
51
49
|
racc (~> 1.4)
|
52
50
|
phantomjs (2.1.1.0)
|
53
51
|
public_suffix (4.0.6)
|
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# capybara-lockstep
|
2
2
|
|
3
|
-
This Ruby gem synchronizes [Capybara](https://github.com/teamcapybara/capybara) commands with client-side JavaScript and AJAX requests. This greatly improves the stability of
|
3
|
+
This Ruby gem synchronizes [Capybara](https://github.com/teamcapybara/capybara) commands with client-side JavaScript and AJAX requests. This greatly improves the stability of an end-to-end ("E2E") test suite, even if that suite has timing issues.
|
4
|
+
|
5
|
+
The next section explain why your test suite is flaky and how capybara-lockstep can help.\
|
6
|
+
If you don't care you may skip to [installation instructions](#installation).
|
4
7
|
|
5
8
|
|
6
9
|
Why are tests flaky?
|
7
10
|
--------------------
|
8
11
|
|
9
|
-
A naively written
|
12
|
+
A naively written E2E test will have [race conditions](https://makandracards.com/makandra/47336-fixing-flaky-integration-tests) between the test script and the controlled browser. How often these timing issues will fail your test depends on luck and your machine's performance. You may not see these issues for years until a colleague runs your suite on their new laptop.
|
10
13
|
|
11
14
|
Here is a typical example for a test that will fail with unlucky timing:
|
12
15
|
|
@@ -23,28 +26,56 @@ end
|
|
23
26
|
|
24
27
|
This test has four timing issues that may cause it to fail:
|
25
28
|
|
26
|
-
1. We click on the
|
29
|
+
1. We click on the *New tweet* button, but the the JS event handler to open the tweet form wasn't registered yet.
|
27
30
|
2. We start filling in the form, but it wasn't loaded yet.
|
28
31
|
3. After sending the tweet we immediately navigate away, killing the form submission request that is still in flight. Hence the tweet will never appear in the next step.
|
29
32
|
4. We look for the new tweet, but the timeline wasn't loaded yet.
|
30
33
|
|
31
|
-
Capybara will retry individual commands or expectations when they fail
|
34
|
+
[Capybara will retry](https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends) individual commands or expectations when they fail.\
|
35
|
+
However, only issues **2** and **4** can be healed by retrying.
|
36
|
+
|
37
|
+
While it is [possible](https://makandracards.com/makandra/47336-fixing-flaky-integration-tests) to remove most of the timing issues above, it requires skill and discipline.\
|
38
|
+
capybara-lockstep fixes issues **1**, **2**, **3** and **4** without any changes to the test code.
|
39
|
+
|
40
|
+
|
41
|
+
### This is a JavaScript problem
|
42
|
+
|
43
|
+
The timing issues above will only manifest in an app where links, forms and buttons are handled by JavaScript.
|
44
|
+
|
45
|
+
When all you have is standard HTML links and forms, stock Capybara will not see timing issues:
|
46
|
+
|
47
|
+
- After a `visit()` Capybara/WebDriver will wait until the page is completely loaded
|
48
|
+
- When following a link Capybara/WebDriver will wait until the link destination is completely loaded
|
49
|
+
- When submitting a form Capybara/WebDriver will wait until the response is completely loaded
|
50
|
+
|
51
|
+
However, when JavaScript handles a link click, you get **zero guarantees**.\
|
52
|
+
Capybara/WebDriver **will not wait** for AJAX requests or any other async work.
|
32
53
|
|
33
|
-
While it is [possible](https://makandracards.com/makandra/47336-fixing-flaky-integration-tests) to remove most of the timing issues above, it requires skill and discipline. capybara-lockstep fixes issues **1**, **2**, **3** and **4** without any changes to the test code.
|
34
54
|
|
35
55
|
|
36
56
|
How capybara-lockstep helps
|
37
57
|
---------------------------
|
38
58
|
|
39
|
-
capybara-lockstep waits until the browser is idle before moving on to the next Capybara command. This greatly relieves the pressure on Capybara's retry logic.
|
59
|
+
capybara-lockstep waits until the browser is idle before moving on to the next Capybara command. This greatly relieves the pressure on [Capybara's retry logic](https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends).
|
40
60
|
|
41
|
-
|
61
|
+
capybara-lockstep synchronizes before:
|
42
62
|
|
43
|
-
-
|
44
|
-
-
|
45
|
-
-
|
63
|
+
- Capybara simulates a user interaction (clicking, typing, etc.)
|
64
|
+
- Capybara visits a new URL
|
65
|
+
- Capybara executes JavaScript
|
66
|
+
|
67
|
+
When capybara-lockstep synchronizes it will:
|
68
|
+
|
69
|
+
- wait for all document resources to load (images, CSS, fonts, frames).
|
70
|
+
- wait for client-side JavaScript to render or hydrate DOM elements.
|
71
|
+
- wait for any pending AJAX requests to finish and their callbacks to be called.
|
46
72
|
- capybara-lockstep waits for dynamically inserted `<script>`s to load (e.g. from [dynamic imports](https://webpack.js.org/guides/code-splitting/#dynamic-imports) or Analytics snippets).
|
47
|
-
- capybara-lockstep waits for dynamically `<img>` or `<iframe>` elements to load.
|
73
|
+
- capybara-lockstep waits for dynamically inserted `<img>` or `<iframe>` elements to load.
|
74
|
+
|
75
|
+
In summary Capybara can no longer observe the page while HTTP requests are in flight.
|
76
|
+
This covers most async work that causes flaky tests.
|
77
|
+
|
78
|
+
You can also configure capybara-lockstep to [wait for other async work](#signaling-asynchronous-work) that does not involve the network, like animations.
|
48
79
|
|
49
80
|
|
50
81
|
Installation
|
@@ -76,127 +107,90 @@ And then execute:
|
|
76
107
|
$ bundle install
|
77
108
|
```
|
78
109
|
|
79
|
-
If you're not using Rails you should also `require 'capybara-lockstep'` in your `spec_helper.rb` (RSpec) or `env.rb` (Cucumber).
|
110
|
+
If you're not using Rails you should also `require 'capybara-lockstep'` in your `spec_helper.rb` (RSpec), `test_helper.rb` (Minitest) or `env.rb` (Cucumber).
|
80
111
|
|
81
112
|
|
82
113
|
### Including the JavaScript snippet
|
83
114
|
|
84
115
|
capybara-lockstep requires a JavaScript snippet to be embedded by the application under test. If that snippet is missing on a screen, capybara-lockstep will not be able to synchronize with the browser. In that case the test will continue without synchronization.
|
85
116
|
|
86
|
-
If you're using Rails you can use the `capybara_lockstep` helper to insert the snippet into your application layouts:
|
117
|
+
**If you're using Rails** you can use the `capybara_lockstep` helper to insert the snippet into your application layouts:
|
87
118
|
|
88
119
|
```erb
|
89
|
-
<%= capybara_lockstep if
|
120
|
+
<%= capybara_lockstep if defined?(Capybara::Lockstep) %>
|
90
121
|
```
|
91
122
|
|
92
|
-
Ideally the snippet should be included in the `<head>` before any other `<script>` tags.
|
123
|
+
Ideally the snippet should be included in the `<head>` before any other `<script>` tags.
|
93
124
|
|
94
|
-
If you
|
125
|
+
**If you're not using Rails** you can `include Capybara::Lockstep::Helper` and access the JavaScript with `capybara_lockstep_script`.
|
95
126
|
|
96
|
-
If you
|
127
|
+
**If you have a strict [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)** the `capybara_lockstep` Rails helper will insert a CSP nonce by default. You can also pass an explicit nonce string using the `:nonce` option.
|
97
128
|
|
98
129
|
|
99
|
-
### Signaling the end of page initialization
|
100
130
|
|
101
|
-
|
131
|
+
### Verify successful integration
|
102
132
|
|
103
|
-
capybara-lockstep will
|
133
|
+
capybara-lockstep will automatically patch Capybara to wait for the browser after every command.
|
104
134
|
|
105
|
-
|
135
|
+
Run your test suite to see if integration was successful and whether stability improves. During validation we recommend to activate the [debugging log](#debugging-log) before your test:
|
106
136
|
|
107
|
-
```
|
108
|
-
|
137
|
+
```ruby
|
138
|
+
Capybara::Lockstep.debug = true
|
109
139
|
```
|
110
140
|
|
111
|
-
|
112
|
-
|
113
|
-
More precisely, the attribute should be removed in the same [JavaScript task](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) ("tick") that will finish initializing. capybara-lockstep will assume that the page will be initialized by the end of this task.
|
141
|
+
You should see messages like this in your console:
|
114
142
|
|
115
|
-
|
143
|
+
```text
|
144
|
+
[capybara-lockstep] Synchronizing
|
145
|
+
[capybara-lockstep] Finished waiting for JavaScript
|
146
|
+
[capybara-lockstep] Synchronized successfully
|
147
|
+
```
|
116
148
|
|
117
|
-
|
149
|
+
Note that you may see some failures from tests with wrong assertions, which previously passed due to lucky timing.
|
118
150
|
|
119
|
-
If all your initializing JavaScript runs synchronously on `DOMContentLoaded`, you can remove `[data-initializing]` in an event handler:
|
120
151
|
|
121
|
-
```js
|
122
|
-
document.addEventListener('DOMContentLoaded', function() {
|
123
|
-
// Initialize the page here
|
124
|
-
document.body.removeAttribute('data-initializing')
|
125
|
-
})
|
126
|
-
```
|
127
152
|
|
128
|
-
|
153
|
+
## Signaling asynchronous work
|
129
154
|
|
130
|
-
|
131
|
-
document.addEventListener('DOMContentLoaded', function() {
|
132
|
-
Libary.initialize({
|
133
|
-
onFinished: function() {
|
134
|
-
document.body.removeAttribute('data-initializing')
|
135
|
-
}
|
136
|
-
})
|
137
|
-
})
|
138
|
-
```
|
155
|
+
By default capybara-lockstep waits until resources have loaded, AJAX requests have finished and their callbacks have been called.
|
139
156
|
|
140
|
-
|
157
|
+
You can configure capybara-lockstep to wait for other async work that does not involve the network. Let's say we have an animation that fades in a new element over 2 seconds. The following will prevent Capybara from observing the page while the animation is running:
|
141
158
|
|
142
159
|
```js
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
160
|
+
async function fadeIn(element) {
|
161
|
+
CapybaraLockstep.startWork('Animation')
|
162
|
+
startAnimation(element, 'fade-in')
|
163
|
+
await waitForAnimationEnd(element)
|
164
|
+
CapybaraLockstep.stopWork('Animation')
|
165
|
+
}
|
147
166
|
```
|
148
167
|
|
149
|
-
|
168
|
+
The string argument is used for logging (when logging is enabled). It does **not** need to be unique per job. In this case you should see messages like this in your browser's JavaScript console:
|
150
169
|
|
151
|
-
```
|
152
|
-
|
153
|
-
|
154
|
-
HugeLibrary.initialize()
|
155
|
-
document.body.removeAttribute('data-initializing')
|
156
|
-
})
|
157
|
-
})
|
170
|
+
```text
|
171
|
+
[capybara-lockstep] Started work: Animation [1 jobs]
|
172
|
+
[capybara-lockstep] Finished work: Animation [0 jobs]
|
158
173
|
```
|
159
174
|
|
175
|
+
You may omit the string argument, in which case nothing will be logged, but the work will still be tracked.
|
160
176
|
|
161
|
-
#### Example: Unpoly
|
162
|
-
|
163
|
-
When you're using [Unpoly](https://unpoly.com/) initializing will usually happen synchronously in [compilers](https://unpoly.com/up.compiler). Hence a compiler is a good place to remove `[data-initializing]`:
|
164
|
-
|
165
|
-
```js
|
166
|
-
up.compiler('body', function(body) {
|
167
|
-
body.removeAttribute('data-initializing')
|
168
|
-
})
|
169
|
-
```
|
170
177
|
|
171
|
-
|
178
|
+
## Note on interacting with the JavaScript API
|
172
179
|
|
173
|
-
|
180
|
+
If you only load capybara-lockstep in tests you, should check for the `CapybaraLockstep` global to be defined before you interact with the JavaScript API.
|
174
181
|
|
175
182
|
```js
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
link: function() {
|
180
|
-
document.body.removeAttribute('data-initializing')
|
181
|
-
}
|
182
|
-
}
|
183
|
-
})
|
183
|
+
if (window.CapybaraLockstep) {
|
184
|
+
// interact with CapybaraLockstep
|
185
|
+
}
|
184
186
|
```
|
185
187
|
|
186
|
-
|
187
|
-
|
188
|
-
capybara-lockstep will automatically patch Capybara to wait for the browser after every command.
|
189
|
-
|
190
|
-
Run your test suite to see if integration was successful and whether stability improves. During validation we recommend to activate `Capybara::Lockstep.debug = true` in your `spec_helper.rb` (RSpec) or `env.rb` (Cucumber). You should see messages like this in your console:
|
188
|
+
If you can use ES6 you may also use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) to only call a function if `window.CapybaraLockstep` is defined:
|
191
189
|
|
192
|
-
```
|
193
|
-
|
194
|
-
[capybara-lockstep] Finished waiting for JavaScript
|
195
|
-
[capybara-lockstep] Synchronized successfully
|
190
|
+
```js
|
191
|
+
window.CapybaraLockstep?.startWork('Work')
|
196
192
|
```
|
197
193
|
|
198
|
-
Note that you may see some failures from tests with wrong assertions, which sometimes passed due to lucky timing.
|
199
|
-
|
200
194
|
|
201
195
|
## Performance impact
|
202
196
|
|
@@ -204,7 +198,7 @@ capybara-lockstep may or may not impact the runtime of your test suite. It depen
|
|
204
198
|
|
205
199
|
While waiting for the browser to be idle does take a few milliseconds, Capybara no longer needs to retry failed commands. You will also save time from not needing to re-run failed tests.
|
206
200
|
|
207
|
-
In casual testing I experienced a performance impact between +/- 10%.
|
201
|
+
In casual testing with large test suites I experienced a performance impact between +/- 10%.
|
208
202
|
|
209
203
|
|
210
204
|
## Debugging log
|
@@ -243,44 +237,41 @@ Capybara::Lockstep.debug = Rails.logger
|
|
243
237
|
|
244
238
|
### Logging in the browser only
|
245
239
|
|
246
|
-
To enable logging in the browser console (but not STDOUT), include the snippet with `{ debug: true }`:
|
240
|
+
To enable logging in the browser console (but not STDOUT), include the [JavaScript snippet](#including-the-javascript-snippet) with `{ debug: true }`:
|
247
241
|
|
248
|
-
```
|
242
|
+
```ruby
|
249
243
|
capybara_lockstep(debug: true)
|
250
244
|
```
|
251
245
|
|
246
|
+
## Synchronization timeout
|
252
247
|
|
253
|
-
|
254
|
-
|
255
|
-
Sometimes you want to disable browser synchronization, e.g. to observe a loading spinner during a long-running request.
|
248
|
+
By default capybara-lockstep will wait `Capybara.default_max_wait_time` seconds for the page initialize and for JavaScript and AJAX request to finish.
|
256
249
|
|
257
|
-
|
250
|
+
When synchronization times out, capybara-lockstep will [log](#debugging-log):
|
258
251
|
|
259
|
-
```
|
260
|
-
|
261
|
-
Capybara::Lockstep.enabled = false
|
262
|
-
do_unsynchronized_work
|
263
|
-
ensure
|
264
|
-
Capybara::Lockstep.enabled = true
|
265
|
-
end
|
252
|
+
```text
|
253
|
+
[capybara-lockstep] Could not synchronize within 3 seconds
|
266
254
|
```
|
267
255
|
|
268
|
-
|
256
|
+
You can configure a different timeout:
|
269
257
|
|
270
|
-
|
258
|
+
```ruby
|
259
|
+
Capybara::Lockstep.timeout = 5 # seconds
|
260
|
+
```
|
271
261
|
|
272
|
-
|
262
|
+
By default Capybara will **not** raise an error after a timeout. You may occasionally get a slow server response, and Capybara will retry synchronization before the next interaction or `visit`. This is often good enough.
|
273
263
|
|
274
|
-
|
264
|
+
If you want to be strict you may configure that an `Capybara::Lockstep::Timeout` error is raised after a timeout:
|
275
265
|
|
276
266
|
```ruby
|
277
|
-
Capybara::Lockstep.
|
267
|
+
Capybara::Lockstep.timeout_with = :error
|
278
268
|
```
|
279
269
|
|
280
|
-
To revert to
|
270
|
+
To revert to defaults:
|
281
271
|
|
282
272
|
```ruby
|
283
273
|
Capybara::Lockstep.timeout = nil
|
274
|
+
Capybara::Lockstep.timeout_with = nil
|
284
275
|
```
|
285
276
|
|
286
277
|
|
@@ -300,70 +291,85 @@ You may also synchronize from your client-side JavaScript. The following will ru
|
|
300
291
|
CapybaraLockstep.synchronize(callback)
|
301
292
|
```
|
302
293
|
|
303
|
-
## Signaling asynchronous work
|
304
294
|
|
305
|
-
|
295
|
+
## Disabling synchronization
|
306
296
|
|
307
|
-
|
308
|
-
CapybaraLockstep.startWork('Eject warp core')
|
309
|
-
doAsynchronousWork().then(function() {
|
310
|
-
CapybaraLockstep.stopWork('Eject warp core')
|
311
|
-
})
|
312
|
-
```
|
297
|
+
Sometimes you want to disable browser synchronization, e.g. to observe a loading spinner during a long-running request.
|
313
298
|
|
314
|
-
|
299
|
+
To disable automatic synchronization:
|
315
300
|
|
316
|
-
```
|
317
|
-
|
318
|
-
|
301
|
+
```ruby
|
302
|
+
begin
|
303
|
+
Capybara::Lockstep.mode = :manual
|
304
|
+
do_unsynchronized_work
|
305
|
+
ensure
|
306
|
+
Capybara::Lockstep.mode = :auto
|
307
|
+
end
|
319
308
|
```
|
320
309
|
|
321
|
-
|
310
|
+
In the `:manual` mode you may still force synchronization by calling `Capybara::Lockstep.synchronize` manually.
|
322
311
|
|
312
|
+
To completely disable synchronization:
|
323
313
|
|
324
|
-
|
314
|
+
```ruby
|
315
|
+
Capybara::Lockstep.mode = :off
|
316
|
+
Capybara::Lockstep.synchronize # will not synchronize
|
317
|
+
```
|
325
318
|
|
326
|
-
If you only load capybara-lockstep in tests you, should check for the `CapybaraLockstep` global to be defined before you interact with the JavaScript API.
|
327
319
|
|
328
|
-
```js
|
329
|
-
if (window.CapybaraLockstep) {
|
330
|
-
// interact with CapybaraLockstep
|
331
|
-
}
|
332
|
-
```
|
333
320
|
|
334
321
|
## Handling legacy promises
|
335
322
|
|
336
|
-
Legacy promise implementations (like jQuery's `$.Deferred` and AngularJS' `$q`) work using tasks instead of microtasks. Their AJAX implementations (like `$.ajax()` and `$http`) use
|
323
|
+
Legacy promise implementations (like jQuery's `$.Deferred` and AngularJS' `$q`) work using [tasks instead of microtasks](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/). Their AJAX implementations (like `$.ajax()` and `$http`) use task-based promises to signal that a request is done.
|
337
324
|
|
338
325
|
This means there is a time window in which all AJAX requests have finished, but their callbacks have not yet run:
|
339
326
|
|
340
327
|
```js
|
341
|
-
|
328
|
+
$http.get('/foo').then(function() {
|
342
329
|
// This callback runs one task after the response was received
|
343
330
|
})
|
344
331
|
```
|
345
332
|
|
346
|
-
It is theoretically possible that your test will observe the browser in that window, and expect content that has not been rendered yet.
|
333
|
+
It is theoretically possible that your test will observe the browser in that window, and expect content that has not been rendered yet. Affected code must call `then()` on a task-based promise **or** use `setTimeout()` to push work into the next task.
|
347
334
|
|
348
|
-
|
349
|
-
|
335
|
+
Any issues caused by this will usually be mitigated by Capybara's retry logic. **If** you think that this is an issue for your test suite, you can configure capybara-headless to wait additional tasks before it considers the browser to be idle:
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
Capybara::Lockstep.wait_tasks = 1
|
350
339
|
```
|
351
340
|
|
352
|
-
If you see longer `then()`
|
341
|
+
If you see longer chains of `then()` or nested `setTimeout()` calls in your code, you may need to configure a higher number of tasks to wait.
|
353
342
|
|
354
|
-
|
343
|
+
Waiting additional tasks will have a negative performance impact on your test suite.
|
355
344
|
|
345
|
+
> **Note:** When capybara-lockstep detects jQuery on the page, it will automatically patch [`$.ajax()`](https://api.jquery.com/jQuery.ajax/) to wait an additional task after the response was received. If your only concern is callbacks to `$.ajax()` you do not need so set `Capybara::Lockstep.wait_tasks`.
|
356
346
|
|
357
|
-
## Development
|
358
347
|
|
359
|
-
|
348
|
+
## Contributing
|
360
349
|
|
361
|
-
|
350
|
+
Pull requests are welcome on GitHub at <https://github.com/makandra/capybara-lockstep>.
|
362
351
|
|
352
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
363
353
|
|
364
|
-
|
354
|
+
Then, run `rake spec` to run the tests.
|
365
355
|
|
366
|
-
|
356
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
357
|
+
|
358
|
+
### Manually testing a change
|
359
|
+
|
360
|
+
To test an unrelased change with a test suite, we recommend to temporarily link the local repository from your test suites's `Gemfile`:
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
gem 'capybara-lockstep', path: '../capybara-lockstep'
|
364
|
+
```
|
365
|
+
|
366
|
+
As an alternative you may also install this gem onto your local machine by running `bundle exec rake install`.
|
367
|
+
|
368
|
+
### Releasing a new version
|
369
|
+
|
370
|
+
- Update the version number in `version.rb`
|
371
|
+
- Run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
372
|
+
- If RubyGems publishing seems to freeze, try entering your OTP code.
|
367
373
|
|
368
374
|
|
369
375
|
## License
|
data/capybara-lockstep.gemspec
CHANGED
@@ -14,6 +14,10 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.metadata["homepage_uri"] = spec.homepage
|
15
15
|
spec.metadata["source_code_uri"] = spec.homepage
|
16
16
|
|
17
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/makandra/capybara-lockstep/issues"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/makandra/capybara-lockstep/blob/master/CHANGELOG.md"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = 'true'
|
20
|
+
|
17
21
|
# Specify which files should be added to the gem when it is released.
|
18
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
23
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
@@ -55,8 +55,7 @@ module Capybara
|
|
55
55
|
# we force a non-lazy synchronization so we pick up all client-side changes
|
56
56
|
# that have not been caused by Capybara commands.
|
57
57
|
script_may_navigate_away = script =~ /\b(location|history)\b/
|
58
|
-
Lockstep.log "Synchronizing before script: #{script}"
|
59
|
-
Lockstep.synchronize(lazy: !script_may_navigate_away)
|
58
|
+
Lockstep.auto_synchronize(lazy: !script_may_navigate_away, log: "Synchronizing before script: #{script}")
|
60
59
|
end
|
61
60
|
|
62
61
|
super(script, *args, &block).tap do
|
@@ -143,9 +142,10 @@ module Capybara
|
|
143
142
|
module Lockstep
|
144
143
|
module SynchronizeWithCatchUp
|
145
144
|
ruby2_keywords def synchronize(*args, &block)
|
146
|
-
# This method is called
|
145
|
+
# This method is called by Capybara before most interactions with
|
146
|
+
# the browser. It is a different method than Capybara::Lockstep.synchronize!
|
147
147
|
# We use the { lazy } option to only synchronize when we're out of sync.
|
148
|
-
Capybara::Lockstep.
|
148
|
+
Capybara::Lockstep.auto_synchronize(lazy: true)
|
149
149
|
|
150
150
|
super(*args, &block)
|
151
151
|
end
|
@@ -10,6 +10,14 @@ module Capybara
|
|
10
10
|
@timeout = seconds
|
11
11
|
end
|
12
12
|
|
13
|
+
def timeout_with
|
14
|
+
@timeout_with.nil? ? :log : @timeout_with
|
15
|
+
end
|
16
|
+
|
17
|
+
def timeout_with=(action)
|
18
|
+
@timeout_with = action&.to_sym
|
19
|
+
end
|
20
|
+
|
13
21
|
def debug?
|
14
22
|
# @debug may also be a Logger object, so convert it to a boolean
|
15
23
|
@debug.nil? ? false : !!@debug
|
@@ -29,16 +37,30 @@ module Capybara
|
|
29
37
|
@debug
|
30
38
|
end
|
31
39
|
|
32
|
-
def
|
40
|
+
def mode
|
33
41
|
if javascript_driver?
|
34
|
-
@
|
42
|
+
@mode.nil? ? :auto : @mode
|
35
43
|
else
|
36
|
-
|
44
|
+
:off
|
37
45
|
end
|
38
46
|
end
|
39
47
|
|
48
|
+
def mode=(mode)
|
49
|
+
@mode = mode&.to_sym
|
50
|
+
end
|
51
|
+
|
40
52
|
def enabled=(enabled)
|
41
|
-
|
53
|
+
case enabled
|
54
|
+
when true
|
55
|
+
log "Setting `Capybara::Lockstep.enabled = true` is deprecated. Set `Capybara::Lockstep.mode = :auto` instead."
|
56
|
+
self.mode = :auto
|
57
|
+
when false
|
58
|
+
log "Setting `Capybara::Lockstep.enabled = false` is deprecated. Set `Capybara::Lockstep.mode = :manual` or `Capybara::Lockstep.mode = :off` instead."
|
59
|
+
self.mode = :manual
|
60
|
+
when nil
|
61
|
+
# Reset to default
|
62
|
+
self.mode = nil
|
63
|
+
end
|
42
64
|
end
|
43
65
|
|
44
66
|
def wait_tasks
|
@@ -55,10 +77,6 @@ module Capybara
|
|
55
77
|
@wait_tasks
|
56
78
|
end
|
57
79
|
|
58
|
-
def disabled?
|
59
|
-
!enabled?
|
60
|
-
end
|
61
|
-
|
62
80
|
private
|
63
81
|
|
64
82
|
def javascript_driver?
|
@@ -123,43 +123,21 @@ window.CapybaraLockstep = (function() {
|
|
123
123
|
}
|
124
124
|
}
|
125
125
|
|
126
|
-
function trackInteraction() {
|
127
|
-
// We already override all interaction methods in the Selenium browser nodes, so they
|
128
|
-
// wait for an idle frame afterwards. However a test script might also dispatch synthetic
|
129
|
-
// events with executate_script() to manipulate the browser in ways that are not possible
|
130
|
-
// with the Capybara API. When we observe such an event we wait until the end of the microtask,
|
131
|
-
// assuming any busy action will be queued by then.
|
132
|
-
['click', 'mousedown', 'keydown', 'change', 'input', 'submit', 'focusin', 'focusout', 'scroll'].forEach(function(eventType) {
|
133
|
-
// Use { useCapture: true } so we get the event before another listener
|
134
|
-
// can prevent it from bubbling up to the document.
|
135
|
-
document.addEventListener(eventType, onInteraction, { capture: true, passive: true })
|
136
|
-
})
|
137
|
-
}
|
138
|
-
|
139
|
-
function onInteraction(event) {
|
140
|
-
startWork()
|
141
|
-
// (1) We wait until the end of this microtask, assuming that any callback that
|
142
|
-
// would queue an AJAX request or load additional scripts will run by then.
|
143
|
-
// (2) For performance reasons we don't wait for `waitTasks` here.
|
144
|
-
// Whatever was queued by an event handler should call us again, and then
|
145
|
-
// we do wait for additional tasks.
|
146
|
-
Promise.resolve().then(stopWorkNow)
|
147
|
-
}
|
148
|
-
|
149
126
|
function trackRemoteElements() {
|
150
127
|
if (!window.MutationObserver) {
|
151
128
|
return
|
152
129
|
}
|
153
130
|
|
154
|
-
// Dynamic imports or analytics snippets may insert a
|
155
|
-
//
|
131
|
+
// Dynamic imports or analytics snippets may insert a script element
|
132
|
+
// that loads and executes additional JavaScript. We want to be isBusy()
|
156
133
|
// until such scripts have loaded or errored.
|
157
134
|
let observer = new MutationObserver(onAnyElementChanged)
|
158
135
|
observer.observe(document, { subtree: true, childList: true })
|
159
136
|
}
|
160
137
|
|
161
138
|
function trackJQuery() {
|
162
|
-
//
|
139
|
+
// CapybaraLockstep.track() is called as the first script in the head.
|
140
|
+
// jQuery will be loaded after us, so we wait until DOMContentReady.
|
163
141
|
whenReady(function() {
|
164
142
|
if (!window.jQuery || waitTasks > 0) {
|
165
143
|
return
|
@@ -180,28 +158,6 @@ window.CapybaraLockstep = (function() {
|
|
180
158
|
})
|
181
159
|
}
|
182
160
|
|
183
|
-
let INITIALIZING_ATTRIBUTE = 'data-initializing'
|
184
|
-
|
185
|
-
function trackHydration() {
|
186
|
-
// Until we have a body on which we can observe [data-initializing]
|
187
|
-
// we consider ourselves busy.
|
188
|
-
startWork()
|
189
|
-
whenReady(function() {
|
190
|
-
stopWorkNow()
|
191
|
-
if (document.body.hasAttribute(INITIALIZING_ATTRIBUTE)) {
|
192
|
-
startWork('Page initialization')
|
193
|
-
let observer = new MutationObserver(onInitializingAttributeChanged)
|
194
|
-
observer.observe(document.body, { attributes: true, attributeFilter: [INITIALIZING_ATTRIBUTE] })
|
195
|
-
}
|
196
|
-
})
|
197
|
-
}
|
198
|
-
|
199
|
-
function onInitializingAttributeChanged() {
|
200
|
-
if (!document.body.hasAttribute(INITIALIZING_ATTRIBUTE)) {
|
201
|
-
stopWork('Page initialization')
|
202
|
-
}
|
203
|
-
}
|
204
|
-
|
205
161
|
function isRemoteScript(element) {
|
206
162
|
if (element.tagName === 'SCRIPT') {
|
207
163
|
let src = element.getAttribute('src')
|
@@ -303,13 +259,28 @@ window.CapybaraLockstep = (function() {
|
|
303
259
|
}
|
304
260
|
}
|
305
261
|
|
262
|
+
function trackOldUnpoly() {
|
263
|
+
// CapybaraLockstep.track() is called as the first script in the head.
|
264
|
+
// Unpoly will be loaded after us, so we wait until DOMContentReady.
|
265
|
+
whenReady(function() {
|
266
|
+
// Unpoly 0.x would wait one task after DOMContentLoaded before booting.
|
267
|
+
// There's a slim chance that Capybara can observe the page before compilers have run.
|
268
|
+
// Unpoly 1.0+ runs compilers on DOMContentLoaded, so there's no issue.
|
269
|
+
if (window.up?.version?.startsWith('0.')) {
|
270
|
+
startWork('Old Unpoly')
|
271
|
+
setTimeout(function () {
|
272
|
+
stopWork('Old Unpoly')
|
273
|
+
})
|
274
|
+
}
|
275
|
+
})
|
276
|
+
}
|
277
|
+
|
306
278
|
function track() {
|
279
|
+
trackOldUnpoly()
|
307
280
|
trackFetch()
|
308
281
|
trackXHR()
|
309
|
-
trackInteraction()
|
310
282
|
trackRemoteElements()
|
311
283
|
trackJQuery()
|
312
|
-
trackHydration()
|
313
284
|
}
|
314
285
|
|
315
286
|
function synchronize(callback) {
|
@@ -23,14 +23,26 @@ module Capybara
|
|
23
23
|
page.instance_variable_set(:@lockstep_synchronized, value)
|
24
24
|
end
|
25
25
|
|
26
|
-
def synchronize(lazy: false)
|
27
|
-
if (lazy && synchronized?) || synchronizing? ||
|
26
|
+
def synchronize(lazy: false, log: nil)
|
27
|
+
if (lazy && synchronized?) || synchronizing? || mode == :off
|
28
28
|
return
|
29
29
|
end
|
30
30
|
|
31
|
+
# Allow passing a log message that is only logged
|
32
|
+
# when we're actually synchronizing.
|
33
|
+
if log
|
34
|
+
self.log(log)
|
35
|
+
end
|
36
|
+
|
31
37
|
synchronize_now
|
32
38
|
end
|
33
39
|
|
40
|
+
def auto_synchronize(**options)
|
41
|
+
if mode == :auto
|
42
|
+
synchronize(**options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
34
46
|
private
|
35
47
|
|
36
48
|
def synchronize_now
|
@@ -56,6 +68,8 @@ module Capybara
|
|
56
68
|
if (protocol === 'data:' || protocol == 'about:') {
|
57
69
|
done(#{ERROR_PAGE_MISSING.to_json})
|
58
70
|
} else if (document.readyState === 'complete') {
|
71
|
+
// WebDriver always waits for the `load` event after a visit(),
|
72
|
+
// unless a different page load strategy was configured.
|
59
73
|
synchronize()
|
60
74
|
} else {
|
61
75
|
window.addEventListener('load', synchronize)
|
@@ -76,9 +90,14 @@ module Capybara
|
|
76
90
|
end
|
77
91
|
end
|
78
92
|
rescue ::Selenium::WebDriver::Error::ScriptTimeoutError
|
79
|
-
|
80
|
-
|
81
|
-
|
93
|
+
timeout_message = "Could not synchronize within #{timeout} seconds"
|
94
|
+
log timeout_message
|
95
|
+
if timeout_with == :error
|
96
|
+
raise Timeout, timeout_message
|
97
|
+
else
|
98
|
+
# Don't raise an error, this may happen if the server is slow to respond.
|
99
|
+
# We will retry on the next Capybara synchronize call.
|
100
|
+
end
|
82
101
|
rescue ::Selenium::WebDriver::Error::UnexpectedAlertOpenError
|
83
102
|
log ERROR_ALERT_OPEN
|
84
103
|
# Don't raise an error, this will happen in an innocent test.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara-lockstep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henning Koch
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capybara
|
@@ -101,6 +101,9 @@ licenses:
|
|
101
101
|
metadata:
|
102
102
|
homepage_uri: https://github.com/makandra/capybara-lockstep
|
103
103
|
source_code_uri: https://github.com/makandra/capybara-lockstep
|
104
|
+
bug_tracker_uri: https://github.com/makandra/capybara-lockstep/issues
|
105
|
+
changelog_uri: https://github.com/makandra/capybara-lockstep/blob/master/CHANGELOG.md
|
106
|
+
rubygems_mfa_required: 'true'
|
104
107
|
post_install_message:
|
105
108
|
rdoc_options: []
|
106
109
|
require_paths:
|
@@ -116,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
119
|
- !ruby/object:Gem::Version
|
117
120
|
version: '0'
|
118
121
|
requirements: []
|
119
|
-
rubygems_version: 3.
|
122
|
+
rubygems_version: 3.1.4
|
120
123
|
signing_key:
|
121
124
|
specification_version: 4
|
122
125
|
summary: Synchronize Capybara commands with client-side JavaScript and AJAX requests
|