@webship-js/nextjs-steps 0.0.0-stage → 2.0.1
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 +575 -2
- package/package.json +64 -4
- package/tests/features/README.md +28 -0
- package/tests/features/build.feature +24 -0
- package/tests/features/default-app.feature +33 -0
- package/tests/features/development.feature +24 -0
- package/tests/features/errors.feature +38 -0
- package/tests/features/forms.feature +56 -0
- package/tests/features/images.feature +38 -0
- package/tests/features/loading.feature +43 -0
- package/tests/features/navigation.feature +46 -0
- package/tests/features/pages-router.feature +35 -0
- package/tests/features/search-and-pagination.feature +37 -0
- package/tests/features/session.feature +59 -0
- package/tests/step-definitions/README.md +30 -0
- package/tests/step-definitions/nextjs-errors.steps.js +250 -0
- package/tests/step-definitions/nextjs-forms.steps.js +266 -0
- package/tests/step-definitions/nextjs-helpers.js +1036 -0
- package/tests/step-definitions/nextjs-images.steps.js +200 -0
- package/tests/step-definitions/nextjs-loading.steps.js +170 -0
- package/tests/step-definitions/nextjs-navigation.steps.js +194 -0
- package/tests/step-definitions/nextjs-pages.steps.js +204 -0
- package/tests/step-definitions/nextjs-session.steps.js +254 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The page as the framework serves it: the wait for the page to be ready,
|
|
4
|
+
// hydration and its errors, the production build, the router that serves the
|
|
5
|
+
// page, the release it runs on, and pages prerendered or rendered for each
|
|
6
|
+
// request.
|
|
7
|
+
//
|
|
8
|
+
// This file also puts the recorders on every page of the run (see
|
|
9
|
+
// nextjs-helpers.js): the identity of each document and the commits of the
|
|
10
|
+
// router, the requests of the app by kind, and the errors of the console.
|
|
11
|
+
|
|
12
|
+
const { Then, When } = require('@cucumber/cucumber');
|
|
13
|
+
const { onNewPage } = require('webship-js/tests/step-definitions/webship');
|
|
14
|
+
const {
|
|
15
|
+
budget,
|
|
16
|
+
failure,
|
|
17
|
+
listed,
|
|
18
|
+
requests,
|
|
19
|
+
consoleOf,
|
|
20
|
+
hydrationErrors,
|
|
21
|
+
documentScript,
|
|
22
|
+
appInfo,
|
|
23
|
+
hydrationProblem,
|
|
24
|
+
pageReady,
|
|
25
|
+
eventually,
|
|
26
|
+
} = require('./nextjs-helpers');
|
|
27
|
+
|
|
28
|
+
onNewPage(async (world, page) => {
|
|
29
|
+
await page.addInitScript(documentScript);
|
|
30
|
+
// The requests and the console of a page are kept from the moment it opens.
|
|
31
|
+
requests(page);
|
|
32
|
+
consoleOf(page);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Wait until the page is ready: loaded and hydrated, no part of it showing a
|
|
37
|
+
* loading state, and no request of the app running (see nextjs-helpers.js).
|
|
38
|
+
* Fails with what the page was still doing when the time ran out.
|
|
39
|
+
*
|
|
40
|
+
* Example #1: When I wait for the page to be ready
|
|
41
|
+
* Example #2: And I wait for the page to be ready
|
|
42
|
+
* Example #3: When we wait for the page to be ready
|
|
43
|
+
* Example #4: Then I wait for the page to be ready
|
|
44
|
+
* Example #5: Given we wait for the page to be ready
|
|
45
|
+
*/
|
|
46
|
+
When(/^(I |we )*wait for the page to be ready$/, { timeout: 60000 }, async function (pronounCase) {
|
|
47
|
+
await pageReady(this, { strict: true });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The page has hydrated: the app took over the HTML the server sent (its root
|
|
52
|
+
* carries React's fiber) and no part of it is still waiting to be hydrated.
|
|
53
|
+
* With "not", the page shows only the server's HTML.
|
|
54
|
+
*
|
|
55
|
+
* Example #1: Then I should see a hydrated page
|
|
56
|
+
* Example #2: And we should see a hydrated page
|
|
57
|
+
* Example #3: Then I should not see a hydrated page
|
|
58
|
+
* Example #4: And I should see a hydrated page
|
|
59
|
+
* Example #5: Then we should not see a hydrated page
|
|
60
|
+
*/
|
|
61
|
+
Then(/^(I |we )*should( not)* see a hydrated page$/, async function (pronounCase, notCase) {
|
|
62
|
+
const hydrated = async () => !(await hydrationProblem(this));
|
|
63
|
+
if (notCase) {
|
|
64
|
+
if (await hydrated()) {
|
|
65
|
+
throw failure(this, `Expected the page not to be hydrated, but the app has taken it over (${this.page.url()}).`, 'A page shows only its server HTML when JavaScript is off or its scripts did not load.');
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
await eventually(this, hydrated, (ok) => ok, () => ({
|
|
70
|
+
message: `Expected a hydrated page, but the app did not take over the page within ${budget(this)} ms (${this.page.url()}).`,
|
|
71
|
+
hint: 'Look at the browser console for an error, and check that the scripts of the page (/_next/static/…) load.',
|
|
72
|
+
}));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* No hydration error on this page: the HTML of the server and the first render
|
|
77
|
+
* in the browser were the same (React reports a mismatch in the console: in
|
|
78
|
+
* words in development, as the errors #418, #423 and #425 in production).
|
|
79
|
+
*
|
|
80
|
+
* Example #1: Then there should be no hydration errors
|
|
81
|
+
* Example #2: And there should be no hydration errors
|
|
82
|
+
* Example #3: Then there should be hydration errors
|
|
83
|
+
* Example #4: But there should be no hydration errors
|
|
84
|
+
* Example #5: Then there should be no hydration errors
|
|
85
|
+
*/
|
|
86
|
+
Then(/^there should be (no )?hydration errors$/, async function (noCase) {
|
|
87
|
+
await pageReady(this);
|
|
88
|
+
const errors = hydrationErrors(this.page);
|
|
89
|
+
if (noCase && errors.length) {
|
|
90
|
+
throw failure(this, `Expected no hydration errors, but the console reported ${errors.length}: ${listed(errors.map((e) => e.slice(0, 160)))}.`, 'The server rendered something the browser renders differently: a date, a random value, or a check of window or localStorage during render.');
|
|
91
|
+
}
|
|
92
|
+
if (!noCase && !errors.length) {
|
|
93
|
+
throw failure(this, `Expected hydration errors, but the console reported none (${this.page.url()}).`, 'Hydration errors appear in the console of the page while it hydrates.');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The app is served as a production build: no development tools on the page,
|
|
99
|
+
* no development server behind it. With "not", it is the development server.
|
|
100
|
+
*
|
|
101
|
+
* Example #1: Then the site should be served as a production build
|
|
102
|
+
* Example #2: And the site should be a production build
|
|
103
|
+
* Example #3: Then the site should not be served as a production build
|
|
104
|
+
* Example #4: Then site should be served as a production build
|
|
105
|
+
* Example #5: And the site should not be a production build
|
|
106
|
+
*/
|
|
107
|
+
Then(/^(?:the )?site should( not)* be (?:served as )?a production build$/, async function (notCase) {
|
|
108
|
+
await pageReady(this);
|
|
109
|
+
const info = await appInfo(this);
|
|
110
|
+
if (!info.router) {
|
|
111
|
+
throw failure(this, `Expected a page of the app, but this page is not served by the framework (${this.page.url()}).`, 'Open a page of the app first; the steps read what the framework puts on every page.');
|
|
112
|
+
}
|
|
113
|
+
if (!notCase && info.development) {
|
|
114
|
+
throw failure(this, `Expected a production build, but the development server serves this page (its development tools are on it) at ${this.page.url()}.`, 'Serve the app with `next build` then `next start`, not `next dev`.');
|
|
115
|
+
}
|
|
116
|
+
if (notCase && !info.development) {
|
|
117
|
+
throw failure(this, `Expected the development server, but a production build serves this page (${this.page.url()}).`, 'Serve the app with `next dev` to see its development tools.');
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The router that serves the page: the App Router (the app/ folder) or the
|
|
123
|
+
* Pages Router (the pages/ folder).
|
|
124
|
+
*
|
|
125
|
+
* Example #1: Then the page should be served by the app router
|
|
126
|
+
* Example #2: Then the page should be served by the pages router
|
|
127
|
+
* Example #3: And page should be served by the app router
|
|
128
|
+
* Example #4: And the page should be served by the pages router
|
|
129
|
+
* Example #5: Then the page should be served by the app router
|
|
130
|
+
*/
|
|
131
|
+
Then(/^(?:the )?page should be served by the (app|pages) router$/, async function (router) {
|
|
132
|
+
await pageReady(this);
|
|
133
|
+
const info = await appInfo(this);
|
|
134
|
+
if (info.router !== router) {
|
|
135
|
+
throw failure(this, `Expected the ${router} router to serve ${this.page.url()}, but ${info.router ? `the ${info.router} router does` : 'the framework does not serve this page'}.`, 'The App Router serves the routes of the app/ folder; the Pages Router those of the pages/ folder.');
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The release of the framework the page runs on: "16" for any 16.x release,
|
|
141
|
+
* "16.4" for any 16.4.x, "16.4.6" for exactly that one.
|
|
142
|
+
*
|
|
143
|
+
* Example #1: Then the page should run on the release "16"
|
|
144
|
+
* Example #2: And the page should run on the release "16.4.6"
|
|
145
|
+
* Example #3: Then the page should not run on the release "14"
|
|
146
|
+
* Example #4: Then page should run on the release "15.5"
|
|
147
|
+
* Example #5: And the page should run on the release "16.4"
|
|
148
|
+
*/
|
|
149
|
+
Then(/^(?:the )?page should( not)* run on the release "([^"]*)"$/, async function (notCase, release) {
|
|
150
|
+
await pageReady(this);
|
|
151
|
+
const info = await appInfo(this);
|
|
152
|
+
const matches = !!info.version && (info.version === release || info.version.startsWith(`${release}.`));
|
|
153
|
+
if (!notCase && !matches) {
|
|
154
|
+
throw failure(this, `Expected the page to run on the release ${release}, but it runs on ${info.version || 'no release the page says'} (${this.page.url()}).`, 'The page says its release in window.next.version.');
|
|
155
|
+
}
|
|
156
|
+
if (notCase && matches) {
|
|
157
|
+
throw failure(this, `Expected the page not to run on the release ${release}, but it runs on ${info.version}.`, 'The page says its release in window.next.version.');
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Whether the server sends the page from what it prerendered (at build time or
|
|
163
|
+
* by revalidation) or renders it for each request: the headers of its answer
|
|
164
|
+
* (x-nextjs-prerender, x-nextjs-cache, a shared cache time in Cache-Control)
|
|
165
|
+
* and, for the Pages Router, the page's own data (a page optimized to static
|
|
166
|
+
* HTML, or one with getStaticProps; getServerSideProps renders for each
|
|
167
|
+
* request).
|
|
168
|
+
*
|
|
169
|
+
* Example #1: Then the page should be prerendered
|
|
170
|
+
* Example #2: Then the page should not be prerendered
|
|
171
|
+
* Example #3: And the page should be prerendered
|
|
172
|
+
* Example #4: And page should not be prerendered
|
|
173
|
+
* Example #5: Then the page should be prerendered
|
|
174
|
+
*/
|
|
175
|
+
Then(/^(?:the )?page should( not)* be prerendered$/, async function (notCase) {
|
|
176
|
+
await pageReady(this);
|
|
177
|
+
const url = this.page.url();
|
|
178
|
+
let headers = {};
|
|
179
|
+
let status = 0;
|
|
180
|
+
let body = '';
|
|
181
|
+
try {
|
|
182
|
+
const response = await this.page.context().request.get(url, { failOnStatusCode: false, maxRedirects: 0 });
|
|
183
|
+
headers = response.headers();
|
|
184
|
+
status = response.status();
|
|
185
|
+
body = await response.text().catch(() => '');
|
|
186
|
+
} catch (err) {
|
|
187
|
+
throw failure(this, `Could not ask the server for ${url} again: ${err.message}`, 'The step reads the headers of the page\'s answer.');
|
|
188
|
+
}
|
|
189
|
+
let data = null;
|
|
190
|
+
const script = body.match(/<script id="__NEXT_DATA__"[^>]*>([\s\S]*?)<\/script>/);
|
|
191
|
+
if (script) {
|
|
192
|
+
try { data = JSON.parse(script[1]); } catch { data = null; }
|
|
193
|
+
}
|
|
194
|
+
const fromHeaders = /^1\b/.test(headers['x-nextjs-prerender'] || '') || /^(HIT|STALE|PRERENDER)/i.test(headers['x-nextjs-cache'] || '') || /s-maxage=\d+/.test(headers['cache-control'] || '');
|
|
195
|
+
const fromData = !!data && !data.gssp && !!(data.autoExport || data.nextExport || data.gsp);
|
|
196
|
+
const prerendered = fromHeaders || fromData;
|
|
197
|
+
const seen = `status ${status}, x-nextjs-prerender: ${headers['x-nextjs-prerender'] || '(none)'}, x-nextjs-cache: ${headers['x-nextjs-cache'] || '(none)'}, cache-control: ${headers['cache-control'] || '(none)'}${data ? `, page data: ${['autoExport', 'gsp', 'gssp'].filter((k) => data[k]).join(', ') || 'none of autoExport, gsp, gssp'}` : ''}`;
|
|
198
|
+
if (!notCase && !prerendered) {
|
|
199
|
+
throw failure(this, `Expected ${url} to be prerendered, but the server rendered it for this request (${seen}).`, 'A page that reads cookies, headers or the search parameters, or that opts out of caching, is rendered for each request (getServerSideProps in the Pages Router). A development server prerenders nothing.');
|
|
200
|
+
}
|
|
201
|
+
if (notCase && prerendered) {
|
|
202
|
+
throw failure(this, `Expected ${url} to be rendered for each request, but the server sent what it prerendered (${seen}).`, 'A page with no dynamic input is prerendered at build time.');
|
|
203
|
+
}
|
|
204
|
+
});
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The session of an app with a login form: logging in by a handle of the users
|
|
4
|
+
// registry (a session taken over when the account already has one), through
|
|
5
|
+
// the form, or with an address and a password; filling in the form without
|
|
6
|
+
// sending it; logging out; who is signed in; the login form, and where it
|
|
7
|
+
// sends a person back to.
|
|
8
|
+
//
|
|
9
|
+
// Accounts are configuration, not test data: they are listed in cucumber.js
|
|
10
|
+
// (`worldParameters.users`) and created once by the build of the app. A
|
|
11
|
+
// feature never creates or deletes users. Who is signed in is read from the
|
|
12
|
+
// app's session address (`worldParameters.sessionPath`, /api/auth/session, the
|
|
13
|
+
// one of the app's authentication library).
|
|
14
|
+
|
|
15
|
+
const { Given, Then, When } = require('@cucumber/cucumber');
|
|
16
|
+
const { friendly } = require('webship-js/tests/step-definitions/webship');
|
|
17
|
+
const {
|
|
18
|
+
budget,
|
|
19
|
+
same,
|
|
20
|
+
listed,
|
|
21
|
+
failure,
|
|
22
|
+
shortUrl,
|
|
23
|
+
currentPath,
|
|
24
|
+
pathOf,
|
|
25
|
+
pageReady,
|
|
26
|
+
afterAction,
|
|
27
|
+
personClick,
|
|
28
|
+
waitHydrated,
|
|
29
|
+
firstPresent,
|
|
30
|
+
resolveUser,
|
|
31
|
+
loginPath,
|
|
32
|
+
sessionPath,
|
|
33
|
+
loginForm,
|
|
34
|
+
currentSession,
|
|
35
|
+
LOGIN_HINT,
|
|
36
|
+
CUT_REQUEST_HINT,
|
|
37
|
+
openLoginForm,
|
|
38
|
+
submitLogin,
|
|
39
|
+
logInAs,
|
|
40
|
+
forgetSession,
|
|
41
|
+
eventually,
|
|
42
|
+
} = require('./nextjs-helpers');
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Log in as an account of the users registry (`worldParameters.users` in
|
|
46
|
+
* cucumber.js), by its handle. An account that logged in earlier in the run
|
|
47
|
+
* takes its session over; otherwise the step logs in through the login form.
|
|
48
|
+
* The browser ends on the page the app shows after a login.
|
|
49
|
+
*
|
|
50
|
+
* Example #1: Given I am a logged in user with the "administrator" user
|
|
51
|
+
* Example #2: Given we are a logged in user with the "editor" user
|
|
52
|
+
* Example #3: Given I am a logged in user with "administrator"
|
|
53
|
+
* Example #4: And I am a logged in user with the "editor" user
|
|
54
|
+
* Example #5: Given we are a logged in user with the "administrator" user
|
|
55
|
+
*/
|
|
56
|
+
Given(/^(I am |we are )*a logged in user with( the)* "([^"]*)"( user)*$/, { timeout: 120000 }, async function (pronounCase, theCase, handle, userCase) {
|
|
57
|
+
await logInAs(this, handle);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Log in through the login form as an account of the users registry, as a
|
|
62
|
+
* person does: a new session, never one taken over. For the scenarios that
|
|
63
|
+
* test the form itself; the step fails when the app refuses the login.
|
|
64
|
+
*
|
|
65
|
+
* Example #1: When I log in as "administrator" with the login form
|
|
66
|
+
* Example #2: When we log in as "editor" with the login form
|
|
67
|
+
* Example #3: And I log in as "editor" with the login form
|
|
68
|
+
* Example #4: When I log in as "administrator" with the login form
|
|
69
|
+
* Example #5: And we log in as "administrator" with the login form
|
|
70
|
+
*/
|
|
71
|
+
When(/^(I |we )*log in as "([^"]*)" with the login form$/, { timeout: 120000 }, async function (pronounCase, handle) {
|
|
72
|
+
const user = resolveUser(this, handle);
|
|
73
|
+
forgetSession(this);
|
|
74
|
+
const outcome = await submitLogin(this, user.login, user.password);
|
|
75
|
+
if (outcome.error) {
|
|
76
|
+
throw friendly({
|
|
77
|
+
message: `The login form refused "${handle}" (${user.login}): ${outcome.error}`,
|
|
78
|
+
hint: outcome.cut ? CUT_REQUEST_HINT : 'Check the address and the password of the account in worldParameters.users, and that the build of the app created the account.',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
this.__signedInAs = handle;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Send the login form with an address and a password as typed: for the
|
|
86
|
+
* refusals (a wrong password, an address the app does not know). The step
|
|
87
|
+
* does not fail when the app refuses; the next steps say what it shows.
|
|
88
|
+
*
|
|
89
|
+
* Example #1: When I log in with the email "administrator@example.com" and the password "wrong-password"
|
|
90
|
+
* Example #2: When we log in with the email "nobody@example.com" and the password "wrong-password"
|
|
91
|
+
* Example #3: And I log in with the email "editor@example.com" and the password "not-the-password"
|
|
92
|
+
* Example #4: When I log in with the email "administrator@example.com" and the password "123"
|
|
93
|
+
* Example #5: When I log in with the email "not-an-address" and the password "wrong-password"
|
|
94
|
+
*/
|
|
95
|
+
When(/^(I |we )*log in with the email "([^"]*)" and the password "([^"]*)"$/, { timeout: 120000 }, async function (pronounCase, login, password) {
|
|
96
|
+
forgetSession(this);
|
|
97
|
+
const outcome = await submitLogin(this, login, password);
|
|
98
|
+
if (outcome.cut) {
|
|
99
|
+
throw friendly({ message: `The login could not be sent: ${outcome.error}`, hint: CUT_REQUEST_HINT });
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Open the login page as a new visitor and fill in its form with an account
|
|
105
|
+
* of the users registry, without sending it: for the scenarios that look at
|
|
106
|
+
* the form while it is sent (its button busy, the answers held back).
|
|
107
|
+
*
|
|
108
|
+
* Example #1: When I fill in the login form as "administrator"
|
|
109
|
+
* Example #2: And we fill in the login form as "editor"
|
|
110
|
+
* Example #3: Given I fill in the login form as "editor"
|
|
111
|
+
* Example #4: When I fill in the login form as "editor"
|
|
112
|
+
* Example #5: And I fill in the login form as "administrator"
|
|
113
|
+
*/
|
|
114
|
+
When(/^(I |we )*fill in the login form as "([^"]*)"$/, { timeout: 60000 }, async function (pronounCase, handle) {
|
|
115
|
+
const user = resolveUser(this, handle);
|
|
116
|
+
forgetSession(this);
|
|
117
|
+
await openLoginForm(this);
|
|
118
|
+
const form = loginForm(this);
|
|
119
|
+
const field = form.locator('input[type="email"], input[name="email"], input[name="username"], input[autocomplete="username"], input[type="text"]').first();
|
|
120
|
+
await waitHydrated(this, field, 'the address field of the login form');
|
|
121
|
+
await field.fill(user.login);
|
|
122
|
+
await form.locator('input[type="password"]').first().fill(user.password);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Log out with the app's own button or link ("Sign Out", "Log out"), and wait
|
|
127
|
+
* for the page the app shows after it. The session of the account is
|
|
128
|
+
* forgotten for the rest of the run.
|
|
129
|
+
*
|
|
130
|
+
* Example #1: When I log out
|
|
131
|
+
* Example #2: And we log out
|
|
132
|
+
* Example #3: Then I log out
|
|
133
|
+
* Example #4: When we log out
|
|
134
|
+
* Example #5: And I log out
|
|
135
|
+
*/
|
|
136
|
+
When(/^(I |we )*log out$/, { timeout: 60000 }, async function (pronounCase) {
|
|
137
|
+
await pageReady(this);
|
|
138
|
+
const names = /^\s*(sign ?out|log ?out)\s*$/i;
|
|
139
|
+
const control = await firstPresent([
|
|
140
|
+
this.page.getByRole('button', { name: names }).filter({ visible: true }),
|
|
141
|
+
this.page.getByRole('link', { name: names }).filter({ visible: true }),
|
|
142
|
+
this.page.getByRole('menuitem', { name: names }).filter({ visible: true }),
|
|
143
|
+
]);
|
|
144
|
+
if (!(await control.count())) {
|
|
145
|
+
throw failure(this, `Expected a button or a link to log out ("Sign Out", "Log out") on ${shortUrl(this.page.url())}, but there is none.`, 'Log in first, and open a page of the app that offers it.');
|
|
146
|
+
}
|
|
147
|
+
await afterAction(this, () => personClick(this, control, 'the button to log out'));
|
|
148
|
+
forgetSession(this);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Whether anybody is signed in: the app's session address answers with a user
|
|
153
|
+
* (or null). With "not", nobody is.
|
|
154
|
+
*
|
|
155
|
+
* Example #1: Then I should be logged in
|
|
156
|
+
* Example #2: Then I should not be logged in
|
|
157
|
+
* Example #3: And we should be logged in
|
|
158
|
+
* Example #4: And I should not be logged in
|
|
159
|
+
* Example #5: Then we should not be logged in
|
|
160
|
+
*/
|
|
161
|
+
Then(/^(I |we )*should( not)* be logged in$/, async function (pronounCase, notCase) {
|
|
162
|
+
await pageReady(this);
|
|
163
|
+
if (!sessionPath(this)) {
|
|
164
|
+
throw failure(this, 'Expected to read who is signed in, but worldParameters.sessionPath is empty.', 'Set worldParameters.sessionPath in cucumber.js to the address that answers with the session (/api/auth/session for the app\'s authentication library).');
|
|
165
|
+
}
|
|
166
|
+
const session = await eventually(this, () => currentSession(this), (s) => s.known && (notCase ? !s.user : !!s.user), (s) => ({
|
|
167
|
+
message: !s.known
|
|
168
|
+
? `Expected to read the session at ${sessionPath(this)}, but it answered ${s.status || 'nothing'}.`
|
|
169
|
+
: (notCase ? `Expected nobody to be signed in, but ${s.user.email || s.user.name || 'an account'} is.` : 'Expected to be logged in, but nobody is signed in.'),
|
|
170
|
+
hint: !s.known ? 'worldParameters.sessionPath is the address of the app that answers with the session as JSON (null when nobody is signed in).' : 'Who is signed in is what the app\'s session address says for the cookies of the browser.',
|
|
171
|
+
}));
|
|
172
|
+
if (!notCase) this.__signedInAs = this.__signedInAs || (session.user && session.user.email);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Who is signed in: the session address answers with the account of the
|
|
177
|
+
* users registry the handle names (its address).
|
|
178
|
+
*
|
|
179
|
+
* Example #1: Then I should be logged in as "administrator"
|
|
180
|
+
* Example #2: And we should be logged in as "editor"
|
|
181
|
+
* Example #3: Then I should be logged in as "editor"
|
|
182
|
+
* Example #4: And I should be logged in as "administrator"
|
|
183
|
+
* Example #5: Then we should be logged in as "administrator"
|
|
184
|
+
*/
|
|
185
|
+
Then(/^(I |we )*should be logged in as "([^"]*)"$/, async function (pronounCase, handle) {
|
|
186
|
+
await pageReady(this);
|
|
187
|
+
const user = resolveUser(this, handle);
|
|
188
|
+
await eventually(this, () => currentSession(this), (s) => s.known && !!s.user && same(s.user.email || '', user.email || user.login), (s) => ({
|
|
189
|
+
message: !s.known
|
|
190
|
+
? `Expected to read the session at ${sessionPath(this)}, but it answered ${s.status || 'nothing'}.`
|
|
191
|
+
: `Expected to be logged in as "${handle}" (${user.email || user.login}), but ${s.user ? `${s.user.email || s.user.name} is` : 'nobody is'} signed in.`,
|
|
192
|
+
hint: 'Who is signed in is what the app\'s session address says for the cookies of the browser.',
|
|
193
|
+
}));
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The page shows the app's login form (a visible form with a password field),
|
|
198
|
+
* at the login page (`worldParameters.loginPath`).
|
|
199
|
+
*
|
|
200
|
+
* Example #1: Then I should see the login form
|
|
201
|
+
* Example #2: And we should see the login form
|
|
202
|
+
* Example #3: Then I should not see the login form
|
|
203
|
+
* Example #4: And I should see the login form
|
|
204
|
+
* Example #5: Then we should see the login form
|
|
205
|
+
*/
|
|
206
|
+
Then(/^(I |we )*should( not)* see the login form$/, async function (pronounCase, notCase) {
|
|
207
|
+
await pageReady(this);
|
|
208
|
+
const form = loginForm(this);
|
|
209
|
+
if (notCase) {
|
|
210
|
+
if ((await form.count()) > 0) {
|
|
211
|
+
throw failure(this, `Expected no login form, but ${shortUrl(this.page.url())} shows one.`, 'A page for signed-in accounts only sends a visitor to the login form.');
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
await form.waitFor({ state: 'visible', timeout: budget(this) });
|
|
217
|
+
} catch {
|
|
218
|
+
throw failure(this, `Expected the login form, but ${shortUrl(this.page.url())} shows none.`, LOGIN_HINT);
|
|
219
|
+
}
|
|
220
|
+
if (currentPath(this) !== pathOf(loginPath(this))) {
|
|
221
|
+
throw failure(this, `Expected the login form at ${loginPath(this)}, but the form is at ${shortUrl(this.page.url())}.`, 'Set worldParameters.loginPath in cucumber.js to the app\'s login page.');
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Where the login form will send the person back to once logged in: the page
|
|
227
|
+
* they asked for, kept in the address of the login page (callbackUrl,
|
|
228
|
+
* redirect, next, returnTo, from).
|
|
229
|
+
*
|
|
230
|
+
* Example #1: Then the login form should send me back to "/dashboard"
|
|
231
|
+
* Example #2: And the login form should send us back to "/dashboard/invoices"
|
|
232
|
+
* Example #3: Then login form should send me back to "/dashboard/customers"
|
|
233
|
+
* Example #4: And the login form should send me back to "/dashboard/invoices?page=2"
|
|
234
|
+
* Example #5: Then the login form should send me back to "/dashboard"
|
|
235
|
+
*/
|
|
236
|
+
Then(/^(?:the )?login form should send (?:me|us) back to "([^"]*)"$/, async function (path) {
|
|
237
|
+
await pageReady(this);
|
|
238
|
+
const url = new URL(this.page.url());
|
|
239
|
+
const kept = ['callbackUrl', 'callbackurl', 'redirect', 'redirectTo', 'next', 'returnTo', 'from']
|
|
240
|
+
.map((name) => url.searchParams.get(name))
|
|
241
|
+
.filter(Boolean);
|
|
242
|
+
const wanted = new URL(path, url.origin);
|
|
243
|
+
const back = kept.some((value) => {
|
|
244
|
+
try {
|
|
245
|
+
const target = new URL(value, url.origin);
|
|
246
|
+
return target.pathname.replace(/\/+$/, '') === wanted.pathname.replace(/\/+$/, '') && (!wanted.search || target.search === wanted.search);
|
|
247
|
+
} catch {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
if (!back) {
|
|
252
|
+
throw failure(this, `Expected the login form to send the person back to "${path}", but its address keeps ${listed(kept)} (${shortUrl(this.page.url())}).`, 'A page for signed-in accounts sends a visitor to the login page with the page asked for in its address (callbackUrl for the app\'s authentication library).');
|
|
253
|
+
}
|
|
254
|
+
});
|