webship-js 2.0.7 → 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.
- package/docs/global-settings.md +1 -1
- package/docs/step-definitions/given-i-am-specific-page.md +4 -0
- package/docs/step-definitions/then-response-status-code-should-be-number.md +4 -2
- package/docs/step-definitions/when-i-go-specific-page.md +4 -0
- package/examples/response-status/index.html +12 -0
- package/examples/response-status.html +19 -0
- package/package.json +1 -1
- package/tests/features/absolute-url.feature +51 -0
- package/tests/features/response-status-code.feature +108 -0
- package/tests/step-definitions/assertion.steps.js +14 -18
- package/tests/step-definitions/file-download.steps.js +2 -2
- package/tests/step-definitions/navigation.steps.js +8 -5
- package/tests/step-definitions/response.steps.js +67 -18
- package/tests/step-definitions/rest.steps.js +3 -2
- package/tests/step-definitions/webship.js +16 -0
package/docs/global-settings.md
CHANGED
|
@@ -42,7 +42,7 @@ on the command line).
|
|
|
42
42
|
| --- | --- | --- |
|
|
43
43
|
| string | `http://localhost:8080` | `LAUNCH_URL` |
|
|
44
44
|
|
|
45
|
-
Base URL prepended to every relative path in `Given I am on "/path"`.
|
|
45
|
+
Base URL prepended to every relative path in `Given I am on "/path"`. An absolute `http://` or `https://` URL is opened as written.
|
|
46
46
|
|
|
47
47
|
### `assetsFolder`
|
|
48
48
|
|
|
@@ -25,8 +25,12 @@ Given we are on the "/about-us" page
|
|
|
25
25
|
Given on "/about-us"
|
|
26
26
|
|
|
27
27
|
Given on the "/about-us" page
|
|
28
|
+
|
|
29
|
+
Given I am on "https://un.org"
|
|
28
30
|
```
|
|
29
31
|
|
|
32
|
+
A path is opened under the `launchUrl`; an absolute `http://` or `https://` URL is opened as written.
|
|
33
|
+
|
|
30
34
|
## Key Information
|
|
31
35
|
|
|
32
36
|
This step definition is part of the Webship JS 2.0.x documentation's Step Definitions section. It supports flexible phrasing patterns, allowing users to specify page locations using various grammatical structures. The pattern accepts optional articles ("the") and the word "page," accommodating different natural language preferences while directing the test to navigate to the specified URL or page identifier.
|
|
@@ -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
|
-
|
|
50
|
+
And the response status code should not be 404
|
|
49
51
|
```
|
|
50
52
|
|
|
51
53
|
### Example 3
|
|
52
54
|
|
|
53
55
|
```gherkin
|
|
54
|
-
|
|
56
|
+
Then the response status code should be 404
|
|
55
57
|
```
|
|
56
58
|
|
|
57
59
|
### Example 4
|
|
@@ -27,8 +27,12 @@ When navigating to "/products"
|
|
|
27
27
|
When we go to "/products"
|
|
28
28
|
|
|
29
29
|
When we navigate to "/terms"
|
|
30
|
+
|
|
31
|
+
When I go to "https://un.org"
|
|
30
32
|
```
|
|
31
33
|
|
|
34
|
+
A path is opened under the `launchUrl`; an absolute `http://` or `https://` URL is opened as written.
|
|
35
|
+
|
|
32
36
|
## Key Details
|
|
33
37
|
|
|
34
38
|
This step accepts flexible phrasing with multiple variations including "go," "navigate," and collective pronouns ("we"). The page identifier is captured as a parameter in quotation marks and can represent either a page name or a complete URL path.
|
|
@@ -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
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Feature: Steps that take a URL accept a path or an absolute URL
|
|
2
|
+
As a tester,
|
|
3
|
+
I want a step given "/page" to open it under the launch URL,
|
|
4
|
+
and a step given "http://host/page" to open that URL as written,
|
|
5
|
+
so that a scenario can leave the site under test and come back.
|
|
6
|
+
|
|
7
|
+
Scenario: A path is joined to the launch URL
|
|
8
|
+
Given I am on "/about-us.html"
|
|
9
|
+
Then the path should be "/about-us.html"
|
|
10
|
+
And I should see "About Us"
|
|
11
|
+
|
|
12
|
+
Scenario: An absolute URL is opened as written
|
|
13
|
+
Given I am on "http://localhost:8080/contact-us.html"
|
|
14
|
+
Then the path should be "/contact-us.html"
|
|
15
|
+
And I should see "Contact Us"
|
|
16
|
+
|
|
17
|
+
Scenario: An absolute URL is opened as written, with the page phrasing
|
|
18
|
+
Given I am on the "http://localhost:8080/path.html?lang=en" page
|
|
19
|
+
Then the path should be "/path.html"
|
|
20
|
+
And current url should have the "lang" parameter with the "en" value
|
|
21
|
+
|
|
22
|
+
Scenario: Go to a path, then to an absolute URL
|
|
23
|
+
Given I am on the homepage
|
|
24
|
+
When I go to "/about-us.html"
|
|
25
|
+
Then the path should be "/about-us.html"
|
|
26
|
+
When I navigate to "http://localhost:8080/contact-us.html"
|
|
27
|
+
Then the path should be "/contact-us.html"
|
|
28
|
+
And I should see "Contact Us"
|
|
29
|
+
|
|
30
|
+
Scenario: Go to an absolute URL with the other phrasings
|
|
31
|
+
Given I am on the homepage
|
|
32
|
+
When we go to "http://localhost:8080/path.html"
|
|
33
|
+
Then the path should be "/path.html"
|
|
34
|
+
When navigating to "http://localhost:8080/element.html"
|
|
35
|
+
Then the path should be "/element.html"
|
|
36
|
+
|
|
37
|
+
Scenario: An absolute URL is case-insensitive in its scheme
|
|
38
|
+
Given I am on "HTTP://localhost:8080/path.html"
|
|
39
|
+
Then the path should be "/path.html"
|
|
40
|
+
|
|
41
|
+
Scenario: A REST request takes a path or an absolute URL
|
|
42
|
+
When I send a REST "GET" request to "/example-api.json"
|
|
43
|
+
Then the REST response status code should be 200
|
|
44
|
+
When I send a REST "GET" request to "http://localhost:8080/example-api.json"
|
|
45
|
+
Then the REST response status code should be 200
|
|
46
|
+
|
|
47
|
+
Scenario: A download takes a path or an absolute URL
|
|
48
|
+
When I download the file from the URL "/example-api.json"
|
|
49
|
+
Then the downloaded file name should be "example-api.json"
|
|
50
|
+
When I download the file from the URL "http://localhost:8080/example-api.json"
|
|
51
|
+
Then the downloaded file name should be "example-api.json"
|
|
@@ -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
|
|
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
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { friendly } = require('./webship');
|
|
3
|
+
const { friendly, resolveUrl } = require('./webship');
|
|
4
4
|
|
|
5
5
|
// Validate file downloads triggered via URL or link click.
|
|
6
6
|
|
|
@@ -31,7 +31,7 @@ function tmpDownloadDir() {
|
|
|
31
31
|
*
|
|
32
32
|
*/
|
|
33
33
|
When(/^(I |we )*download the file from the URL "([^"]*)"$/, async function (pronoun, url) {
|
|
34
|
-
const target =
|
|
34
|
+
const target = resolveUrl(this.launchUrl, url);
|
|
35
35
|
try {
|
|
36
36
|
const resp = await this.page.request.get(target);
|
|
37
37
|
assert.strictEqual(resp.status(), 200, `Download failed with status ${resp.status()}.`);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
const { Given, When, Then } = require('@cucumber/cucumber');
|
|
8
8
|
const assert = require('assert');
|
|
9
|
-
const { gotoUrl, waitForPageLoad, smartSettle } = require('./webship');
|
|
9
|
+
const { gotoUrl, resolveUrl, waitForPageLoad, smartSettle } = require('./webship');
|
|
10
10
|
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
12
12
|
// Sessions / visits
|
|
@@ -49,7 +49,8 @@ Given(/^(I am |we are )?on( the)* (homepage|frontpage)$/, async function (pronou
|
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Open a specific page under the launch URL
|
|
52
|
+
* Open a specific page: a path is opened under the launch URL, an absolute
|
|
53
|
+
* `http://` or `https://` URL is opened as written.
|
|
53
54
|
*
|
|
54
55
|
* Example #1: Given I am on "/about-us.html"
|
|
55
56
|
* Example #2: Given I am on the "/about-us.html" page
|
|
@@ -61,7 +62,7 @@ Given(/^(I am |we are )?on( the)* (homepage|frontpage)$/, async function (pronou
|
|
|
61
62
|
*
|
|
62
63
|
*/
|
|
63
64
|
Given(/^(I am |we are )*on( the)* "([^"]*)?"( page)*$/, async function (pronounCase, theCase, url, pageCase) {
|
|
64
|
-
await gotoUrl(this.page, this.launchUrl
|
|
65
|
+
await gotoUrl(this.page, resolveUrl(this.launchUrl, url));
|
|
65
66
|
await this.page.waitForSelector('body', { state: 'attached', timeout: 10000 });
|
|
66
67
|
await waitForPageLoad(this.page);
|
|
67
68
|
});
|
|
@@ -87,7 +88,8 @@ When(/^(I go |I navigate |we go |we navigate |navigating )?to( the)* (homepage|f
|
|
|
87
88
|
});
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
|
-
* Navigate to a specific page
|
|
91
|
+
* Navigate to a specific page: a path is opened under the launch URL, an
|
|
92
|
+
* absolute `http://` or `https://` URL is opened as written.
|
|
91
93
|
*
|
|
92
94
|
* Example #1: When I go to "/contact-us.html"
|
|
93
95
|
* Example #2: When I go to "/user/login"
|
|
@@ -95,11 +97,12 @@ When(/^(I go |I navigate |we go |we navigate |navigating )?to( the)* (homepage|f
|
|
|
95
97
|
* Example #4: When navigating to "/products"
|
|
96
98
|
* Example #5: When we go to "/products"
|
|
97
99
|
* Example #6: When we navigate to "/terms"
|
|
100
|
+
* Example #7: When I go to "https://un.org"
|
|
98
101
|
*
|
|
99
102
|
*/
|
|
100
103
|
When(/^(I go |I navigate |we go |we navigate |navigating )?to "([^"]*)?"$/, async function (pronounCase, url) {
|
|
101
104
|
const defaultTime = this.minWaitTime.page || 3000;
|
|
102
|
-
await gotoUrl(this.page, this.launchUrl
|
|
105
|
+
await gotoUrl(this.page, resolveUrl(this.launchUrl, url));
|
|
103
106
|
await this.page.waitForSelector('body', { state: 'attached', timeout: defaultTime });
|
|
104
107
|
await waitForPageLoad(this.page, defaultTime);
|
|
105
108
|
});
|
|
@@ -1,38 +1,85 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// Inspect HTTP response
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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() ===
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
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
|
-
|
|
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 };
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
const { Given, When, Then } = require('@cucumber/cucumber');
|
|
7
7
|
const assert = require('assert');
|
|
8
8
|
const axios = require('axios');
|
|
9
|
+
const { resolveUrl } = require('./webship');
|
|
9
10
|
|
|
10
11
|
function ensureState(world) {
|
|
11
12
|
if (!world._rest) world._rest = { headers: {}, response: null };
|
|
@@ -38,7 +39,7 @@ Given('a REST header {string} with value {string}', async function (name, value)
|
|
|
38
39
|
*/
|
|
39
40
|
When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pronoun, method, url) {
|
|
40
41
|
const s = ensureState(this);
|
|
41
|
-
const target =
|
|
42
|
+
const target = resolveUrl(this.launchUrl, url);
|
|
42
43
|
s.response = await axios.request({
|
|
43
44
|
method, url: target, headers: s.headers, validateStatus: () => true,
|
|
44
45
|
});
|
|
@@ -71,7 +72,7 @@ When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pr
|
|
|
71
72
|
*/
|
|
72
73
|
When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)" with body:$/, async function (pronoun, method, url, body) {
|
|
73
74
|
const s = ensureState(this);
|
|
74
|
-
const target =
|
|
75
|
+
const target = resolveUrl(this.launchUrl, url);
|
|
75
76
|
s.response = await axios.request({
|
|
76
77
|
method, url: target, headers: s.headers, data: body, validateStatus: () => true,
|
|
77
78
|
});
|
|
@@ -293,6 +293,21 @@ function buildSelector(attrValue, attr) {
|
|
|
293
293
|
// empty-response edge cases so a redirect to about:blank or a 204 does
|
|
294
294
|
// not crash the suite. All other failures rethrow with extra context
|
|
295
295
|
// (URL + likely cause).
|
|
296
|
+
/**
|
|
297
|
+
* Resolve the value a step was given into the URL to request: an absolute
|
|
298
|
+
* `http://` or `https://` URL is returned as written, anything else is a
|
|
299
|
+
* path and is appended to the launch URL.
|
|
300
|
+
*
|
|
301
|
+
* @param {string} launchUrl
|
|
302
|
+
* @param {string} url
|
|
303
|
+
* @returns {string}
|
|
304
|
+
*/
|
|
305
|
+
function resolveUrl(launchUrl, url) {
|
|
306
|
+
const value = url === undefined || url === null ? '' : String(url);
|
|
307
|
+
if (/^https?:\/\//i.test(value)) return value;
|
|
308
|
+
return (launchUrl || '') + value;
|
|
309
|
+
}
|
|
310
|
+
|
|
296
311
|
/**
|
|
297
312
|
* Navigate to a URL with friendly error messages for common failures
|
|
298
313
|
* (server down, DNS, empty response). All other errors rethrow with
|
|
@@ -613,6 +628,7 @@ module.exports = {
|
|
|
613
628
|
isAnyModalVisible,
|
|
614
629
|
buildSelector,
|
|
615
630
|
gotoUrl,
|
|
631
|
+
resolveUrl,
|
|
616
632
|
fillField,
|
|
617
633
|
getLocatorText,
|
|
618
634
|
pad,
|