webship-js 2.0.9 → 2.0.10

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.
@@ -0,0 +1,14 @@
1
+ Fix: send the REST and API requests with the HTTPS setting of the browser (#333)
2
+
3
+ The REST and API request steps sent their request from Node with axios,
4
+ outside the browser, so on HTTPS with a self-signed or locally issued
5
+ certificate they failed with "self-signed certificate" although the
6
+ browser itself opened the site.
7
+
8
+ Both now use Playwright's request API. The REST steps go through the
9
+ browser context and follow its cookies, credentials and HTTPS setting.
10
+ The API steps keep their own base URL, authentication and headers: they
11
+ use a request context of their own, with the HTTPS setting of the browser
12
+ context and no cookie of the browser.
13
+
14
+ Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@@ -0,0 +1,42 @@
1
+ p='docs/09-api-testing.md'
2
+ s=open(p).read()
3
+ old="Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. The browser context's cookie jar is shared, so a UI login can be followed by REST-level assertions in the same scenario.\n"
4
+ new="""Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. Every request is sent with Playwright's request API and follows the HTTPS setting of the browser context (`ignoreHTTPSErrors` in `playwright.config.ts`), so a site served with a self-signed or locally issued certificate answers these steps the same way it answers the browser.
5
+
6
+ There are two families of steps, and they differ in one point:
7
+
8
+ | Steps | Sent | Cookies of the browser |
9
+ | --- | --- | --- |
10
+ | `I send a REST "<method>" request to "<url>"` (`rest.steps.js`) | through the browser context | shared: a UI login can be followed by REST-level assertions in the same scenario |
11
+ | `I send a <METHOD> request to "<endpoint>"` (`api.steps.js`) | outside the browser session | not sent, and none comes back into the browser: the request carries only the base URL, the authentication and the headers the scenario set |
12
+ """
13
+ assert old in s; s=s.replace(old,new)
14
+ old="A parallel short-form lives in `rest.steps.js` for quick smoke tests:\n"
15
+ new="A parallel short-form lives in `rest.steps.js` for quick smoke tests. A path is joined to the launch URL, an absolute URL is requested as written, and the request goes through the browser context: it shares the session cookies and the HTTPS setting of the page.\n"
16
+ assert old in s; s=s.replace(old,new)
17
+ open(p,'w').write(s)
18
+ p='docs/api-step-definitions.md'
19
+ s=open(p).read()
20
+ old="""webship-js ships native HTTP / REST API step definitions powered by
21
+ Playwright's request fixture and `axios`. They share the browser
22
+ context's cookie jar so the same scenario can mix UI actions and
23
+ direct API calls.
24
+ """
25
+ new="""webship-js ships native HTTP / REST API step definitions powered by
26
+ Playwright's request API, so the same scenario can mix UI actions and
27
+ direct API calls.
28
+
29
+ The API steps on this page send their requests outside the browser
30
+ session: a request carries the base URL, the authentication and the
31
+ headers the scenario set, and no cookie of the browser. They follow the
32
+ HTTPS setting of the browser context (`ignoreHTTPSErrors` in
33
+ `playwright.config.ts`), so an API served with a self-signed or locally
34
+ issued certificate answers them the same way it answers the browser.
35
+
36
+ To send a request with the cookies of the browser session, after a UI
37
+ login for example, use the short-form REST steps
38
+ (`When I send a REST "GET" request to "/api/me"`), which go through the
39
+ browser context.
40
+ """
41
+ assert old in s; s=s.replace(old,new)
42
+ open(p,'w').write(s)
@@ -0,0 +1,8 @@
1
+ const http = require('http');
2
+ http.createServer((req, res) => {
3
+ let b = ''; req.on('data', c => b += c); req.on('end', () => {
4
+ res.setHeader('content-type', 'application/json');
5
+ res.setHeader('set-cookie', 'from_api=1; Path=/');
6
+ res.end(JSON.stringify({ method: req.method, cookie: req.headers.cookie || 'none', auth: req.headers.authorization || 'none', type: req.headers['content-type'] || 'none', body: b }));
7
+ });
8
+ }).listen(8990);
@@ -0,0 +1,35 @@
1
+ const http = require('http');
2
+ const axios = require('axios');
3
+ const { request } = require('playwright');
4
+ const srv = http.createServer((req, res) => {
5
+ let b = ''; req.on('data', c => b += c); req.on('end', () => {
6
+ res.setHeader('content-type', 'application/json');
7
+ res.end(JSON.stringify({ m: req.method, h: req.headers, b }));
8
+ });
9
+ }).listen(0, async () => {
10
+ const u = `http://localhost:${srv.address().port}/x`;
11
+ const cases = [
12
+ ['GET nodata', { method: 'GET', url: u, headers: {}, data: null }],
13
+ ['GET undefined', { method: 'GET', url: u, headers: {} }],
14
+ ['GET obj', { method: 'GET', url: u, headers: {}, data: { a: 1 } }],
15
+ ['POST null', { method: 'POST', url: u, headers: {}, data: null }],
16
+ ['POST obj', { method: 'POST', url: u, headers: {}, data: { a: 1 } }],
17
+ ['POST str json', { method: 'POST', url: u, headers: {}, data: '{"a": 1}' }],
18
+ ['POST str json ct', { method: 'POST', url: u, headers: { 'Content-Type': 'application/json' }, data: '{"a":1}' }],
19
+ ['POST str form', { method: 'POST', url: u, headers: {}, data: 'a=1&b=2' }],
20
+ ['POST str ct json nonjson', { method: 'POST', url: u, headers: { 'Content-Type': 'application/json' }, data: JSON.stringify({a:'1'}) }],
21
+ ['DELETE', { method: 'DELETE', url: u, headers: {} }],
22
+ ['post lowercase', { method: 'post', url: u, headers: {}, data: 'x' }],
23
+ ];
24
+ const ctx = await request.newContext();
25
+ for (const [n, c] of cases) {
26
+ const r = await axios({ ...c, validateStatus: () => true });
27
+ const d = r.data; delete d.h.host; delete d.h.connection;
28
+ console.log('AX', n, JSON.stringify(d));
29
+ const o = { method: c.method, headers: c.headers };
30
+ if (c.data !== undefined && c.data !== null) o.data = c.data;
31
+ const p = await ctx.fetch(u, o); const pj = await p.json(); delete pj.h.host; delete pj.h.connection;
32
+ console.log('PW', n, JSON.stringify(pj));
33
+ }
34
+ await ctx.dispose(); srv.close();
35
+ });
@@ -0,0 +1 @@
1
+ 2359762
@@ -0,0 +1,22 @@
1
+ Feature: cookie check (not committed)
2
+ Scenario: REST shares the browser cookies, API does not
3
+ Given I am on "/about-us.html"
4
+ And the cookie "session_id" is set to "abc123"
5
+ Given the API base URL is "http://localhost:8990"
6
+ And I am authenticating as "tester" with "secret" password
7
+ When I send a POST request to "/echo" with body:
8
+ """
9
+ {"a": 1}
10
+ """
11
+ Then the JSON response should have "cookie" equal to "none"
12
+ And the JSON response should have "auth" equal to "Basic dGVzdGVyOnNlY3JldA=="
13
+ And the JSON response should have "type" equal to "application/json"
14
+ And a cookie with the name "from_api" should not exist
15
+ When I send a REST "POST" request to "http://localhost:8990/echo" with body:
16
+ """
17
+ a=1&b=2
18
+ """
19
+ Then the REST response should contain "session_id=abc123"
20
+ And the REST response should contain "application/x-www-form-urlencoded"
21
+ And the REST response should contain "a=1&b=2"
22
+ And a cookie with the name "from_api" should exist
@@ -0,0 +1 @@
1
+ 2324561
@@ -0,0 +1 @@
1
+ 2324559
@@ -0,0 +1,49 @@
1
+ ### Problem/Motivation
2
+
3
+ `When I send a REST "<method>" request to "<url>"` (`tests/step-definitions/rest.steps.js`) and the API request steps `When I send a <METHOD> request to "<endpoint>"` with their `with body:`, `with values:` and `with form data:` forms (`tests/step-definitions/api.steps.js`) send their request from Node with `axios`, outside the browser. Node does not trust a self-signed or locally issued certificate, so on a site served over HTTPS with one the REST step fails with:
4
+
5
+ ```
6
+ Error: self-signed certificate
7
+ ```
8
+
9
+ and the API steps fail with:
10
+
11
+ ```
12
+ GET request failed: self-signed certificate
13
+ ```
14
+
15
+ (`unable to verify the first certificate` when the certificate comes from a local authority.) The browser itself opens the same site, because the browser context sets `ignoreHTTPSErrors`. It is the same weakness as #330, in the remaining steps that still send a request from Node.
16
+
17
+ #### Steps to reproduce
18
+
19
+ 1. Serve `examples/` over HTTPS with a self-signed certificate: `npx http-server examples -p 8443 -S -C cert.pem -K key.pem`.
20
+ 2. Run the suite with `LAUNCH_URL=https://localhost:8443`.
21
+ 3. `When I send a REST "GET" request to "/example-api.json"` (`tests/features/absolute-url.feature:41`) fails with `Error: self-signed certificate`.
22
+ 4. `Given the API base URL is "/"`, `When I send a GET request to "/example-api.json"` fails with `GET request failed: self-signed certificate`.
23
+
24
+ ### Proposed resolution
25
+
26
+ Send these requests with Playwright's request API so they follow the HTTPS setting of the browser context: the REST steps through the browser context itself, the API steps from a request context of their own that carries no cookie of the browser.
27
+
28
+ AI-Generated: Yes
29
+
30
+ ### Checkpoints:
31
+ - [x] File an issue
32
+ - [ ] Addition/Change/Update/Fix
33
+ - [ ] Testing to ensure no regression
34
+ - [ ] Automated unit testing coverage
35
+ - [ ] Automated functional testing coverage
36
+ - [ ] UX/UI designer responsibilities
37
+ - [ ] Readability
38
+ - [ ] Accessibility
39
+ - [ ] Performance
40
+ - [ ] Security
41
+ - [ ] Developer Documentation
42
+ - [ ] User Guide Documentation
43
+ - [ ] Reviewed by human
44
+ - [ ] Code review by maintainers
45
+ - [ ] Full testing and approval
46
+ - [ ] Credit contributors
47
+ - [ ] Review with the product owner
48
+ - [ ] Release notes snippet
49
+ - [ ] Release
@@ -0,0 +1,2 @@
1
+ const base = require('../cucumber.js').default;
2
+ module.exports = { default: { ...base, paths: process.env.ONE.split(','), format: ['progress', 'json:.scratch/one.json'] } };
@@ -0,0 +1,280 @@
1
+ [
2
+ {
3
+ "description": "",
4
+ "elements": [
5
+ {
6
+ "description": "",
7
+ "id": "cookie-check-(not-committed);rest-shares-the-browser-cookies,-api-does-not",
8
+ "keyword": "Scenario",
9
+ "line": 2,
10
+ "name": "REST shares the browser cookies, API does not",
11
+ "steps": [
12
+ {
13
+ "keyword": "Before",
14
+ "hidden": true,
15
+ "result": {
16
+ "status": "passed",
17
+ "duration": 59045699
18
+ }
19
+ },
20
+ {
21
+ "keyword": "Before",
22
+ "hidden": true,
23
+ "result": {
24
+ "status": "passed",
25
+ "duration": 146611
26
+ }
27
+ },
28
+ {
29
+ "keyword": "Before",
30
+ "hidden": true,
31
+ "result": {
32
+ "status": "passed",
33
+ "duration": 429690
34
+ }
35
+ },
36
+ {
37
+ "keyword": "Before",
38
+ "hidden": true,
39
+ "result": {
40
+ "status": "passed",
41
+ "duration": 358450
42
+ }
43
+ },
44
+ {
45
+ "keyword": "Before",
46
+ "hidden": true,
47
+ "result": {
48
+ "status": "passed",
49
+ "duration": 56777
50
+ }
51
+ },
52
+ {
53
+ "keyword": "Before",
54
+ "hidden": true,
55
+ "result": {
56
+ "status": "passed",
57
+ "duration": 106737
58
+ }
59
+ },
60
+ {
61
+ "arguments": [],
62
+ "keyword": "Given ",
63
+ "line": 3,
64
+ "name": "I am on \"/about-us.html\"",
65
+ "match": {
66
+ "location": "tests/step-definitions/navigation.steps.js:64"
67
+ },
68
+ "result": {
69
+ "status": "passed",
70
+ "duration": 520204689
71
+ }
72
+ },
73
+ {
74
+ "arguments": [],
75
+ "keyword": "And ",
76
+ "line": 4,
77
+ "name": "the cookie \"session_id\" is set to \"abc123\"",
78
+ "match": {
79
+ "location": "tests/step-definitions/storage.steps.js:31"
80
+ },
81
+ "result": {
82
+ "status": "passed",
83
+ "duration": 2178232
84
+ }
85
+ },
86
+ {
87
+ "arguments": [],
88
+ "keyword": "Given ",
89
+ "line": 5,
90
+ "name": "the API base URL is \"http://localhost:8990\"",
91
+ "match": {
92
+ "location": "tests/step-definitions/api.steps.js:101"
93
+ },
94
+ "result": {
95
+ "status": "passed",
96
+ "duration": 449720
97
+ }
98
+ },
99
+ {
100
+ "arguments": [],
101
+ "keyword": "And ",
102
+ "line": 6,
103
+ "name": "I am authenticating as \"tester\" with \"secret\" password",
104
+ "match": {
105
+ "location": "tests/step-definitions/api.steps.js:66"
106
+ },
107
+ "result": {
108
+ "status": "passed",
109
+ "duration": 356252
110
+ }
111
+ },
112
+ {
113
+ "arguments": [
114
+ {
115
+ "content": "{\"a\": 1}",
116
+ "line": 8
117
+ }
118
+ ],
119
+ "keyword": "When ",
120
+ "line": 7,
121
+ "name": "I send a POST request to \"/echo\" with body:",
122
+ "match": {
123
+ "location": "tests/step-definitions/api.steps.js:314"
124
+ },
125
+ "result": {
126
+ "status": "passed",
127
+ "duration": 15836827
128
+ }
129
+ },
130
+ {
131
+ "arguments": [],
132
+ "keyword": "Then ",
133
+ "line": 11,
134
+ "name": "the JSON response should have \"cookie\" equal to \"none\"",
135
+ "match": {
136
+ "location": "tests/step-definitions/api.steps.js:548"
137
+ },
138
+ "result": {
139
+ "status": "passed",
140
+ "duration": 230198
141
+ }
142
+ },
143
+ {
144
+ "arguments": [],
145
+ "keyword": "And ",
146
+ "line": 12,
147
+ "name": "the JSON response should have \"auth\" equal to \"Basic dGVzdGVyOnNlY3JldA==\"",
148
+ "match": {
149
+ "location": "tests/step-definitions/api.steps.js:548"
150
+ },
151
+ "result": {
152
+ "status": "passed",
153
+ "duration": 134050
154
+ }
155
+ },
156
+ {
157
+ "arguments": [],
158
+ "keyword": "And ",
159
+ "line": 13,
160
+ "name": "the JSON response should have \"type\" equal to \"application/json\"",
161
+ "match": {
162
+ "location": "tests/step-definitions/api.steps.js:548"
163
+ },
164
+ "result": {
165
+ "status": "passed",
166
+ "duration": 146486
167
+ }
168
+ },
169
+ {
170
+ "arguments": [],
171
+ "keyword": "And ",
172
+ "line": 14,
173
+ "name": "a cookie with the name \"from_api\" should not exist",
174
+ "match": {
175
+ "location": "tests/step-definitions/cookie.steps.js:132"
176
+ },
177
+ "result": {
178
+ "status": "passed",
179
+ "duration": 1479906
180
+ }
181
+ },
182
+ {
183
+ "arguments": [
184
+ {
185
+ "content": "a=1&b=2",
186
+ "line": 16
187
+ }
188
+ ],
189
+ "keyword": "When ",
190
+ "line": 15,
191
+ "name": "I send a REST \"POST\" request to \"http://localhost:8990/echo\" with body:",
192
+ "match": {
193
+ "location": "tests/step-definitions/rest.steps.js:88"
194
+ },
195
+ "result": {
196
+ "status": "passed",
197
+ "duration": 7299009
198
+ }
199
+ },
200
+ {
201
+ "arguments": [],
202
+ "keyword": "Then ",
203
+ "line": 19,
204
+ "name": "the REST response should contain \"session_id=abc123\"",
205
+ "match": {
206
+ "location": "tests/step-definitions/rest.steps.js:118"
207
+ },
208
+ "result": {
209
+ "status": "passed",
210
+ "duration": 308369
211
+ }
212
+ },
213
+ {
214
+ "arguments": [],
215
+ "keyword": "And ",
216
+ "line": 20,
217
+ "name": "the REST response should contain \"application/x-www-form-urlencoded\"",
218
+ "match": {
219
+ "location": "tests/step-definitions/rest.steps.js:118"
220
+ },
221
+ "result": {
222
+ "status": "passed",
223
+ "duration": 187290
224
+ }
225
+ },
226
+ {
227
+ "arguments": [],
228
+ "keyword": "And ",
229
+ "line": 21,
230
+ "name": "the REST response should contain \"a=1&b=2\"",
231
+ "match": {
232
+ "location": "tests/step-definitions/rest.steps.js:118"
233
+ },
234
+ "result": {
235
+ "status": "passed",
236
+ "duration": 100281
237
+ }
238
+ },
239
+ {
240
+ "arguments": [],
241
+ "keyword": "And ",
242
+ "line": 22,
243
+ "name": "a cookie with the name \"from_api\" should exist",
244
+ "match": {
245
+ "location": "tests/step-definitions/cookie.steps.js:32"
246
+ },
247
+ "result": {
248
+ "status": "passed",
249
+ "duration": 1266994
250
+ }
251
+ },
252
+ {
253
+ "keyword": "After",
254
+ "hidden": true,
255
+ "result": {
256
+ "status": "passed",
257
+ "duration": 97005
258
+ }
259
+ },
260
+ {
261
+ "keyword": "After",
262
+ "hidden": true,
263
+ "result": {
264
+ "status": "passed",
265
+ "duration": 18792075
266
+ }
267
+ }
268
+ ],
269
+ "tags": [],
270
+ "type": "scenario"
271
+ }
272
+ ],
273
+ "id": "cookie-check-(not-committed)",
274
+ "line": 1,
275
+ "keyword": "Feature",
276
+ "name": "cookie check (not committed)",
277
+ "tags": [],
278
+ "uri": ".scratch/features/cookies.feature"
279
+ }
280
+ ]
@@ -0,0 +1,163 @@
1
+ import re,io
2
+ p='tests/step-definitions/webship.js'
3
+ s=open(p).read()
4
+ helper = r'''
5
+ // Transport settings of the browser context that a request sent outside the
6
+ // browser session still has to follow: the HTTPS setting, the proxy and the
7
+ // client certificates. Nothing of the session itself (cookies, storage,
8
+ // credentials, extra headers) is in this list.
9
+ function transportOptions() {
10
+ const from = playwrightConfig.contextOptions || {};
11
+ const options = {};
12
+ for (const key of ['ignoreHTTPSErrors', 'proxy', 'clientCertificates']) {
13
+ if (from[key] !== undefined) options[key] = from[key];
14
+ }
15
+ return options;
16
+ }
17
+
18
+ function hasHeader(headers, name) {
19
+ const wanted = name.toLowerCase();
20
+ return Object.keys(headers).some((key) => key.toLowerCase() === wanted);
21
+ }
22
+
23
+ /**
24
+ * Send an HTTP request with Playwright's request API and return the answer
25
+ * as `{ status, statusText, headers, data, config }`: header names are in
26
+ * lower case and `data` is the parsed JSON when the body is JSON, the body
27
+ * text otherwise. No status code throws.
28
+ *
29
+ * `session: true` sends the request through the browser context, so it
30
+ * shares the cookies, the credentials and the extra headers of the page.
31
+ * `session: false` sends it from a request context of its own: no cookie of
32
+ * the browser goes with it and none comes back into the browser. Both follow
33
+ * the HTTPS setting of the browser context, so a self-signed or locally
34
+ * issued certificate is accepted whenever the browser accepts it.
35
+ *
36
+ * @param {Object} world
37
+ * @param {{method: string, url: string, headers?: Object, data?: *, session?: boolean}} request
38
+ * @returns {Promise<{status: number, statusText: string, headers: Object, data: *, config: Object}>}
39
+ */
40
+ async function sendRequest(world, { method, url, headers, data, session }) {
41
+ const verb = String(method || 'GET').toUpperCase();
42
+ const sent = Object.assign({}, headers || {});
43
+ if (!hasHeader(sent, 'accept')) sent.Accept = 'application/json, text/plain, */*';
44
+ const options = { method: verb, headers: sent, failOnStatusCode: false };
45
+ if (data !== undefined && data !== null) {
46
+ options.data = data;
47
+ // A raw text body with no declared type goes out as a form body.
48
+ if (typeof data === 'string' && !hasHeader(sent, 'content-type') && /^(POST|PUT|PATCH)$/.test(verb)) {
49
+ sent['Content-Type'] = 'application/x-www-form-urlencoded';
50
+ }
51
+ }
52
+ const shared = session && world && world.context ? world.context.request : null;
53
+ const own = shared ? null : await playwright.request.newContext(transportOptions());
54
+ try {
55
+ const response = await (shared || own).fetch(url, options);
56
+ const text = await response.text();
57
+ let body = text;
58
+ try { body = JSON.parse(text); } catch (e) { /* not JSON: keep the text */ }
59
+ return {
60
+ status: response.status(),
61
+ statusText: response.statusText(),
62
+ headers: response.headers(),
63
+ data: body,
64
+ config: { method: verb.toLowerCase(), url },
65
+ };
66
+ } finally {
67
+ if (own) await own.dispose();
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Navigate to a URL with friendly error messages for common failures'''
73
+ marker = '''
74
+ /**
75
+ * Navigate to a URL with friendly error messages for common failures'''
76
+ assert s.count(marker)==1
77
+ s=s.replace(marker,helper)
78
+ s=s.replace(" resolveUrl,\n fillField,"," resolveUrl,\n sendRequest,\n fillField,")
79
+ open(p,'w').write(s)
80
+
81
+ p='tests/step-definitions/rest.steps.js'
82
+ s=open(p).read()
83
+ s=s.replace("""// Lightweight REST testing — distinct from the existing api.steps.js steps
84
+ // Preserves exact step phrasing for compatibility.
85
+ ""","""// Lightweight REST testing — distinct from the existing api.steps.js steps
86
+ // Preserves exact step phrasing for compatibility.
87
+ //
88
+ // A REST request is sent through the browser context, so it follows the
89
+ // browser session: its cookies, its credentials and its HTTPS setting (a
90
+ // self-signed or locally issued certificate is accepted like in the page).
91
+ """)
92
+ s=s.replace("const axios = require('axios');\nconst { resolveUrl } = require('./webship');","const { resolveUrl, sendRequest, friendly } = require('./webship');")
93
+ s=s.replace("""function ensureState(world) {
94
+ if (!world._rest) world._rest = { headers: {}, response: null };
95
+ return world._rest;
96
+ }
97
+ ""","""function ensureState(world) {
98
+ if (!world._rest) world._rest = { headers: {}, response: null };
99
+ return world._rest;
100
+ }
101
+
102
+ async function sendRest(world, method, url, body) {
103
+ const s = ensureState(world);
104
+ const target = resolveUrl(world.launchUrl, url);
105
+ try {
106
+ s.response = await sendRequest(world, {
107
+ method, url: target, headers: s.headers, data: body, session: true,
108
+ });
109
+ } catch (e) {
110
+ throw friendly({
111
+ action: `send the REST ${method} request to "${target}"`,
112
+ cause: e,
113
+ hint: 'check the URL is reachable from the machine that runs the tests.',
114
+ });
115
+ }
116
+ }
117
+ """)
118
+ old1=""" const s = ensureState(this);
119
+ const target = resolveUrl(this.launchUrl, url);
120
+ s.response = await axios.request({
121
+ method, url: target, headers: s.headers, validateStatus: () => true,
122
+ });
123
+ """
124
+ old2=""" const s = ensureState(this);
125
+ const target = resolveUrl(this.launchUrl, url);
126
+ s.response = await axios.request({
127
+ method, url: target, headers: s.headers, data: body, validateStatus: () => true,
128
+ });
129
+ """
130
+ assert old1 in s and old2 in s
131
+ s=s.replace(old1," await sendRest(this, method, url);\n").replace(old2," await sendRest(this, method, url, body);\n")
132
+ open(p,'w').write(s)
133
+
134
+ p='tests/step-definitions/api.steps.js'
135
+ s=open(p).read()
136
+ s=s.replace("const axios = require('axios');\nconst assert = require('assert');\nconst { friendly } = require('./webship');","const assert = require('assert');\nconst { friendly, sendRequest } = require('./webship');")
137
+ s=s.replace("""// Global variables to store API response and request data""","""// The API steps talk to an API on its own terms: its own base URL, its own
138
+ // authentication and headers. A request is sent outside the browser session
139
+ // (no cookie of the browser goes with it), and it follows the HTTPS setting
140
+ // of the browser context, so a self-signed or locally issued certificate is
141
+ // accepted whenever the browser accepts it.
142
+ function sendApiRequest(world, method, url, headers, data) {
143
+ return sendRequest(world, { method, url, headers, data, session: false });
144
+ }
145
+
146
+ // Global variables to store API response and request data""")
147
+ pat=re.compile(r"""apiResponse = await axios\(\{
148
+ method: method,
149
+ url: url,
150
+ headers: (.*?),
151
+ data: (\w+(?:\.stringify\(fields\))?),
152
+ validateStatus: function \(status\) \{
153
+ return true; // Don't throw on any status code
154
+ \}
155
+ \}\);""",re.S)
156
+ def rep(m):
157
+ h=re.sub(r"\s+"," ",m.group(1)).replace("{ ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }","{ ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }")
158
+ return "apiResponse = await sendApiRequest(this, method, url, %s, %s);"%(h,m.group(2))
159
+ s,n=pat.subn(rep,s)
160
+ assert n==4,n
161
+ s,n=re.subn(r"throw friendly\(`\$\{method\} request( with \w+(?: data)?)? failed: \$\{error\.message\}`\);", lambda m:"throw friendly(`${method} request%s to \"${url}\" failed.`, error);"%(m.group(1) or ''), s)
162
+ assert n==4,n
163
+ open(p,'w').write(s)
package/.scratch/pr.md ADDED
@@ -0,0 +1,30 @@
1
+ The REST request steps (`I send a REST "<method>" request to "<url>"`, with and without a body) and the API request steps (`I send a <METHOD> request to "<endpoint>"`, plain and with a body, values or form data) now send their request with Playwright's request API instead of `axios` from Node, so they follow the HTTPS setting of the browser context and reach a site served with a self-signed or locally issued certificate. The REST steps go through the browser context and share its cookies; the API steps keep their own base URL, authentication and headers and carry no cookie of the browser. Step phrases and the status, header, body and JSON assertions that follow them are unchanged.
2
+
3
+ Closes #333
4
+
5
+ Proof: the new `tests/features/rest-and-api-requests.feature` (14 scenarios: GET and POST with a JSON body, 404 and 405 answers, a response header and JSON assertions, a path and an absolute URL) fails 7 of 14 before the change over HTTPS with a self-signed certificate (`self-signed certificate`) and passes after it. Full suite locally, over HTTP and over HTTPS with a self-signed certificate: 345 scenarios, 1652 steps, all passed in both.
6
+
7
+ AI-Generated: Yes
8
+
9
+ ### Checkpoints:
10
+ - [x] File an issue
11
+ - [x] Addition/Change/Update/Fix
12
+ - [x] Testing to ensure no regression
13
+ - [ ] Automated unit testing coverage
14
+ - [x] Automated functional testing coverage
15
+ - [ ] UX/UI designer responsibilities
16
+ - [ ] Readability
17
+ - [ ] Accessibility
18
+ - [ ] Performance
19
+ - [ ] Security
20
+ - [ ] Developer Documentation
21
+ - [x] User Guide Documentation
22
+ - [ ] Reviewed by human
23
+ - [ ] Code review by maintainers
24
+ - [ ] Full testing and approval
25
+ - [ ] Credit contributors
26
+ - [ ] Review with the product owner
27
+ - [ ] Release notes snippet
28
+ - [ ] Release
29
+
30
+ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
@@ -1,6 +1,13 @@
1
1
  # API Testing
2
2
 
3
- Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. The browser context's cookie jar is shared, so a UI login can be followed by REST-level assertions in the same scenario.
3
+ Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. Every request is sent with Playwright's request API and follows the HTTPS setting of the browser context (`ignoreHTTPSErrors` in `playwright.config.ts`), so a site served with a self-signed or locally issued certificate answers these steps the same way it answers the browser.
4
+
5
+ There are two families of steps, and they differ in one point:
6
+
7
+ | Steps | Sent | Cookies of the browser |
8
+ | --- | --- | --- |
9
+ | `I send a REST "<method>" request to "<url>"` (`rest.steps.js`) | through the browser context | shared: a UI login can be followed by REST-level assertions in the same scenario |
10
+ | `I send a <METHOD> request to "<endpoint>"` (`api.steps.js`) | outside the browser session | not sent, and none comes back into the browser: the request carries only the base URL, the authentication and the headers the scenario set |
4
11
 
5
12
  ## Setting headers and base URL
6
13
 
@@ -96,7 +103,7 @@ Given the basic authentication with the username "admin" and the password "secre
96
103
 
97
104
  ## Short-form REST steps
98
105
 
99
- A parallel short-form lives in `rest.steps.js` for quick smoke tests:
106
+ A parallel short-form lives in `rest.steps.js` for quick smoke tests. A path is joined to the launch URL, an absolute URL is requested as written, and the request goes through the browser context: it shares the session cookies and the HTTPS setting of the page.
100
107
 
101
108
  ```gherkin
102
109
  Given a REST header "Authorization" with value "Bearer abc123"
@@ -4,10 +4,21 @@
4
4
  ## Overview
5
5
 
6
6
  webship-js ships native HTTP / REST API step definitions powered by
7
- Playwright's request fixture and `axios`. They share the browser
8
- context's cookie jar so the same scenario can mix UI actions and
7
+ Playwright's request API, so the same scenario can mix UI actions and
9
8
  direct API calls.
10
9
 
10
+ The API steps on this page send their requests outside the browser
11
+ session: a request carries the base URL, the authentication and the
12
+ headers the scenario set, and no cookie of the browser. They follow the
13
+ HTTPS setting of the browser context (`ignoreHTTPSErrors` in
14
+ `playwright.config.ts`), so an API served with a self-signed or locally
15
+ issued certificate answers them the same way it answers the browser.
16
+
17
+ To send a request with the cookies of the browser session, after a UI
18
+ login for example, use the short-form REST steps
19
+ (`When I send a REST "GET" request to "/api/me"`), which go through the
20
+ browser context.
21
+
11
22
  ## Available API Step Definitions
12
23
 
13
24
  The documentation lists 22 distinct step definition categories:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webship-js",
3
- "version": "2.0.9",
3
+ "version": "2.0.10",
4
4
  "description": "Webship JS automated functional testing.",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -0,0 +1,120 @@
1
+ Feature: REST and API requests follow the HTTPS setting of the browser
2
+ As a tester,
3
+ I want the REST and API request steps to reach every site the browser reaches
4
+ So that they work on a site served over HTTPS with a self-signed or locally issued certificate
5
+
6
+ Background:
7
+ Given the API base URL is "/"
8
+
9
+ Scenario: A REST GET request to a path
10
+ When I send a REST "GET" request to "/example-api.json"
11
+ Then the REST response status code should be 200
12
+ And the REST response should contain "JSON:API paints my bikeshed!"
13
+ And the REST response should contain "articles"
14
+
15
+ Scenario: A REST GET request to an absolute URL
16
+ When I send a REST "GET" request to "http://localhost:8080/example-api.json"
17
+ Then the REST response status code should be 200
18
+ And the REST response should contain "The shortest article. Ever."
19
+
20
+ Scenario: A REST GET request to a missing page answers 404
21
+ When I send a REST "GET" request to "/not-existing-page.json"
22
+ Then the REST response status code should be 404
23
+
24
+ Scenario: A REST request after the browser opened a page
25
+ Given I am on "/about-us.html"
26
+ When we send a REST "GET" request to "/example-api.json"
27
+ Then the REST response status code should be 200
28
+ And the REST response should contain "people"
29
+ And I should see "About Us"
30
+
31
+ Scenario: A REST POST request with a JSON body to a path that takes no POST
32
+ Given a REST header "Content-Type" with value "application/json"
33
+ When I send a REST "POST" request to "/example-api.json" with body:
34
+ """
35
+ {"title": "Webship", "body": "A REST request with a JSON body"}
36
+ """
37
+ Then the REST response status code should be 405
38
+
39
+ Scenario: A REST POST request with a JSON body to an absolute URL
40
+ Given a REST header "Content-Type" with value "application/json"
41
+ When I send a REST "POST" request to "https://jsonplaceholder.typicode.com/posts" with body:
42
+ """
43
+ {"title": "Webship", "body": "A REST request with a JSON body", "userId": 1}
44
+ """
45
+ Then the REST response status code should be 201
46
+ And the REST response should contain "Webship"
47
+ And the REST response should contain "A REST request with a JSON body"
48
+ And the REST response should contain "id"
49
+
50
+ Scenario: A REST DELETE request to an absolute URL
51
+ When I send a REST "DELETE" request to "https://jsonplaceholder.typicode.com/posts/1"
52
+ Then the REST response status code should be 200
53
+
54
+ Scenario: An API GET request to a path under the launch URL
55
+ When I send a GET request to "/example-api.json"
56
+ Then the API response code should be 200
57
+ And the response should be valid JSON
58
+ And the response header "Content-Type" should contain "application/json"
59
+ And the JSON response should have "data.0.type" equal to "articles"
60
+ And the JSON response should have "included.0.attributes.name" equal to "John"
61
+ And the JSON property "included.0.attributes.age" should be 80
62
+ And the JSON response should have property "data.0.relationships.author"
63
+ And the JSON response should not have property "data.0.password"
64
+ And the API response should contain "JSON:API paints my bikeshed!"
65
+
66
+ Scenario: An API GET request to an absolute base URL
67
+ Given the API base URL is "http://localhost:8080"
68
+ When I send a GET request to "/example-api.json"
69
+ Then the API response code should be 200
70
+ And the header "Content-Type" should contain "application/json"
71
+ And the JSON response should have "data.0.id" equal to "1"
72
+
73
+ Scenario: An API GET request to a missing page answers 404
74
+ When I send a GET request to "/not-existing-page.json"
75
+ Then the API response code should be 404
76
+ And the API response should not contain "articles"
77
+
78
+ Scenario: An API POST request with a JSON body to a path that takes no POST
79
+ When I send a POST request to "/example-api.json" with body:
80
+ """
81
+ {"title": "Webship", "body": "An API request with a JSON body"}
82
+ """
83
+ Then the API response code should be 405
84
+
85
+ Scenario: An API POST request with a JSON body to an absolute base URL
86
+ Given the API base URL is "https://jsonplaceholder.typicode.com"
87
+ When I send a POST request to "/posts" with body:
88
+ """
89
+ {"title": "Webship", "body": "An API request with a JSON body", "userId": 1}
90
+ """
91
+ Then the API response code should be 201
92
+ And the response should be valid JSON
93
+ And the response header "Content-Type" should contain "application/json"
94
+ And the JSON response should have "title" equal to "Webship"
95
+ And the JSON response should have "userId" equal to 1
96
+ And the JSON response should have property "id"
97
+
98
+ Scenario: An API POST request with values and with form data
99
+ Given the API base URL is "https://jsonplaceholder.typicode.com"
100
+ When I send a POST request to "/posts" with values:
101
+ | title | Webship |
102
+ | body | Sent as values |
103
+ Then the API response code should be 201
104
+ And the JSON response should have "title" equal to "Webship"
105
+ When I send a POST request to "/posts" with form data:
106
+ """
107
+ title=Webship
108
+ body=Sent as form data
109
+ """
110
+ Then the API response code should be 201
111
+ And the JSON response should have "body" equal to "Sent as form data"
112
+
113
+ Scenario: An API request with a request body set before it and basic authentication
114
+ Given the API base URL is "https://jsonplaceholder.typicode.com"
115
+ And I am authenticating as "tester" with "secret" password
116
+ And I set the request body to '{"title": "Webship", "userId": 7}'
117
+ When I send a PUT request to "/posts/1"
118
+ Then the API response code should be 200
119
+ And the JSON response should have "title" equal to "Webship"
120
+ And the JSON response should have "userId" equal to 7
@@ -1,7 +1,15 @@
1
1
  const { Given, When, Then, Before } = require('@cucumber/cucumber');
2
- const axios = require('axios');
3
2
  const assert = require('assert');
4
- const { friendly } = require('./webship');
3
+ const { friendly, sendRequest } = require('./webship');
4
+
5
+ // The API steps talk to an API on its own terms: its own base URL, its own
6
+ // authentication and headers. A request is sent outside the browser session
7
+ // (no cookie of the browser goes with it), and it follows the HTTPS setting
8
+ // of the browser context, so a self-signed or locally issued certificate is
9
+ // accepted whenever the browser accepts it.
10
+ function sendApiRequest(world, method, url, headers, data) {
11
+ return sendRequest(world, { method, url, headers, data, session: false });
12
+ }
5
13
 
6
14
  // Global variables to store API response and request data
7
15
  let apiResponse = null;
@@ -223,17 +231,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)"$/, async function (method
223
231
  const url = baseURL + '/' + prepareUrl(endpoint);
224
232
 
225
233
  try {
226
- apiResponse = await axios({
227
- method: method,
228
- url: url,
229
- headers: apiHeaders,
230
- data: apiRequestData,
231
- validateStatus: function (status) {
232
- return true; // Don't throw on any status code
233
- }
234
- });
234
+ apiResponse = await sendApiRequest(this, method, url, apiHeaders, apiRequestData);
235
235
  } catch (error) {
236
- throw friendly(`${method} request failed: ${error.message}`);
236
+ throw friendly(`${method} request to "${url}" failed.`, error);
237
237
  }
238
238
  });
239
239
 
@@ -269,17 +269,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with values:$/, async fun
269
269
  });
270
270
 
271
271
  try {
272
- apiResponse = await axios({
273
- method: method,
274
- url: url,
275
- headers: { ...apiHeaders, 'Content-Type': 'application/json' },
276
- data: JSON.stringify(fields),
277
- validateStatus: function (status) {
278
- return true; // Don't throw on any status code
279
- }
280
- });
272
+ apiResponse = await sendApiRequest(this, method, url, { ...apiHeaders, 'Content-Type': 'application/json' }, JSON.stringify(fields));
281
273
  } catch (error) {
282
- throw friendly(`${method} request with values failed: ${error.message}`);
274
+ throw friendly(`${method} request with values to "${url}" failed.`, error);
283
275
  }
284
276
  });
285
277
 
@@ -342,17 +334,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with body:$/, async funct
342
334
  }
343
335
 
344
336
  try {
345
- apiResponse = await axios({
346
- method: method,
347
- url: url,
348
- headers: headers,
349
- data: requestData,
350
- validateStatus: function (status) {
351
- return true; // Don't throw on any status code
352
- }
353
- });
337
+ apiResponse = await sendApiRequest(this, method, url, headers, requestData);
354
338
  } catch (error) {
355
- throw friendly(`${method} request with body failed: ${error.message}`);
339
+ throw friendly(`${method} request with body to "${url}" failed.`, error);
356
340
  }
357
341
  });
358
342
 
@@ -404,20 +388,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with form data:$/, async
404
388
  .join('&');
405
389
 
406
390
  try {
407
- apiResponse = await axios({
408
- method: method,
409
- url: url,
410
- headers: {
411
- ...apiHeaders,
412
- 'Content-Type': 'application/x-www-form-urlencoded'
413
- },
414
- data: formData,
415
- validateStatus: function (status) {
416
- return true; // Don't throw on any status code
417
- }
418
- });
391
+ apiResponse = await sendApiRequest(this, method, url, { ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }, formData);
419
392
  } catch (error) {
420
- throw friendly(`${method} request with form data failed: ${error.message}`);
393
+ throw friendly(`${method} request with form data to "${url}" failed.`, error);
421
394
  }
422
395
  });
423
396
 
@@ -2,17 +2,36 @@
2
2
 
3
3
  // Lightweight REST testing — distinct from the existing api.steps.js steps
4
4
  // Preserves exact step phrasing for compatibility.
5
+ //
6
+ // A REST request is sent through the browser context, so it follows the
7
+ // browser session: its cookies, its credentials and its HTTPS setting (a
8
+ // self-signed or locally issued certificate is accepted like in the page).
5
9
 
6
10
  const { Given, When, Then } = require('@cucumber/cucumber');
7
11
  const assert = require('assert');
8
- const axios = require('axios');
9
- const { resolveUrl } = require('./webship');
12
+ const { resolveUrl, sendRequest, friendly } = require('./webship');
10
13
 
11
14
  function ensureState(world) {
12
15
  if (!world._rest) world._rest = { headers: {}, response: null };
13
16
  return world._rest;
14
17
  }
15
18
 
19
+ async function sendRest(world, method, url, body) {
20
+ const s = ensureState(world);
21
+ const target = resolveUrl(world.launchUrl, url);
22
+ try {
23
+ s.response = await sendRequest(world, {
24
+ method, url: target, headers: s.headers, data: body, session: true,
25
+ });
26
+ } catch (e) {
27
+ throw friendly({
28
+ action: `send the REST ${method} request to "${target}"`,
29
+ cause: e,
30
+ hint: 'check the URL is reachable from the machine that runs the tests.',
31
+ });
32
+ }
33
+ }
34
+
16
35
  /**
17
36
  * Add an HTTP header to subsequent REST requests.
18
37
  *
@@ -38,11 +57,7 @@ Given('a REST header {string} with value {string}', async function (name, value)
38
57
  *
39
58
  */
40
59
  When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pronoun, method, url) {
41
- const s = ensureState(this);
42
- const target = resolveUrl(this.launchUrl, url);
43
- s.response = await axios.request({
44
- method, url: target, headers: s.headers, validateStatus: () => true,
45
- });
60
+ await sendRest(this, method, url);
46
61
  });
47
62
 
48
63
  /**
@@ -71,11 +86,7 @@ When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pr
71
86
  *
72
87
  */
73
88
  When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)" with body:$/, async function (pronoun, method, url, body) {
74
- const s = ensureState(this);
75
- const target = resolveUrl(this.launchUrl, url);
76
- s.response = await axios.request({
77
- method, url: target, headers: s.headers, data: body, validateStatus: () => true,
78
- });
89
+ await sendRest(this, method, url, body);
79
90
  });
80
91
 
81
92
  /**
@@ -308,6 +308,72 @@ function resolveUrl(launchUrl, url) {
308
308
  return (launchUrl || '') + value;
309
309
  }
310
310
 
311
+ // Transport settings of the browser context that a request sent outside the
312
+ // browser session still has to follow: the HTTPS setting, the proxy and the
313
+ // client certificates. Nothing of the session itself (cookies, storage,
314
+ // credentials, extra headers) is in this list.
315
+ function transportOptions() {
316
+ const from = playwrightConfig.contextOptions || {};
317
+ const options = {};
318
+ for (const key of ['ignoreHTTPSErrors', 'proxy', 'clientCertificates']) {
319
+ if (from[key] !== undefined) options[key] = from[key];
320
+ }
321
+ return options;
322
+ }
323
+
324
+ function hasHeader(headers, name) {
325
+ const wanted = name.toLowerCase();
326
+ return Object.keys(headers).some((key) => key.toLowerCase() === wanted);
327
+ }
328
+
329
+ /**
330
+ * Send an HTTP request with Playwright's request API and return the answer
331
+ * as `{ status, statusText, headers, data, config }`: header names are in
332
+ * lower case and `data` is the parsed JSON when the body is JSON, the body
333
+ * text otherwise. No status code throws.
334
+ *
335
+ * `session: true` sends the request through the browser context, so it
336
+ * shares the cookies, the credentials and the extra headers of the page.
337
+ * `session: false` sends it from a request context of its own: no cookie of
338
+ * the browser goes with it and none comes back into the browser. Both follow
339
+ * the HTTPS setting of the browser context, so a self-signed or locally
340
+ * issued certificate is accepted whenever the browser accepts it.
341
+ *
342
+ * @param {Object} world
343
+ * @param {{method: string, url: string, headers?: Object, data?: *, session?: boolean}} request
344
+ * @returns {Promise<{status: number, statusText: string, headers: Object, data: *, config: Object}>}
345
+ */
346
+ async function sendRequest(world, { method, url, headers, data, session }) {
347
+ const verb = String(method || 'GET').toUpperCase();
348
+ const sent = Object.assign({}, headers || {});
349
+ if (!hasHeader(sent, 'accept')) sent.Accept = 'application/json, text/plain, */*';
350
+ const options = { method: verb, headers: sent, failOnStatusCode: false };
351
+ if (data !== undefined && data !== null) {
352
+ options.data = data;
353
+ // A raw text body with no declared type goes out as a form body.
354
+ if (typeof data === 'string' && !hasHeader(sent, 'content-type') && /^(POST|PUT|PATCH)$/.test(verb)) {
355
+ sent['Content-Type'] = 'application/x-www-form-urlencoded';
356
+ }
357
+ }
358
+ const shared = session && world && world.context ? world.context.request : null;
359
+ const own = shared ? null : await playwright.request.newContext(transportOptions());
360
+ try {
361
+ const response = await (shared || own).fetch(url, options);
362
+ const text = await response.text();
363
+ let body = text;
364
+ try { body = JSON.parse(text); } catch (e) { /* not JSON: keep the text */ }
365
+ return {
366
+ status: response.status(),
367
+ statusText: response.statusText(),
368
+ headers: response.headers(),
369
+ data: body,
370
+ config: { method: verb.toLowerCase(), url },
371
+ };
372
+ } finally {
373
+ if (own) await own.dispose();
374
+ }
375
+ }
376
+
311
377
  /**
312
378
  * Navigate to a URL with friendly error messages for common failures
313
379
  * (server down, DNS, empty response). All other errors rethrow with
@@ -629,6 +695,7 @@ module.exports = {
629
695
  buildSelector,
630
696
  gotoUrl,
631
697
  resolveUrl,
698
+ sendRequest,
632
699
  fillField,
633
700
  getLocatorText,
634
701
  pad,