webship-js 2.0.8 → 2.0.9

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.
@@ -5,6 +5,8 @@
5
5
 
6
6
  "This step definition is used after making a request to the web service under test. Once the response is received, this step definition asserts HTTP status code returned by the server matches the expected code."
7
7
 
8
+ The status is the one the browser received for the page it is showing, after any redirect. It is read from the browser session itself, so it follows the session cookies, the HTTPS settings of the browser context (a self-signed or locally issued certificate is fine) and any stubbed URL. A redirect is followed, so the step sees the status of the page the redirect ends on, never the 301 or 302 of the redirect itself.
9
+
8
10
  ## Gherkin Syntax
9
11
 
10
12
  ```
@@ -45,13 +47,13 @@ Then the response status code should be 200
45
47
  ### Example 2
46
48
 
47
49
  ```gherkin
48
- Then the response status code should be 404
50
+ And the response status code should not be 404
49
51
  ```
50
52
 
51
53
  ### Example 3
52
54
 
53
55
  ```gherkin
54
- And the response status code should be 301
56
+ Then the response status code should be 404
55
57
  ```
56
58
 
57
59
  ### Example 4
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Response status: redirected section</title>
6
+ </head>
7
+ <body>
8
+ <h1>Redirected section</h1>
9
+ <p>The address without the trailing slash redirects here.</p>
10
+ <p><a href="../response-status.html">Back to the response status page</a></p>
11
+ </body>
12
+ </html>
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Response status</title>
6
+ </head>
7
+ <body>
8
+ <h1>Response status</h1>
9
+ <p>Links to pages that answer with different HTTP status codes.</p>
10
+ <ul>
11
+ <li><a href="existing-page.html">Open the existing page</a></li>
12
+ <li><a href="not-existing-page.html">Open the missing page</a></li>
13
+ <li><a href="response-status">Open the redirected section</a></li>
14
+ <li><a href="#details">Jump to the details</a></li>
15
+ </ul>
16
+ <h2 id="details">Details</h2>
17
+ <p>The details are on this same page.</p>
18
+ </body>
19
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webship-js",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "Webship JS automated functional testing.",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -0,0 +1,108 @@
1
+ Feature: The response status code is the one the browser received
2
+ As a tester,
3
+ I want the response status and header steps to read the response of the page the browser is showing
4
+ So that they follow the browser session: its cookies, its HTTPS settings, redirects and stubbed pages
5
+
6
+ Scenario: Status after a direct visit
7
+ Given I am on "/existing-page.html"
8
+ Then the response status code should be 200
9
+ And the response status code should not be 404
10
+ And the response status code should not be 500
11
+
12
+ Scenario: Status of a missing page
13
+ Given I am on "/not-existing-page.html"
14
+ Then the response status code should be 404
15
+ And the response status code should not be 200
16
+
17
+ Scenario: Status after a redirect is the status of the page the redirect ends on
18
+ Given I am on "/response-status"
19
+ Then the path should be "/response-status/"
20
+ And I should see "Redirected section"
21
+ And the response status code should be 200
22
+ And the response status code should not be 302
23
+
24
+ Scenario: Status after clicking a link to an existing page
25
+ Given I am on "/response-status.html"
26
+ When I click "Open the existing page"
27
+ Then I should see "An example page that returns a 200 response code"
28
+ And the response status code should be 200
29
+
30
+ Scenario: Status after clicking a link to a missing page
31
+ Given I am on "/response-status.html"
32
+ Then the response status code should be 200
33
+ When I click "Open the missing page"
34
+ Then the path should be "/not-existing-page.html"
35
+ And the response status code should be 404
36
+ And the response status code should not be 200
37
+
38
+ Scenario: Status after clicking a link that redirects
39
+ Given I am on "/response-status.html"
40
+ When I click "Open the redirected section"
41
+ Then I should see "Redirected section"
42
+ And the response status code should be 200
43
+
44
+ Scenario: Status stays the same after a jump inside the page
45
+ Given I am on "/response-status.html"
46
+ When I click "Jump to the details"
47
+ Then the response status code should be 200
48
+ And the response status code should not be 404
49
+
50
+ Scenario: Status of a page that answers 403
51
+ Given I am on the homepage
52
+ And the URL "**/restricted.html" returns status 403 with body "Access denied"
53
+ When I go to "/restricted.html"
54
+ Then the response status code should be 403
55
+ And the response status code should not be 200
56
+
57
+ Scenario: Status of a page that answers 500
58
+ Given I am on the homepage
59
+ And the URL "**/broken.html" returns status 500 with body "Server error"
60
+ When I go to "/broken.html"
61
+ Then the response status code should be 500
62
+ And the response status code should not be 200
63
+
64
+ Scenario: Status follows the page after a visit to a missing page and a new visit
65
+ Given I am on "/not-existing-page.html"
66
+ Then the response status code should be 404
67
+ When I go to "/existing-page.html"
68
+ Then the response status code should be 200
69
+ And the response status code should not be 404
70
+
71
+ Scenario: Status after reloading the page
72
+ Given I am on "/existing-page.html"
73
+ When I reload the page
74
+ Then the response status code should be 200
75
+
76
+ Scenario: Status after moving back in the history
77
+ Given I am on "/existing-page.html"
78
+ When I go to "/not-existing-page.html"
79
+ Then the response status code should be 404
80
+ When I move backward one page
81
+ Then the path should be "/existing-page.html"
82
+ And the response status code should be 200
83
+ When I move forward one page
84
+ Then the response status code should be 404
85
+
86
+ Scenario: Status in a new anonymous session
87
+ Given I am an anonymous user
88
+ When I go to "/existing-page.html"
89
+ Then the response status code should be 200
90
+ When I go to "/not-existing-page.html"
91
+ Then the response status code should be 404
92
+
93
+ Scenario: Headers are read from the same response as the status
94
+ Given I am on "/existing-page.html"
95
+ Then the response should contain the header "content-type"
96
+ And the response header "content-type" should contain the value "html"
97
+ When I go to "/example-api.json"
98
+ Then the response status code should be 200
99
+ And the response header "content-type" should contain the value "application/json"
100
+ And the response header "content-type" should not contain the value "html"
101
+
102
+ Scenario: Status on a page opened after the browser session was replaced
103
+ Given the basic authentication with the username "tester" and the password "secret"
104
+ When I go to "/existing-page.html"
105
+ Then the response status code should be 200
106
+ And the response should contain the header "content-type"
107
+ When I go to "/not-existing-page.html"
108
+ Then the response status code should be 404
@@ -10,8 +10,8 @@
10
10
 
11
11
  const { Then } = require('@cucumber/cucumber');
12
12
  const assert = require('assert');
13
- const axios = require('axios');
14
13
  const { buildSelector, getLocatorText, friendly } = require('./webship');
14
+ const { getLastResponse } = require('./response.steps');
15
15
 
16
16
  // ---------------------------------------------------------------------------
17
17
  // Plain page-text
@@ -341,27 +341,23 @@ Then(/^(the )*response should( not)* contain "([^"]*)?"$/, async function (theCa
341
341
  /**
342
342
  * Assert that the current page's response status is or is not a given code.
343
343
  *
344
+ * The status is the one the browser received for the page it is showing
345
+ * (after any redirect), read from the browser session itself.
346
+ *
344
347
  * Example #1: Then the response status code should be 200
345
348
  * Example #2: And the response status code should not be 404
346
- * Example #3: Then the response status code should be 301
349
+ * Example #3: Then the response status code should be 404
350
+ * Example #4: Then the response status code should not be 500
351
+ * Example #5: When I am on "/admin"
352
+ * Then the response status code should be 403
347
353
  *
348
354
  */
349
355
  Then(/^(the )*response status code should( not)* be (\d+)$/, async function (theCase, notCase, expectedStatusCode) {
350
- const currentURL = this.page.url();
351
- try {
352
- const response = await axios.get(currentURL);
353
- const status = response.status;
354
- if (notCase) {
355
- assert.notStrictEqual(status, parseInt(expectedStatusCode), `Status code should NOT be ${expectedStatusCode}`);
356
- } else {
357
- assert.strictEqual(status, parseInt(expectedStatusCode), `Expected status ${expectedStatusCode} but got ${status}`);
358
- }
359
- } catch (error) {
360
- const status = error.response ? error.response.status : null;
361
- if (notCase) {
362
- if (status !== null) assert.notStrictEqual(status, parseInt(expectedStatusCode));
363
- } else {
364
- assert.strictEqual(status, parseInt(expectedStatusCode), `Expected status ${expectedStatusCode} but got ${status}`);
365
- }
356
+ const expected = parseInt(expectedStatusCode, 10);
357
+ const { status } = await getLastResponse(this);
358
+ if (notCase) {
359
+ assert.notStrictEqual(status, expected, `Status code should NOT be ${expected}`);
360
+ } else {
361
+ assert.strictEqual(status, expected, `Expected status ${expected} but got ${status}`);
366
362
  }
367
363
  });
@@ -1,38 +1,85 @@
1
1
  'use strict';
2
2
 
3
- // Inspect HTTP response headers using Playwright's request API on the current page URL.
3
+ // Inspect the HTTP response of the page the browser is showing: its status
4
+ // code and its headers. Both are read from the response the browser itself
5
+ // received for the main document, so they follow the browser session — its
6
+ // cookies, its HTTPS settings, any stubbed route — and never a second request
7
+ // sent from outside the browser.
4
8
 
5
9
  const { Then, Before } = require('@cucumber/cucumber');
6
10
  const assert = require('assert');
11
+ const { friendly } = require('./webship');
7
12
 
8
- // Capture response headers from navigation events, with page.request fallback.
9
- Before({ order: 100 }, async function () {
10
- if (!this.page) return;
11
- this._lastResponseHeaders = null;
12
- this._lastResponseStatus = null;
13
- this.page.on('response', (resp) => {
13
+ const trackedPages = new WeakSet();
14
+
15
+ // A URL without its fragment: "#section" never loads a new document.
16
+ function withoutHash(url) {
17
+ return String(url || '').split('#')[0];
18
+ }
19
+
20
+ // Record the response of every main-document navigation on the current page.
21
+ // Steps that swap the page (auth state, basic authentication, a new session)
22
+ // get the listener on their first use of the response steps.
23
+ function trackResponses(world) {
24
+ const page = world.page;
25
+ if (!page || trackedPages.has(page)) return;
26
+ trackedPages.add(page);
27
+ page.on('response', (resp) => {
14
28
  try {
15
- if (resp.request().isNavigationRequest() && resp.frame() === this.page.mainFrame()) {
16
- this._lastResponseHeaders = resp.headers();
17
- this._lastResponseStatus = resp.status();
29
+ if (resp.request().isNavigationRequest() && resp.frame() === page.mainFrame()) {
30
+ world._lastResponse = { page, url: resp.url(), status: resp.status(), headers: resp.headers() };
31
+ world._lastResponseHeaders = world._lastResponse.headers;
32
+ world._lastResponseStatus = world._lastResponse.status;
18
33
  }
19
34
  } catch (e) { /* ignore */ }
20
35
  });
36
+ }
37
+
38
+ Before({ order: 100 }, async function () {
39
+ this._lastResponse = null;
40
+ this._lastResponseHeaders = null;
41
+ this._lastResponseStatus = null;
42
+ trackResponses(this);
21
43
  });
22
44
 
23
- async function getHeaders(world) {
24
- if (world._lastResponseHeaders) return world._lastResponseHeaders;
25
- // Fallback: fetch current URL via Playwright request context to get headers.
26
- const url = world.page.url();
27
- if (!url || url === 'about:blank') return {};
45
+ /**
46
+ * The response of the document the page is showing: { status, headers }.
47
+ *
48
+ * The recorded navigation response answers when it belongs to the current
49
+ * page and URL. Anything else — a page opened before the listener was there,
50
+ * a history move served from the browser cache, a URL changed by the History
51
+ * API — is asked for again through the browser context, which shares the
52
+ * session cookies and the HTTPS settings of the page.
53
+ */
54
+ async function getLastResponse(world) {
55
+ const page = world.page;
56
+ if (!page) throw friendly('No page is open, so there is no response to read.');
57
+ trackResponses(world);
58
+ const url = page.url();
59
+ const last = world._lastResponse;
60
+ // 304 Not Modified only says the cached copy is still good: ask again for
61
+ // the status of the document itself.
62
+ if (last && last.page === page && last.status !== 304 && withoutHash(last.url) === withoutHash(url)) {
63
+ return last;
64
+ }
65
+ if (!url || url === 'about:blank') {
66
+ throw friendly({
67
+ message: 'No page has been opened yet, so there is no response to read.',
68
+ hint: 'open a page first, for example: Given I am on "/".',
69
+ });
70
+ }
28
71
  try {
29
- const resp = await world.page.request.get(url);
30
- return resp.headers();
72
+ const resp = await page.request.get(url);
73
+ return { page, url, status: resp.status(), headers: resp.headers() };
31
74
  } catch (e) {
32
- return {};
75
+ throw friendly(`Could not read the response of "${url}".`, e);
33
76
  }
34
77
  }
35
78
 
79
+ async function getHeaders(world) {
80
+ return (await getLastResponse(world)).headers || {};
81
+ }
82
+
36
83
  /**
37
84
  * Assert the most recent navigation response carried a header (case-insensitive).
38
85
  *
@@ -96,3 +143,5 @@ Then('the response header {string} should not contain the value {string}', async
96
143
  if (!key) return;
97
144
  assert.ok(String(h[key]).indexOf(value) === -1, `Header "${name}" should not contain "${value}".`);
98
145
  });
146
+
147
+ module.exports = { getLastResponse };