@webship-js/vue-steps 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +415 -2
- package/demo/app/app.vue +19 -0
- package/demo/app/pages/about.vue +10 -0
- package/demo/app/pages/client-only.vue +12 -0
- package/demo/app/pages/contact.vue +44 -0
- package/demo/app/pages/index.vue +15 -0
- package/demo/app/pages/posts/[id].vue +16 -0
- package/demo/app/pages/posts/index.vue +18 -0
- package/demo/app/pages/posts/latest.vue +11 -0
- package/demo/nuxt.config.ts +9 -0
- package/demo/server/api/contact.post.ts +19 -0
- package/demo/server/api/posts/[id].get.ts +8 -0
- package/demo/server/api/posts.get.ts +8 -0
- package/package.json +67 -4
- package/tests/features/errors.feature +14 -0
- package/tests/features/forms.feature +38 -0
- package/tests/features/page.feature +32 -0
- package/tests/features/views.feature +57 -0
- package/tests/step-definitions/vue-errors.steps.js +51 -0
- package/tests/step-definitions/vue-forms.steps.js +171 -0
- package/tests/step-definitions/vue-helpers.js +294 -0
- package/tests/step-definitions/vue-page.steps.js +109 -0
- package/tests/step-definitions/vue-probe.js +305 -0
- package/tests/step-definitions/vue-views.steps.js +211 -0
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webship-js/vue-steps",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Cucumber step definitions for Vue apps, Nuxt first, a plugin for webship-js (Playwright and Cucumber): steps that wait for the app to wire the server's page up, follow its views to the end of each navigation, send its forms with JavaScript and without, and wait on its data and its error pages, never on a fixed time.",
|
|
5
|
+
"main": "tests/step-definitions/vue-helpers.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"tests/step-definitions/",
|
|
8
|
+
"tests/features/",
|
|
9
|
+
"demo/",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "cucumber-js --config cucumber.js",
|
|
15
|
+
"test:dry": "cucumber-js --config cucumber.js --dry-run",
|
|
16
|
+
"test:examples": "node tests/check-examples.js",
|
|
17
|
+
"test:paths": "node tests/check-paths.js",
|
|
18
|
+
"test:must-fail": "node tests/check-must-fail.js",
|
|
19
|
+
"test:chromium": "BROWSER=chromium cucumber-js --config cucumber.js",
|
|
20
|
+
"test:firefox": "BROWSER=firefox cucumber-js --config cucumber.js",
|
|
21
|
+
"test:webkit": "BROWSER=webkit cucumber-js --config cucumber.js",
|
|
22
|
+
"test:headed": "HEADLESS=false cucumber-js --config cucumber.js",
|
|
23
|
+
"generate-reports": "generate-reports",
|
|
24
|
+
"test:fast": "SLOW_MO=0 cucumber-js --config cucumber.js",
|
|
25
|
+
"demo:build": "bash .github/scripts/build-demo.sh",
|
|
26
|
+
"demo:serve": "bash .github/scripts/serve-demo.sh"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"webship-js",
|
|
30
|
+
"vue",
|
|
31
|
+
"nuxt",
|
|
32
|
+
"hydration",
|
|
33
|
+
"vue-router",
|
|
34
|
+
"progressive-enhancement",
|
|
35
|
+
"cucumber",
|
|
36
|
+
"cucumber-js",
|
|
37
|
+
"playwright",
|
|
38
|
+
"bdd",
|
|
39
|
+
"testing",
|
|
40
|
+
"e2e",
|
|
41
|
+
"gherkin"
|
|
42
|
+
],
|
|
43
|
+
"author": "Webship.co",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/webship/vue-steps.git"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/webship/vue-steps/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/webship/vue-steps#readme",
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">= 24.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"webship-js": "^2.0.18"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"tsx": "^4.23.0",
|
|
64
|
+
"webship-js": "^2.0.18"
|
|
65
|
+
},
|
|
66
|
+
"overrides": {
|
|
67
|
+
"@cucumber/cucumber": "^12"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
@errors
|
|
2
|
+
Feature: Error pages
|
|
3
|
+
A view whose data is missing, and an address the app does not know, show the app's error page, with the
|
|
4
|
+
status in its first heading and the message under it; reached inside the app too.
|
|
5
|
+
|
|
6
|
+
@critical
|
|
7
|
+
Scenario: A post that does not exist
|
|
8
|
+
Given I am on "/posts/9"
|
|
9
|
+
Then the view should be the 404 error page saying "Post not found"
|
|
10
|
+
|
|
11
|
+
Scenario: An address the app does not know
|
|
12
|
+
Given I am on "/no-such-page"
|
|
13
|
+
Then the view should be the 404 error page saying "Page not found"
|
|
14
|
+
And the response status code should be 404
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
@forms
|
|
2
|
+
Feature: A form, with JavaScript and without
|
|
3
|
+
The demo's contact form posts to a server route without JavaScript, and the server sends the browser back
|
|
4
|
+
to the form with the answer in its address; with JavaScript the app sends it in place and shows the
|
|
5
|
+
answer. The same steps run in both: each waits for what its mode brings.
|
|
6
|
+
|
|
7
|
+
@critical
|
|
8
|
+
Scenario: A message is taken
|
|
9
|
+
Given I am on "/contact"
|
|
10
|
+
When I fill in "Your name" with "Ada"
|
|
11
|
+
And I fill in "Your message" with "Hello from the scenarios of the pack."
|
|
12
|
+
And I send the form with the "Send" button
|
|
13
|
+
Then the form should have gone through
|
|
14
|
+
And I should see "Thanks, Ada: your message was sent."
|
|
15
|
+
|
|
16
|
+
Scenario: A message too short is turned down, and says why
|
|
17
|
+
Given I am on "/contact"
|
|
18
|
+
When I fill in "Your name" with "Ada"
|
|
19
|
+
And I fill in "Your message" with "Too short"
|
|
20
|
+
And I send the form with the "Send" button
|
|
21
|
+
Then the form should have been turned down with "Your message needs at least 10 characters."
|
|
22
|
+
|
|
23
|
+
@needs-javascript
|
|
24
|
+
Scenario: With JavaScript, the form is sent in place
|
|
25
|
+
Given I am on "/contact"
|
|
26
|
+
When I fill in "Your name" with "Ada"
|
|
27
|
+
And I fill in "Your message" with "Hello from the scenarios of the pack."
|
|
28
|
+
And I send the form with the "Send" button
|
|
29
|
+
Then the form should have been sent in place
|
|
30
|
+
|
|
31
|
+
@no-javascript
|
|
32
|
+
Scenario: Without JavaScript, the form leads to a new page
|
|
33
|
+
Given I am on "/contact"
|
|
34
|
+
When I fill in "Your name" with "Ada"
|
|
35
|
+
And I fill in "Your message" with "Hello from the scenarios of the pack."
|
|
36
|
+
And I send the form with the "Send" button
|
|
37
|
+
Then the form should have led to a new page
|
|
38
|
+
And the view's address should be "/contact?sent=Ada"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
@page
|
|
2
|
+
Feature: The app wires the server's page up
|
|
3
|
+
The demo: Nuxt's official starter with pages of its own. A page arrives as the server's HTML; the app's
|
|
4
|
+
scripts then wire it up in the browser. One page is drawn in the browser only: the server sends an empty
|
|
5
|
+
shell for it.
|
|
6
|
+
|
|
7
|
+
@critical @needs-javascript
|
|
8
|
+
Scenario: A page of the server is wired up by the app
|
|
9
|
+
Given I am on "/"
|
|
10
|
+
And the page is wired up
|
|
11
|
+
Then the page should be wired up
|
|
12
|
+
And I should see "Welcome to the demo"
|
|
13
|
+
|
|
14
|
+
@needs-javascript
|
|
15
|
+
Scenario: A page drawn in the browser only
|
|
16
|
+
Given I am on "/client-only"
|
|
17
|
+
And the page is wired up
|
|
18
|
+
Then the page should have been drawn in the browser only
|
|
19
|
+
And I should see "rendered in the browser"
|
|
20
|
+
|
|
21
|
+
@no-javascript
|
|
22
|
+
Scenario: Without JavaScript the server's page is all there is
|
|
23
|
+
Given I am on "/posts"
|
|
24
|
+
And the page is wired up
|
|
25
|
+
Then I should see "Moving between pages"
|
|
26
|
+
|
|
27
|
+
@needs-javascript @still-after-steps
|
|
28
|
+
Scenario: A count that changes in place
|
|
29
|
+
Given I am on "/"
|
|
30
|
+
When I press "Add one to the count" by attr
|
|
31
|
+
And I wait for the page to be still
|
|
32
|
+
Then I should see "1" in the "#count" element
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
@views
|
|
2
|
+
Feature: The app's views
|
|
3
|
+
A link of a page the app has wired up changes the view inside the page: the router's navigation ends, the
|
|
4
|
+
route announcer says the new view's title, and the link of the current view is marked. The data of a view
|
|
5
|
+
is fetched during its navigation. Without JavaScript every link loads a new page.
|
|
6
|
+
|
|
7
|
+
@critical @needs-javascript
|
|
8
|
+
Scenario: A link changes the view in place, and the new view is announced
|
|
9
|
+
Given I am on "/"
|
|
10
|
+
When I use the "Posts" link
|
|
11
|
+
Then the view should have changed in place
|
|
12
|
+
And the new view should be announced as "Posts"
|
|
13
|
+
And the "Posts" link should be marked current
|
|
14
|
+
And the view's address should be "/posts"
|
|
15
|
+
And I should see "Moving between pages"
|
|
16
|
+
|
|
17
|
+
@needs-javascript
|
|
18
|
+
Scenario: A view with data of its own, and the browser's back
|
|
19
|
+
Given I am on "/posts"
|
|
20
|
+
When I use the "Moving between pages" link
|
|
21
|
+
Then the view should have changed in place
|
|
22
|
+
And the new view should be announced as "Moving between pages"
|
|
23
|
+
And the view's address should be "/posts/2"
|
|
24
|
+
When I return to the previous view
|
|
25
|
+
Then the view should have changed in place
|
|
26
|
+
And the view's address should be "/posts"
|
|
27
|
+
|
|
28
|
+
@needs-javascript
|
|
29
|
+
Scenario: Data loaded again in the view
|
|
30
|
+
Given I am on "/posts"
|
|
31
|
+
When I press "Load the posts again"
|
|
32
|
+
And I wait for the view's data to arrive
|
|
33
|
+
Then I should see "success" in the "#status" element
|
|
34
|
+
|
|
35
|
+
# A link whose view the app redirects: a route middleware sends /posts/latest on to the latest post. The router
|
|
36
|
+
# runs its guards once for each address, and the steps follow the navigation to its last one.
|
|
37
|
+
@needs-javascript
|
|
38
|
+
Scenario: A link whose view the app redirects changes the view in place, to where it was sent
|
|
39
|
+
Given I am on "/posts"
|
|
40
|
+
When I use the "The latest post" link
|
|
41
|
+
Then the view should have changed in place
|
|
42
|
+
And the view's address should be "/posts/3"
|
|
43
|
+
And the new view should be announced as "Forms"
|
|
44
|
+
|
|
45
|
+
@no-javascript
|
|
46
|
+
Scenario: Without JavaScript a redirected link loads the page it was sent to
|
|
47
|
+
Given I am on "/posts"
|
|
48
|
+
When I use the "The latest post" link
|
|
49
|
+
Then a new page should have loaded
|
|
50
|
+
And the view's address should be "/posts/3"
|
|
51
|
+
|
|
52
|
+
@no-javascript
|
|
53
|
+
Scenario: Without JavaScript every link loads a new page
|
|
54
|
+
Given I am on "/"
|
|
55
|
+
When I use the "About" link
|
|
56
|
+
Then a new page should have loaded
|
|
57
|
+
And the "About" link should be marked current
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Error pages. An address the app does not know, or a view whose data is missing, shows the app's error
|
|
4
|
+
// page: loaded with its status, or reached inside the app (a navigation whose view threw an error). The
|
|
5
|
+
// step reads the status (the page load's, when the page came by one; the app's own error, when it has
|
|
6
|
+
// one) and the first heading, and the message when one is given.
|
|
7
|
+
|
|
8
|
+
const { Then } = require('@cucumber/cucumber');
|
|
9
|
+
const h = require('./vue-helpers');
|
|
10
|
+
|
|
11
|
+
const withoutHash = (url) => String(url || '').split('#')[0];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The view is the app's error page for a status: its first heading carries the status (Nuxt's error page
|
|
15
|
+
* shows it, as most do), the page loaded with that status when it came by a page load, the app's own
|
|
16
|
+
* error says it when the app has one (Nuxt's payload), and the page says the message, when one is given.
|
|
17
|
+
* The step waits for it.
|
|
18
|
+
*
|
|
19
|
+
* Example #1: Then the view should be the 404 error page
|
|
20
|
+
* Example #2: Then the view should be the 404 error page saying "Post not found"
|
|
21
|
+
* Example #3: And the view should be the 500 error page saying "Internal Server Error"
|
|
22
|
+
* Example #4: Then the view should be the 403 error page
|
|
23
|
+
* Example #5: Then the view should be the 404 error page saying "Page not found"
|
|
24
|
+
*/
|
|
25
|
+
Then(/^the view should be the (\d{3}) error page(?: saying "([^"]*)")?$/, async function (code, message) {
|
|
26
|
+
const result = await h.poll(this, async (s) => {
|
|
27
|
+
if (!s || s.readyState === 'loading' || (s.nav && s.nav.pending)) return { done: false };
|
|
28
|
+
const heading = await h.call(this, 'firstHeading');
|
|
29
|
+
const text = message ? await h.call(this, 'pageText') : '';
|
|
30
|
+
const appError = await h.call(this, 'nuxtError').catch(() => null);
|
|
31
|
+
const doc = this.vueDocument;
|
|
32
|
+
const nav = this.vueNavigation;
|
|
33
|
+
// The status of the page load is known when the page on the screen is the one it brought.
|
|
34
|
+
const known = !!doc && withoutHash(doc.url) === withoutHash(s.url) && !(nav && nav.kind === 'app' && nav.at > doc.at);
|
|
35
|
+
const loadOk = !known || String(doc.status) === code;
|
|
36
|
+
const appOk = !appError || String(appError.status) === code;
|
|
37
|
+
const headingOk = !!heading && heading.includes(code);
|
|
38
|
+
const messageOk = !message || h.norm(text).toLowerCase().includes(h.norm(message).toLowerCase());
|
|
39
|
+
return { done: loadOk && appOk && headingOk && messageOk, heading, known, status: known ? doc.status : null, appError, messageOk, state: s };
|
|
40
|
+
}, h.budget(this));
|
|
41
|
+
if (result.done) return;
|
|
42
|
+
const what = [];
|
|
43
|
+
if (result.known && String(result.status) !== code) what.push(`the page loaded with the status ${result.status}`);
|
|
44
|
+
if (result.appError && String(result.appError.status) !== code) what.push(`the app's error is a ${result.appError.status}`);
|
|
45
|
+
if (!result.heading || !result.heading.includes(code)) what.push(result.heading ? `its first heading says "${h.excerpt(result.heading, 60)}"` : 'it has no first heading');
|
|
46
|
+
if (message && !result.messageOk) what.push(`it does not say "${message}"`);
|
|
47
|
+
throw await h.laneFailure(this, `Expected the view to be the ${code} error page${message ? ` saying "${message}"` : ''}, but ${what.join(', and ') || 'it is not'}.`, {
|
|
48
|
+
found: `${result.state ? result.state.url : 'the page'}${result.known ? '' : ' (reached inside the app: the status of no page load says it)'}`,
|
|
49
|
+
hint: 'an error page shows the status in its first heading and the message under it; an error page of the app\'s own may word them differently.',
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Forms, in both modes with one phrase. With JavaScript, an app sends a form in place: a request of its
|
|
4
|
+
// own (Nuxt's $fetch, or a library's), its answer shown in the page. Without JavaScript, or for a form
|
|
5
|
+
// the app leaves to the browser, the browser posts the form and loads the page it is answered with (the
|
|
6
|
+
// server may send it back to the form, with the answer in the address). The step waits for whichever
|
|
7
|
+
// happens, and records it for the assertions.
|
|
8
|
+
|
|
9
|
+
const { When, Then } = require('@cucumber/cucumber');
|
|
10
|
+
const h = require('./vue-helpers');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Send a form with one of its buttons, by the button's name, and wait for what it brings: the answer of
|
|
14
|
+
* the request the app sends in place, and the page still after it; or the page a posted form loads
|
|
15
|
+
* (through the server's redirects, wired up by the app when it can be). A button the app handles in the
|
|
16
|
+
* page without sending anything ends when the page is still.
|
|
17
|
+
*
|
|
18
|
+
* Example #1: When I send the form with the "Send" button
|
|
19
|
+
* Example #2: When I send the form with the "Save" button
|
|
20
|
+
* Example #3: And I send the form with the "Subscribe" button
|
|
21
|
+
* Example #4: When we send the form with the "Log in" button
|
|
22
|
+
* Example #5: When I send the form with the "Search" button
|
|
23
|
+
*/
|
|
24
|
+
When(/^(?:I |we )?send the form with the "([^"]*)" button$/, async function (name) {
|
|
25
|
+
await h.beforeActing(this);
|
|
26
|
+
const found = await h.byRoleAndName(this, 'button', name, this.page.locator('form'));
|
|
27
|
+
if (!found.locator) {
|
|
28
|
+
throw await h.laneFailure(this, `Expected a button named "${name}" in a form of the page, but there is none.`, {
|
|
29
|
+
found: `the buttons of the page's forms: ${h.listed(found.names, 30)}`,
|
|
30
|
+
hint: 'a button is named by its text, or its label; the name is matched from its start.',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (await found.locator.isDisabled().catch(() => false)) {
|
|
34
|
+
throw await h.laneFailure(this, `Could not send the form with the "${name}" button: it is disabled.`, {
|
|
35
|
+
hint: 'the form may need something first (a field filled in), or be sending already.',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const before = await h.pageState(this);
|
|
39
|
+
const seq = await h.call(this, 'seq');
|
|
40
|
+
const clicked = Date.now();
|
|
41
|
+
try {
|
|
42
|
+
await found.locator.click({ timeout: h.budget(this) });
|
|
43
|
+
} catch (e) {
|
|
44
|
+
throw h.friendly({ action: 'click', target: `the "${name}" button`, cause: e, hint: 'it may be covered by another part of the page.' });
|
|
45
|
+
}
|
|
46
|
+
const result = await h.poll(this, async (s) => {
|
|
47
|
+
if (!s) return { done: false };
|
|
48
|
+
if (s.doc !== before.doc) {
|
|
49
|
+
const ready = s.readyState === 'complete' && (!s.app || !s.js || s.wired);
|
|
50
|
+
return { done: ready && s.still, mode: 'whole', state: s };
|
|
51
|
+
}
|
|
52
|
+
const sent = (await h.call(this, 'requestsSince', seq)).filter((r) => r.method !== 'GET');
|
|
53
|
+
if (sent.length) {
|
|
54
|
+
const last = sent[sent.length - 1];
|
|
55
|
+
return { done: sent.every((r) => r.end) && s.still, mode: 'place', status: last.status, body: last.body, state: s };
|
|
56
|
+
}
|
|
57
|
+
// A page load on its way: the old page stays on the screen until the answer comes.
|
|
58
|
+
if (this.vueLoadStarted >= clicked) return { done: false, mode: 'whole', state: s };
|
|
59
|
+
// Nothing sent and nothing loaded: the app handled the button in the page.
|
|
60
|
+
return { done: s.still && Date.now() - clicked > 300, mode: 'none', state: s };
|
|
61
|
+
}, h.budget(this));
|
|
62
|
+
if (!result.done) {
|
|
63
|
+
const s = result.state || {};
|
|
64
|
+
throw await h.laneFailure(this, `Sending the form with the "${name}" button did not end in ${Math.round(h.budget(this) / 1000)} s.`, {
|
|
65
|
+
found: `still busy: ${(s.busy || []).join('; ') || 'nothing'}`,
|
|
66
|
+
hint: 'the server may not answer the form, or the page it sends to may fail to load.',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
let status = result.status;
|
|
70
|
+
let statuses = status === undefined ? [] : [status];
|
|
71
|
+
if (result.mode === 'whole') {
|
|
72
|
+
// The posted form's answer and the pages it sent to: 303 then 200, when the server sends the browser
|
|
73
|
+
// back to the form with the answer in its address.
|
|
74
|
+
statuses = (this.vueDocuments || []).filter((d) => d.at >= clicked).map((d) => d.status);
|
|
75
|
+
status = statuses.length ? statuses[0] : undefined;
|
|
76
|
+
this.vueNavigation = { kind: 'whole', from: before.url, to: result.state.url, doc: result.state.doc, at: Date.now() };
|
|
77
|
+
}
|
|
78
|
+
const alerts = await h.call(this, 'alerts').catch(() => []);
|
|
79
|
+
this.vueForm = { mode: result.mode, status, statuses, body: result.body || '', alerts, button: name, at: Date.now() };
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/** The last form a step of the pack sent, or a failure that says to send one first. */
|
|
83
|
+
async function lastForm(world) {
|
|
84
|
+
if (world.vueForm) return world.vueForm;
|
|
85
|
+
throw await h.laneFailure(world, 'Expected a form the scenario sent, but none of its steps sent one.', {
|
|
86
|
+
hint: 'send it with "I send the form with the "..." button" first.',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether the app turned a sent form down: its answer was a 4xx, or the page shows an alert. */
|
|
91
|
+
function turnedDown(form) {
|
|
92
|
+
return (form.status >= 400 && form.status < 500) || form.alerts.length > 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The app took the last form a step of the pack sent: its answer was not a refusal (2xx, or a redirect),
|
|
97
|
+
* and the page shows no alert (role="alert") about it.
|
|
98
|
+
*
|
|
99
|
+
* Example #1: Then the form should have gone through
|
|
100
|
+
* Example #2: And the form should have gone through
|
|
101
|
+
* Example #3: Then the form should have gone through
|
|
102
|
+
* Example #4: But the form should have gone through
|
|
103
|
+
* Example #5: Then the form should have gone through
|
|
104
|
+
*/
|
|
105
|
+
Then(/^the form should have gone through$/, async function () {
|
|
106
|
+
const form = await lastForm(this);
|
|
107
|
+
if (form.mode === 'none') {
|
|
108
|
+
throw await h.laneFailure(this, `Expected the form to have gone through, but the "${form.button}" button sent nothing.`, {
|
|
109
|
+
found: 'the app handled the button in the page: no request, no page load',
|
|
110
|
+
hint: 'a button can change the page without sending the form; the form is sent by its submit button.',
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (form.status >= 200 && form.status < 400 && !form.alerts.length) return;
|
|
114
|
+
throw await h.laneFailure(this, `Expected the form to have gone through, but ${form.alerts.length ? `the page says "${h.excerpt(form.alerts[0], 100)}"` : `the answer was ${form.status}`}.`, {
|
|
115
|
+
found: form.mode === 'place' ? `the answer of the request sent in place: ${form.status}` : `the pages the posted form loaded: ${form.statuses.join(' then ') || '(none)'}`,
|
|
116
|
+
hint: 'a refused form keeps what was sent and says why in an alert: look at the message, or at the fields it names.',
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The app turned the last form a step of the pack sent down, and says why: its answer was a refusal
|
|
122
|
+
* (4xx), or the page shows an alert (role="alert"); the message given is in the alert (from its start,
|
|
123
|
+
* without regard to case), or in the refusal's answer.
|
|
124
|
+
*
|
|
125
|
+
* Example #1: Then the form should have been turned down with "Your message needs at least 10 characters."
|
|
126
|
+
* Example #2: Then the form should have been turned down with "Your name is required."
|
|
127
|
+
* Example #3: And the form should have been turned down with "Wrong email or password"
|
|
128
|
+
* Example #4: Then the form should have been turned down with "Your message needs"
|
|
129
|
+
* Example #5: Then the form should have been turned down with "This field is required"
|
|
130
|
+
*/
|
|
131
|
+
Then(/^the form should have been turned down with "([^"]*)"$/, async function (message) {
|
|
132
|
+
const form = await lastForm(this);
|
|
133
|
+
const want = h.norm(message).toLowerCase();
|
|
134
|
+
// The alert may come a little after the answer: read the page's alerts again for a while.
|
|
135
|
+
let alerts = form.alerts;
|
|
136
|
+
const deadline = Date.now() + Math.min(h.budget(this), 5000);
|
|
137
|
+
const says = () => alerts.some((a) => h.norm(a).toLowerCase().startsWith(want) || h.norm(a).toLowerCase().includes(want))
|
|
138
|
+
|| (form.status >= 400 && h.norm(form.body).toLowerCase().includes(want));
|
|
139
|
+
while (!(turnedDown({ ...form, alerts }) && says()) && Date.now() < deadline) {
|
|
140
|
+
await h.sleep(50);
|
|
141
|
+
alerts = await h.call(this, 'alerts').catch(() => alerts);
|
|
142
|
+
}
|
|
143
|
+
if (turnedDown({ ...form, alerts }) && says()) return;
|
|
144
|
+
throw await h.laneFailure(this, !turnedDown({ ...form, alerts })
|
|
145
|
+
? `Expected the form to have been turned down with "${message}", but it went through (${form.status}).`
|
|
146
|
+
: `Expected the form to have been turned down with "${message}", but it says ${h.listed(alerts) === '(none)' ? `nothing in an alert (the answer was ${form.status})` : h.listed(alerts)}.`, {
|
|
147
|
+
found: form.mode === 'place' ? `the answer of the request sent in place: ${form.status}` : `the pages the posted form loaded: ${form.statuses.join(' then ') || '(none)'}`,
|
|
148
|
+
hint: 'the message is the text of the page\'s alert (role="alert"), matched from its start without regard to case.',
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* How the last form a step of the pack sent was sent: in place (a request of the app, the page staying
|
|
154
|
+
* as it is), or by the browser, which loaded the page the form was answered with.
|
|
155
|
+
*
|
|
156
|
+
* Example #1: Then the form should have been sent in place
|
|
157
|
+
* Example #2: Then the form should have led to a new page
|
|
158
|
+
* Example #3: And the form should have been sent in place
|
|
159
|
+
* Example #4: But the form should have led to a new page
|
|
160
|
+
* Example #5: Then the form should have been sent in place
|
|
161
|
+
*/
|
|
162
|
+
Then(/^the form should (have been sent in place|have led to a new page)$/, async function (how) {
|
|
163
|
+
const form = await lastForm(this);
|
|
164
|
+
const wanted = how === 'have been sent in place' ? 'place' : 'whole';
|
|
165
|
+
if (form.mode === wanted) return;
|
|
166
|
+
const said = { place: 'was sent in place', whole: 'led to a new page', none: 'was not sent: the app handled the button in the page' };
|
|
167
|
+
throw await h.laneFailure(this, `Expected the form to ${how}, but it ${said[form.mode] || form.mode}.`, {
|
|
168
|
+
found: `the "${form.button}" button`,
|
|
169
|
+
hint: wanted === 'place' ? 'a form is sent in place when the app has wired the page up and handles the form\'s submit.' : 'without JavaScript a form loads the page it is answered with.',
|
|
170
|
+
});
|
|
171
|
+
});
|