webship-js 2.0.10 → 2.0.12
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/.circleci/config.yml +10 -1
- package/.drone.yml +7 -7
- package/.github/ISSUE_TEMPLATE/issue.md +37 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +24 -0
- package/.github/workflows/github-actions.yml +101 -4
- package/.gitlab-ci.yml +9 -4
- package/.harness/webship-js-pipeline.yml +5 -6
- package/.semaphore/semaphore.yml +6 -9
- package/.teamcity/settings.kts +7 -6
- package/.travis.yml +6 -1
- package/AGENTS.md +1 -1
- package/CLAUDE.md +69 -24
- package/Jenkinsfile +5 -3
- package/README.md +13 -1
- package/azure-pipelines.yml +17 -8
- package/bamboo-specs/bamboo.yml +9 -15
- package/bin/generate-reports.js +51 -7
- package/bin/init-webship.js +127 -23
- package/bitbucket-pipelines.yml +5 -1
- package/buildspec.yml +6 -7
- package/cloudbuild.yaml +28 -12
- package/codefresh.yml +6 -6
- package/cucumber.js +46 -11
- package/docs/00-quick-start.md +38 -7
- package/docs/02-bbr-smart-waits.md +21 -10
- package/docs/03-selector-registry.md +24 -0
- package/docs/04-step-reference.md +53 -27
- package/docs/05-web-first-assertions.md +6 -0
- package/docs/07-auth-state.md +1 -1
- package/docs/08-clock-mocking.md +21 -2
- package/docs/09-api-testing.md +40 -2
- package/docs/10-accessibility.md +2 -0
- package/docs/11-debugging.md +24 -1
- package/docs/12-ai-agent-guide.md +8 -1
- package/docs/13-faq.md +75 -1
- package/docs/14-recipes-cookbook.md +2 -0
- package/docs/15-tag-conventions.md +5 -1
- package/docs/16-ci-cd.md +161 -124
- package/docs/README.md +5 -5
- package/docs/advanced-screenshots.md +3 -1
- package/docs/api-step-definitions.md +16 -2
- package/docs/global-settings.md +70 -18
- package/docs/install-webship-js/ddev-webship-js.md +3 -0
- package/docs/install-webship-js.md +16 -2
- package/docs/step-definitions/then-i-should-see.md +14 -1
- package/docs/step-definitions/then-item-checkbox.md +2 -0
- package/docs/step-definitions/when-i-fill.md +4 -0
- package/docs/step-definitions/when-i-select-option-select-list.md +2 -0
- package/examples/a11y-legacy-widget.html +34 -0
- package/examples/auth-state.html +42 -0
- package/examples/choice-controls.html +29 -0
- package/examples/clock.html +33 -0
- package/examples/datetime-fields.html +52 -0
- package/examples/downloads/webship-archive.zip +0 -0
- package/examples/downloads/webship-notes.txt +4 -0
- package/examples/downloads/webship-report.csv +4 -0
- package/examples/downloads.html +16 -0
- package/examples/element-selector-hint.html +16 -0
- package/examples/element-viewport.html +42 -0
- package/examples/field-states.html +27 -0
- package/examples/field-types.html +98 -0
- package/examples/fill-in-empty.html +26 -0
- package/examples/fill-unreachable.html +27 -0
- package/examples/iframe-inner.html +20 -0
- package/examples/iframe-nested.html +16 -0
- package/examples/iframe-newsletter.html +29 -0
- package/examples/iframe-order.html +26 -0
- package/examples/iframe-outer.html +15 -0
- package/examples/iframe.html +21 -0
- package/examples/javascript-errors.html +35 -0
- package/examples/links.html +1 -0
- package/examples/metatags.html +18 -0
- package/examples/mouse-and-pointer.html +83 -0
- package/examples/multi-value-fields.html +54 -0
- package/examples/native-dialogs.html +8 -0
- package/examples/network.html +83 -0
- package/examples/registered-names.html +15 -0
- package/examples/scroll-horizontal.html +50 -0
- package/examples/select-by-label.html +33 -0
- package/examples/selectors-position.html +29 -0
- package/examples/settle-budget.html +21 -0
- package/examples/sorted-tables.html +38 -0
- package/examples/storage.html +57 -0
- package/examples/text-matching.html +48 -0
- package/examples/text-order.html +25 -0
- package/examples/touch.html +22 -0
- package/examples/viewport-size.html +24 -0
- package/examples/viewport-top.html +36 -0
- package/examples/wait-conditions.html +69 -0
- package/examples/web-first.html +99 -0
- package/package.json +24 -2
- package/playwright.config.ts +3 -1
- package/tests/assets/catalog.xml +19 -0
- package/tests/assets/feed-namespaced.xml +13 -0
- package/tests/assets/sitemap.xml +14 -0
- package/tests/assets/webship-site-expected.yaml +21 -0
- package/tests/assets/webship-site.schema.json +46 -0
- package/tests/assets/webship-site.yaml +21 -0
- package/tests/check-api-launch-url.js +106 -0
- package/tests/check-assets.js +103 -0
- package/tests/check-attachments.js +105 -0
- package/tests/check-breakpoints.js +117 -0
- package/tests/check-clock.js +143 -0
- package/tests/check-config.js +90 -0
- package/tests/check-context.js +215 -0
- package/tests/check-dry-run.js +104 -0
- package/tests/check-examples.js +241 -0
- package/tests/check-exit-report.js +141 -0
- package/tests/check-javascript-errors.js +105 -0
- package/tests/check-must-fail.js +85 -0
- package/tests/check-paths.js +82 -0
- package/tests/check-report.js +119 -0
- package/tests/check-scaffold.js +246 -0
- package/tests/check-screenshots.js +117 -0
- package/tests/check-settle-verbs.js +137 -0
- package/tests/check-time-limit.js +104 -0
- package/tests/check-video.js +156 -0
- package/tests/check-world.js +167 -0
- package/tests/features/a11y.feature +18 -0
- package/tests/features/api-base-url.feature +25 -0
- package/tests/features/api-form-data.feature +18 -0
- package/tests/features/api-json-paths.feature +24 -0
- package/tests/features/api-response-contains.feature +21 -0
- package/tests/features/api-response-header-presence.feature +17 -0
- package/tests/features/api-response-header.feature +17 -0
- package/tests/features/api-saved-placeholders.feature +37 -0
- package/tests/features/api-step-definitions-individual.feature +1 -1
- package/tests/features/api-tables.feature +40 -0
- package/tests/features/auth-state.feature +32 -0
- package/tests/features/breakpoints.feature +25 -0
- package/tests/features/choice-controls.feature +35 -0
- package/tests/features/clock.feature +27 -0
- package/tests/features/complete-api-testing-examples.feature +1 -1
- package/tests/features/cookie.feature +9 -0
- package/tests/features/datetime-field-lookup.feature +29 -0
- package/tests/features/dialog.feature +27 -0
- package/tests/features/element-selector-hint.feature +14 -0
- package/tests/features/element.feature +47 -0
- package/tests/features/field-state.feature +21 -0
- package/tests/features/field.feature +104 -0
- package/tests/features/file-download.feature +64 -0
- package/tests/features/fill-in-empty-value.feature +41 -0
- package/tests/features/fill-reachable.feature +13 -0
- package/tests/features/iframe-nested.feature +43 -0
- package/tests/features/iframe.feature +83 -0
- package/tests/features/javascript-tolerated-errors.feature +20 -0
- package/tests/features/javascript.feature +47 -0
- package/tests/features/link.feature +11 -0
- package/tests/features/metatag.feature +38 -0
- package/tests/features/modal-open.feature +13 -0
- package/tests/features/mouse-and-pointer.feature +86 -0
- package/tests/features/multi-value-field-lookup.feature +30 -0
- package/tests/features/network-mocking.feature +80 -0
- package/tests/features/registered-names.feature +14 -0
- package/tests/features/responsive.feature +27 -0
- package/tests/features/scroll-horizontal.feature +55 -0
- package/tests/features/select-by-label.feature +31 -0
- package/tests/features/selector-files.feature +14 -0
- package/tests/features/selectors-position.feature +41 -0
- package/tests/features/selectors-table-header.feature +35 -0
- package/tests/features/settle-budget.feature +11 -0
- package/tests/features/sorted-tables.feature +19 -0
- package/tests/features/storage-quoted-values.feature +26 -0
- package/tests/features/storage.feature +98 -0
- package/tests/features/test--then--i-see-locator.feature +13 -0
- package/tests/features/test--then--i-should--see-text-in-element.feature +9 -1
- package/tests/features/test--then--the-field-should-contain-value.feature +8 -0
- package/tests/features/text-matching.feature +38 -0
- package/tests/features/text-order.feature +14 -0
- package/tests/features/touch.feature +21 -0
- package/tests/features/video.feature +1 -0
- package/tests/features/viewport-top.feature +13 -0
- package/tests/features/wait.feature +89 -0
- package/tests/features/web-first.feature +100 -0
- package/tests/features/xml-namespaces.feature +34 -0
- package/tests/features/xml.feature +96 -0
- package/tests/features/yaml.feature +75 -0
- package/tests/must-fail/api-json-paths.feature +21 -0
- package/tests/must-fail/api-response-contains.feature +23 -0
- package/tests/must-fail/api-response-header-presence.feature +17 -0
- package/tests/must-fail/api-response-header.feature +21 -0
- package/tests/must-fail/api-without-base-url.feature +11 -0
- package/tests/must-fail/assertions.feature +96 -0
- package/tests/must-fail/breakpoints.feature +15 -0
- package/tests/must-fail/choice-controls.feature +25 -0
- package/tests/must-fail/clock.feature +11 -0
- package/tests/must-fail/datetime-field.feature +10 -0
- package/tests/must-fail/element-selector-hint.feature +17 -0
- package/tests/must-fail/field-state.feature +20 -0
- package/tests/must-fail/fill-field.feature +30 -0
- package/tests/must-fail/iframe-text.feature +17 -0
- package/tests/must-fail/modal-open.feature +26 -0
- package/tests/must-fail/multi-value-field.feature +18 -0
- package/tests/must-fail/registered-names.feature +25 -0
- package/tests/must-fail/select-list.feature +15 -0
- package/tests/must-fail/selector-files.feature +10 -0
- package/tests/must-fail/sorted-tables.feature +20 -0
- package/tests/must-fail/storage-before-a-page.feature +13 -0
- package/tests/must-fail/text-matching.feature +16 -0
- package/tests/must-fail/text-order.feature +15 -0
- package/tests/must-fail/viewport-top.feature +20 -0
- package/tests/must-fail/waits.feature +51 -0
- package/tests/must-fail/yaml-numbers.feature +48 -0
- package/tests/reports/README.md +2 -2
- package/tests/step-definitions/a11y.steps.js +36 -23
- package/tests/step-definitions/action.steps.js +22 -8
- package/tests/step-definitions/api.steps.js +349 -67
- package/tests/step-definitions/assertion.steps.js +327 -63
- package/tests/step-definitions/auth.steps.js +15 -18
- package/tests/step-definitions/clock.steps.js +22 -0
- package/tests/step-definitions/debug.steps.js +8 -0
- package/tests/step-definitions/dialog.steps.js +9 -2
- package/tests/step-definitions/element.steps.js +135 -18
- package/tests/step-definitions/field.steps.js +523 -80
- package/tests/step-definitions/file-download.steps.js +74 -10
- package/tests/step-definitions/form.steps.js +76 -29
- package/tests/step-definitions/iframe.steps.js +89 -29
- package/tests/step-definitions/input.steps.js +26 -7
- package/tests/step-definitions/javascript.steps.js +64 -22
- package/tests/step-definitions/modal.steps.js +79 -13
- package/tests/step-definitions/navigation.steps.js +14 -0
- package/tests/step-definitions/network.steps.js +15 -5
- package/tests/step-definitions/response.steps.js +6 -4
- package/tests/step-definitions/responsive.steps.js +26 -33
- package/tests/step-definitions/screenshot.steps.js +26 -9
- package/tests/step-definitions/scroll.steps.js +20 -0
- package/tests/step-definitions/selectors.steps.js +104 -51
- package/tests/step-definitions/storage.steps.js +41 -16
- package/tests/step-definitions/table.steps.js +81 -6
- package/tests/step-definitions/video.steps.js +52 -55
- package/tests/step-definitions/wait.steps.js +209 -54
- package/tests/step-definitions/web-first.steps.js +69 -35
- package/tests/step-definitions/webship.js +807 -128
- package/tests/step-definitions/xml.steps.js +124 -28
- package/tests/step-definitions/yaml.steps.js +90 -46
- package/.scratch/commit.txt +0 -14
- package/.scratch/docs.py +0 -42
- package/.scratch/echo-server.js +0 -8
- package/.scratch/echo.js +0 -35
- package/.scratch/echo.pid +0 -1
- package/.scratch/features/cookies.feature +0 -22
- package/.scratch/http.pid +0 -1
- package/.scratch/https.pid +0 -1
- package/.scratch/issue.md +0 -49
- package/.scratch/one.js +0 -2
- package/.scratch/one.json +0 -280
- package/.scratch/patch.py +0 -163
- package/.scratch/pr.md +0 -30
|
@@ -20,6 +20,7 @@ const { When } = require('@cucumber/cucumber');
|
|
|
20
20
|
* Example #2: When I scroll down 800
|
|
21
21
|
* Example #3: And we scroll down 500
|
|
22
22
|
* Example #4: When scrolling down 1200
|
|
23
|
+
* Example #5: Then we scroll down 200
|
|
23
24
|
*
|
|
24
25
|
*/
|
|
25
26
|
When(/^(I scroll|we scroll|scrolling)? down(?: (\d+))?$/, async function (pronounCase, numValue) {
|
|
@@ -33,6 +34,7 @@ When(/^(I scroll|we scroll|scrolling)? down(?: (\d+))?$/, async function (pronou
|
|
|
33
34
|
* Example #2: When I scroll up 1000
|
|
34
35
|
* Example #3: And we scroll up 300
|
|
35
36
|
* Example #4: When scrolling up 750
|
|
37
|
+
* Example #5: Then we scroll up 200
|
|
36
38
|
*
|
|
37
39
|
*/
|
|
38
40
|
When(/^(I scroll|we scroll|scrolling)? up(?: (\d+))?$/, async function (pronounCase, numValue) {
|
|
@@ -45,6 +47,8 @@ When(/^(I scroll|we scroll|scrolling)? up(?: (\d+))?$/, async function (pronounC
|
|
|
45
47
|
* Example #1: When I scroll to top
|
|
46
48
|
* Example #2: And we scroll to the top
|
|
47
49
|
* Example #3: When scrolling to the top of the page
|
|
50
|
+
* Example #4: Then we scroll to the top of the page
|
|
51
|
+
* Example #5: When I scroll to the top
|
|
48
52
|
*
|
|
49
53
|
*/
|
|
50
54
|
When(/^(I scroll|we scroll|scrolling)? to( the)* top( of the page)*$/, async function (pronounCase, theCase, pageCase) {
|
|
@@ -57,6 +61,8 @@ When(/^(I scroll|we scroll|scrolling)? to( the)* top( of the page)*$/, async fun
|
|
|
57
61
|
* Example #1: When I scroll to the bottom
|
|
58
62
|
* Example #2: And we scroll to bottom
|
|
59
63
|
* Example #3: When scrolling to the bottom of the page
|
|
64
|
+
* Example #4: Then I scroll to the bottom of the page
|
|
65
|
+
* Example #5: When we scroll to the bottom
|
|
60
66
|
*
|
|
61
67
|
*/
|
|
62
68
|
When(/^(I scroll|we scroll|scrolling)? to( the)* bottom( of the page)*$/, async function (pronounCase, theCase, pageCase) {
|
|
@@ -82,6 +88,8 @@ When(/^(I scroll|we scroll|scrolling)? to( the)* bottom( of the page)*$/, async
|
|
|
82
88
|
* Example #1: When I scroll to top of "#off-canvas"
|
|
83
89
|
* Example #2: And we scroll to top of "#sidebar"
|
|
84
90
|
* Example #3: When scrolling to top of "#main-container"
|
|
91
|
+
* Example #4: When I scroll to top of ".chat-history"
|
|
92
|
+
* Example #5: And we scroll to top of "#results"
|
|
85
93
|
*
|
|
86
94
|
*/
|
|
87
95
|
When(/^(I scroll|we scroll|scrolling)? to top of "([^"]*)"$/, async function (pronounCase, selector) {
|
|
@@ -97,6 +105,8 @@ When(/^(I scroll|we scroll|scrolling)? to top of "([^"]*)"$/, async function (pr
|
|
|
97
105
|
* Example #1: When I scroll to bottom of "#off-canvas"
|
|
98
106
|
* Example #2: And we scroll to bottom of "#sidebar"
|
|
99
107
|
* Example #3: When scrolling to bottom of "#main-container"
|
|
108
|
+
* Example #4: When I scroll to bottom of ".chat-history"
|
|
109
|
+
* Example #5: And we scroll to bottom of "#results"
|
|
100
110
|
*
|
|
101
111
|
*/
|
|
102
112
|
When(/^(I scroll|we scroll|scrolling)? to bottom of "([^"]*)"$/, async function (pronounCase, selector) {
|
|
@@ -117,6 +127,7 @@ When(/^(I scroll|we scroll|scrolling)? to bottom of "([^"]*)"$/, async function
|
|
|
117
127
|
* Example #2: When I scroll right 1000
|
|
118
128
|
* Example #3: And we scroll right 300
|
|
119
129
|
* Example #4: When scrolling right 750
|
|
130
|
+
* Example #5: Then we scroll right 200
|
|
120
131
|
*
|
|
121
132
|
*/
|
|
122
133
|
When(/^(I scroll|we scroll|scrolling)? right(?: (\d+))?$/, async function (pronounCase, numValue) {
|
|
@@ -130,6 +141,7 @@ When(/^(I scroll|we scroll|scrolling)? right(?: (\d+))?$/, async function (prono
|
|
|
130
141
|
* Example #2: When I scroll left 800
|
|
131
142
|
* Example #3: And we scroll left 500
|
|
132
143
|
* Example #4: When scrolling left 1200
|
|
144
|
+
* Example #5: Then we scroll left 200
|
|
133
145
|
*
|
|
134
146
|
*/
|
|
135
147
|
When(/^(I scroll|we scroll|scrolling)? left(?: (\d+))?$/, async function (pronounCase, numValue) {
|
|
@@ -142,6 +154,8 @@ When(/^(I scroll|we scroll|scrolling)? left(?: (\d+))?$/, async function (pronou
|
|
|
142
154
|
* Example #1: When I scroll to start
|
|
143
155
|
* Example #2: And we scroll to the start
|
|
144
156
|
* Example #3: When scrolling to the start of the page
|
|
157
|
+
* Example #4: Then I scroll to the start of the page
|
|
158
|
+
* Example #5: When we scroll to start
|
|
145
159
|
*
|
|
146
160
|
*/
|
|
147
161
|
When(/^(I scroll|we scroll|scrolling)? to( the)* start( of the page)*$/, async function (pronounCase, theCase, pageCase) {
|
|
@@ -154,6 +168,8 @@ When(/^(I scroll|we scroll|scrolling)? to( the)* start( of the page)*$/, async f
|
|
|
154
168
|
* Example #1: When I scroll to the end
|
|
155
169
|
* Example #2: And we scroll to end
|
|
156
170
|
* Example #3: When scrolling to the end of the page
|
|
171
|
+
* Example #4: Then I scroll to the end of the page
|
|
172
|
+
* Example #5: When we scroll to the end
|
|
157
173
|
*
|
|
158
174
|
*/
|
|
159
175
|
When(/^(I scroll|we scroll|scrolling)? to( the)* end( of the page)*$/, async function (pronounCase, theCase, pageCase) {
|
|
@@ -170,6 +186,8 @@ When(/^(I scroll|we scroll|scrolling)? to( the)* end( of the page)*$/, async fun
|
|
|
170
186
|
* Example #1: When I scroll to start of "#off-canvas"
|
|
171
187
|
* Example #2: And we scroll to start of "#sidebar"
|
|
172
188
|
* Example #3: When scrolling to start of "#main-container"
|
|
189
|
+
* Example #4: When I scroll to start of ".carousel"
|
|
190
|
+
* Example #5: And we scroll to start of "#gallery"
|
|
173
191
|
*
|
|
174
192
|
*/
|
|
175
193
|
When(/^(I scroll|we scroll|scrolling)? to start of "([^"]*)"$/, async function (pronounCase, selector) {
|
|
@@ -182,6 +200,8 @@ When(/^(I scroll|we scroll|scrolling)? to start of "([^"]*)"$/, async function (
|
|
|
182
200
|
* Example #1: When I scroll to end of "#off-canvas"
|
|
183
201
|
* Example #2: And we scroll to end of "#sidebar"
|
|
184
202
|
* Example #3: When scrolling to end of "#main-container"
|
|
203
|
+
* Example #4: When I scroll to end of ".carousel"
|
|
204
|
+
* Example #5: And we scroll to end of "#gallery"
|
|
185
205
|
*
|
|
186
206
|
*/
|
|
187
207
|
When(/^(I scroll|we scroll|scrolling)? to end of "([^"]*)"$/, async function (pronounCase, selector) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { friendly } = require('./webship');
|
|
3
|
+
const { friendly, failure, breakpoint } = require('./webship');
|
|
4
4
|
|
|
5
5
|
// ---------------------------------------------------------------------------
|
|
6
6
|
// Webship-JS — Selectors step definitions
|
|
@@ -20,8 +20,9 @@ const { friendly } = require('./webship');
|
|
|
20
20
|
// 3. built-in defaults
|
|
21
21
|
//
|
|
22
22
|
// Supported env vars / worldParameters keys:
|
|
23
|
-
//
|
|
24
|
-
//
|
|
23
|
+
// WEBSHIP_SELECTORS_BREAKPOINTS (JSON object) named breakpoints, merged over
|
|
24
|
+
// worldParameters.selectors.breakpoints (one
|
|
25
|
+
// registry for every step and tag: webship.js)
|
|
25
26
|
//
|
|
26
27
|
// Unified config block (worldParameters.selectors):
|
|
27
28
|
// selectors: {
|
|
@@ -34,7 +35,6 @@ const { friendly } = require('./webship');
|
|
|
34
35
|
// xpath: {
|
|
35
36
|
// 'page title': '//h1[contains(@class,"page-title")]',
|
|
36
37
|
// },
|
|
37
|
-
// offset: 60, // scroll offset (px)
|
|
38
38
|
// breakpoints: {
|
|
39
39
|
// xs: { width: 375, height: 667 }, // phone portrait
|
|
40
40
|
// sm: { width: 576, height: 800 }, // large phone / phablet
|
|
@@ -69,36 +69,45 @@ function parseFile(filePath) {
|
|
|
69
69
|
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
// Folder of the selector files when worldParameters.selectors.filesPath is not
|
|
73
|
+
// set: the one the scaffold writes.
|
|
74
|
+
const DEFAULT_FILES_PATH = './tests/selectors/';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Find a selector file of worldParameters.selectors.files or of the "add
|
|
78
|
+
* selectors from ... file" step. A name is looked for in the folder of
|
|
79
|
+
* filesPath first (auth.json), then as written from the project root
|
|
80
|
+
* (tests/selectors/auth.json): the first that exists wins.
|
|
81
|
+
*
|
|
82
|
+
* @param {{filesPath: string}} cfg
|
|
83
|
+
* @param {string} file
|
|
84
|
+
* @param {import('playwright').Page} [page] - Named in the failure, when there is one.
|
|
85
|
+
* @returns {string} The path of the file.
|
|
86
|
+
*/
|
|
87
|
+
function resolveSelectorFile(cfg, file, page) {
|
|
88
|
+
const name = String(file || '').trim();
|
|
89
|
+
const tried = path.isAbsolute(name)
|
|
90
|
+
? [name]
|
|
91
|
+
: [...new Set([path.join(cfg.filesPath || '', name), path.normalize(name)])];
|
|
92
|
+
const found = tried.find((candidate) => fs.existsSync(candidate));
|
|
93
|
+
if (found) return found;
|
|
94
|
+
const message = `The selector file "${name}" was not found.`;
|
|
95
|
+
const details = {
|
|
96
|
+
found: `nothing at ${tried.join(', ')}`,
|
|
97
|
+
hint: `name the file by its name in the selectors folder (${cfg.filesPath || DEFAULT_FILES_PATH}) or by its path from the project root.`,
|
|
98
|
+
};
|
|
99
|
+
if (page) throw failure(page, message, details);
|
|
100
|
+
throw friendly(`Selectors: ${message}\n Found: ${details.found}\n Hint: ${details.hint}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
72
103
|
// ---------------------------------------------------------------------------
|
|
73
104
|
// Config
|
|
74
105
|
// ---------------------------------------------------------------------------
|
|
75
|
-
function pick(envVal, paramVal, fallback) {
|
|
76
|
-
if (envVal !== undefined && envVal !== '') return envVal;
|
|
77
|
-
if (paramVal !== undefined && paramVal !== '' && paramVal !== null) return paramVal;
|
|
78
|
-
return fallback;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
106
|
function resolveConfig(parameters) {
|
|
82
107
|
const s = (parameters && parameters.selectors) || {};
|
|
83
108
|
|
|
84
|
-
let breakpoints = s.breakpoints || {
|
|
85
|
-
xs: { width: 375, height: 667 },
|
|
86
|
-
sm: { width: 576, height: 800 },
|
|
87
|
-
md: { width: 768, height: 1024 },
|
|
88
|
-
lg: { width: 992, height: 768 },
|
|
89
|
-
xl: { width: 1200, height: 900, default: true },
|
|
90
|
-
xxl: { width: 1400, height: 900 },
|
|
91
|
-
xxxl: { width: 1920, height: 1080 },
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
if (process.env.WEBSHIP_SELECTORS_BREAKPOINTS) {
|
|
95
|
-
try { breakpoints = JSON.parse(process.env.WEBSHIP_SELECTORS_BREAKPOINTS); } catch { /* ignore */ }
|
|
96
|
-
}
|
|
97
|
-
|
|
98
109
|
return {
|
|
99
|
-
|
|
100
|
-
breakpoints,
|
|
101
|
-
filesPath: s.filesPath || '',
|
|
110
|
+
filesPath: s.filesPath || DEFAULT_FILES_PATH,
|
|
102
111
|
files: Array.isArray(s.files) ? s.files : [],
|
|
103
112
|
css: Object.assign({}, s.css || {}),
|
|
104
113
|
xpath: Object.assign({}, s.xpath || {}),
|
|
@@ -116,7 +125,7 @@ Before(function () {
|
|
|
116
125
|
|
|
117
126
|
// Auto-load selector files specified in worldParameters.
|
|
118
127
|
for (const file of cfg.files) {
|
|
119
|
-
const filePath =
|
|
128
|
+
const filePath = resolveSelectorFile(cfg, file);
|
|
120
129
|
try {
|
|
121
130
|
const parsed = parseFile(filePath);
|
|
122
131
|
if (parsed && parsed.css) Object.assign(this.__selectorsCss, parsed.css);
|
|
@@ -185,8 +194,22 @@ function resolveSelector(world, name) {
|
|
|
185
194
|
);
|
|
186
195
|
}
|
|
187
196
|
|
|
197
|
+
// The rows of a "define css/xpath selectors" table. A first row that names
|
|
198
|
+
// the columns, as the docs write it (| name | css selector |), is a header,
|
|
199
|
+
// not a selector.
|
|
200
|
+
function selectorRows(dataTable) {
|
|
201
|
+
const rows = dataTable.raw();
|
|
202
|
+
if (rows.length === 0) return rows;
|
|
203
|
+
const [name, selector] = rows[0].map((cell) => String(cell || '').trim().toLowerCase());
|
|
204
|
+
const header = (name === 'name' || name === 'selector name')
|
|
205
|
+
&& /^(?:(?:css|xpath)(?: selector| expression)?|selector)$/.test(selector || '');
|
|
206
|
+
return header ? rows.slice(1) : rows;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Names are separated by a comma or by the whole word "and" ("a, b and c",
|
|
210
|
+
// "a, b, and c"), never by the letters "and" inside a name ("brand logo").
|
|
188
211
|
function parseNames(text) {
|
|
189
|
-
return text.split(/\s*(
|
|
212
|
+
return text.split(/\s*,\s*(?:and\s+)?|\s+and\s+/).map(s => s.trim()).filter(Boolean);
|
|
190
213
|
}
|
|
191
214
|
|
|
192
215
|
// ---------------------------------------------------------------------------
|
|
@@ -242,7 +265,9 @@ async function assertPosition(page, position, c1Name, c1Sel, c2Name, c2Sel) {
|
|
|
242
265
|
case 'over': {
|
|
243
266
|
const intersects = !(g1.left >= g2.left + g2.width || g1.left + g1.width <= g2.left ||
|
|
244
267
|
g1.top >= g2.top + g2.height || g1.top + g1.height <= g2.top);
|
|
245
|
-
|
|
268
|
+
// The subject (g1) is over the other component when they overlap and
|
|
269
|
+
// the subject is not stacked under it.
|
|
270
|
+
pass = intersects && g1.zIndex >= g2.zIndex;
|
|
246
271
|
break;
|
|
247
272
|
}
|
|
248
273
|
}
|
|
@@ -330,8 +355,9 @@ When(/^(I |we )*add "([^"]*)" selector for "([^"]*)" xpath selector$/, function
|
|
|
330
355
|
});
|
|
331
356
|
|
|
332
357
|
/**
|
|
333
|
-
* Load CSS and XPath selectors from a JSON file.
|
|
334
|
-
*
|
|
358
|
+
* Load CSS and XPath selectors from a JSON file. The file is looked for in the
|
|
359
|
+
* folder of worldParameters.selectors.filesPath (default ./tests/selectors/)
|
|
360
|
+
* first, then as written from the project root.
|
|
335
361
|
*
|
|
336
362
|
* Example #1: When I add selectors from "selectors.json" file
|
|
337
363
|
* Example #2: When I add selectors from "admin-selectors.json" file
|
|
@@ -360,8 +386,7 @@ When(/^(I |we )*add "([^"]*)" selector for "([^"]*)" xpath selector$/, function
|
|
|
360
386
|
When(/^(I |we )*add selectors from "([^"]*)" file$/, function (pronounCase, fileName) {
|
|
361
387
|
const cfg = this.__selectorsConfig;
|
|
362
388
|
if (!fileName) throw friendly('Selectors: file name must not be empty.');
|
|
363
|
-
|
|
364
|
-
const filePath = path.join(cfg.filesPath, fileName);
|
|
389
|
+
const filePath = resolveSelectorFile(cfg, fileName, this.page);
|
|
365
390
|
let parsed;
|
|
366
391
|
try { parsed = parseFile(filePath); } catch (e) {
|
|
367
392
|
throw friendly(`Selectors: failed to load selector file "${filePath}": ${e.message}`);
|
|
@@ -419,6 +444,9 @@ Then(/^(I |we )*print xpath selectors$/, function (_pronounCase) {
|
|
|
419
444
|
/**
|
|
420
445
|
* Define named CSS selectors in bulk for the current scenario.
|
|
421
446
|
*
|
|
447
|
+
* The table may start with a header row that names its columns
|
|
448
|
+
* (`| name | css selector |`): that row registers nothing.
|
|
449
|
+
*
|
|
422
450
|
* Example #1:
|
|
423
451
|
* Given I define css selectors:
|
|
424
452
|
* | header | #header |
|
|
@@ -494,9 +522,15 @@ Then(/^(I |we )*print xpath selectors$/, function (_pronounCase) {
|
|
|
494
522
|
* | nav | nav[role=navigation] |
|
|
495
523
|
* | main | main |
|
|
496
524
|
* | footer | footer |
|
|
525
|
+
*
|
|
526
|
+
* Example #14: A header row names the columns and registers nothing:
|
|
527
|
+
* Given I define css selectors:
|
|
528
|
+
* | name | css selector |
|
|
529
|
+
* | heading | h1 |
|
|
530
|
+
* | list | ul |
|
|
497
531
|
*/
|
|
498
532
|
Given(/^(I |we )*define css selectors:$/, function (pronounCase, dataTable) {
|
|
499
|
-
for (const [name, selector] of dataTable
|
|
533
|
+
for (const [name, selector] of selectorRows(dataTable)) {
|
|
500
534
|
if (name && selector) this.__selectorsCss[name.trim()] = selector.trim();
|
|
501
535
|
}
|
|
502
536
|
});
|
|
@@ -504,6 +538,9 @@ Given(/^(I |we )*define css selectors:$/, function (pronounCase, dataTable) {
|
|
|
504
538
|
/**
|
|
505
539
|
* Define named XPath selectors in bulk for the current scenario.
|
|
506
540
|
*
|
|
541
|
+
* The table may start with a header row that names its columns
|
|
542
|
+
* (`| name | xpath selector |`): that row registers nothing.
|
|
543
|
+
*
|
|
507
544
|
* Example #1:
|
|
508
545
|
* Given I define xpath selectors:
|
|
509
546
|
* | first heading | //h1[1] |
|
|
@@ -536,24 +573,36 @@ Given(/^(I |we )*define css selectors:$/, function (pronounCase, dataTable) {
|
|
|
536
573
|
* Example #10:
|
|
537
574
|
* Given I define xpath selectors:
|
|
538
575
|
* | user menu | //*[@id='user-menu'] |
|
|
576
|
+
* Example #11:
|
|
577
|
+
* Given I define xpath selectors:
|
|
578
|
+
* | name | xpath selector |
|
|
579
|
+
* | first heading | //h1[1] |
|
|
539
580
|
*/
|
|
540
581
|
Given(/^(I |we )*define xpath selectors:$/, function (pronounCase, dataTable) {
|
|
541
|
-
for (const [name, selector] of dataTable
|
|
582
|
+
for (const [name, selector] of selectorRows(dataTable)) {
|
|
542
583
|
if (name && selector) this.__selectorsXpath[name.trim()] = selector.trim();
|
|
543
584
|
}
|
|
544
585
|
});
|
|
545
586
|
|
|
546
587
|
/**
|
|
547
|
-
* Resize viewport to a named breakpoint.
|
|
548
|
-
*
|
|
588
|
+
* Resize viewport to a named breakpoint. The same registry as "I set the
|
|
589
|
+
* viewport to the "…" breakpoint" and the @mobile / @desktop tags: the
|
|
590
|
+
* built-in names below, replaced or extended by
|
|
591
|
+
* worldParameters.selectors.breakpoints, WEBSHIP_SELECTORS_BREAKPOINTS and
|
|
592
|
+
* "the following responsive breakpoints:". An unknown name fails with the
|
|
593
|
+
* list of the known ones.
|
|
549
594
|
*
|
|
550
|
-
* xs
|
|
551
|
-
* sm
|
|
552
|
-
* md
|
|
553
|
-
* lg
|
|
554
|
-
* xl
|
|
555
|
-
* xxl
|
|
556
|
-
* xxxl
|
|
595
|
+
* xs 375 x 667 phone portrait
|
|
596
|
+
* sm 576 x 800 large phone / phablet
|
|
597
|
+
* md 768 x 1024 tablet portrait
|
|
598
|
+
* lg 992 x 768 small laptop / tablet landscape
|
|
599
|
+
* xl 1200 x 900 desktop (marked default: true in the scaffold)
|
|
600
|
+
* xxl 1400 x 900 wide desktop / HD
|
|
601
|
+
* xxxl 1920 x 1080 Full HD / large monitor
|
|
602
|
+
* mobile 375 x 667 the size of the @mobile tag
|
|
603
|
+
* tablet 768 x 1024
|
|
604
|
+
* desktop 1200 x 900 the size of the @desktop tag; without a desktop of
|
|
605
|
+
* the project's own, the breakpoint marked default: true
|
|
557
606
|
*
|
|
558
607
|
* Example #1: Given I am viewing the site on a xl screen
|
|
559
608
|
* Example #2: Given I am viewing the site on a xs screen
|
|
@@ -569,11 +618,7 @@ Given(/^(I |we )*define xpath selectors:$/, function (pronounCase, dataTable) {
|
|
|
569
618
|
* Example #12: Given viewing the site on a lg screen
|
|
570
619
|
*/
|
|
571
620
|
Given(/^(I am |we are )?viewing the site on a (?:"([^"]+)"|([a-zA-Z0-9 _,]+)) (?:screen|device)$/, async function (pronounCase, quoted, bare) {
|
|
572
|
-
|
|
573
|
-
const cfg = this.__selectorsConfig;
|
|
574
|
-
if (!cfg.breakpoints[name]) throw friendly(`Selectors: breakpoint "${name}" not defined in worldParameters.selectors.breakpoints.`);
|
|
575
|
-
const { width, height } = cfg.breakpoints[name];
|
|
576
|
-
await this.page.setViewportSize({ width, height });
|
|
621
|
+
await this.page.setViewportSize(breakpoint(this, quoted || bare));
|
|
577
622
|
});
|
|
578
623
|
|
|
579
624
|
// ===========================================================================
|
|
@@ -700,7 +745,9 @@ Then(/^(I |we )*see ([a-zA-Z0-9 ,\-]+) outside of ([a-zA-Z0-9 ,\-]+)$/, async fu
|
|
|
700
745
|
});
|
|
701
746
|
|
|
702
747
|
/**
|
|
703
|
-
* Assert a subject locator overlaps another via intersection and z-index
|
|
748
|
+
* Assert a subject locator overlaps another via intersection and z-index:
|
|
749
|
+
* the two bounding boxes intersect and the z-index of the subject is not
|
|
750
|
+
* lower than the z-index of the other.
|
|
704
751
|
*
|
|
705
752
|
* Example #1: Then I see modal over content
|
|
706
753
|
* Example #2: Then I see dropdown over nav
|
|
@@ -962,6 +1009,12 @@ When(/^(I |we )*select "([^"]*)" text in "([^"]*)" field$/, async function (pron
|
|
|
962
1009
|
/**
|
|
963
1010
|
* Click one or more named components — uses locator.click() which auto-scrolls.
|
|
964
1011
|
*
|
|
1012
|
+
* The names are read without quotes, so the step matches any unquoted words
|
|
1013
|
+
* after "click" (registered names such as "sign in button" need that). A
|
|
1014
|
+
* custom step that starts the same way, such as "I click next button in
|
|
1015
|
+
* tour", is ambiguous with this one: give a custom step a quoted argument
|
|
1016
|
+
* and words of its own ("I click "Next" in the tour") instead.
|
|
1017
|
+
*
|
|
965
1018
|
* Example #1: When I click nav
|
|
966
1019
|
* Example #2: When I click search button
|
|
967
1020
|
* Example #3: When click cta
|
|
@@ -11,6 +11,27 @@
|
|
|
11
11
|
// page's origin and require a navigated page first.
|
|
12
12
|
|
|
13
13
|
const { Given } = require('@cucumber/cucumber');
|
|
14
|
+
const { failure } = require('./webship');
|
|
15
|
+
|
|
16
|
+
// Local storage and session storage belong to the site of the page. On a page
|
|
17
|
+
// that has none (about:blank before the first "I am on", a data: page) the
|
|
18
|
+
// browser refuses them with a SecurityError: say so, and what to do.
|
|
19
|
+
async function inPageStorage(world, what, script, arg) {
|
|
20
|
+
try {
|
|
21
|
+
return await world.page.evaluate(script, arg);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (!/SecurityError|Access is denied|operation is insecure/i.test(String((e && e.message) || ''))) throw e;
|
|
24
|
+
throw failure(world.page, `Could not ${what}: the page has no site yet.`, {
|
|
25
|
+
hint: 'local storage and session storage belong to the site of the page: open a page of the site first, for example: Given I am on "/"',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// A quoted key or value may hold a double quote written \" (a JSON value, a
|
|
31
|
+
// quoted cookie value), read as {string} reads it in the cookie assertions.
|
|
32
|
+
function unquote(text) {
|
|
33
|
+
return String(text === undefined || text === null ? '' : text).replace(/\\"/g, '"').replace(/\\'/g, "'");
|
|
34
|
+
}
|
|
14
35
|
|
|
15
36
|
// ---------------------------------------------------------------------------
|
|
16
37
|
// Cookies — context-level
|
|
@@ -28,7 +49,9 @@ const { Given } = require('@cucumber/cucumber');
|
|
|
28
49
|
* And I am on "/dashboard"
|
|
29
50
|
*
|
|
30
51
|
*/
|
|
31
|
-
Given(/^(?:the cookie|cookie) "([^"]*)" is set to "([^"]*)"$/, async function (name, value) {
|
|
52
|
+
Given(/^(?:the cookie|cookie) "((?:[^"\\]|\\.)*)" is set to "((?:[^"\\]|\\.)*)"$/, async function (name, value) {
|
|
53
|
+
name = unquote(name);
|
|
54
|
+
value = unquote(value);
|
|
32
55
|
const url = this.page.url() && this.page.url() !== 'about:blank' ? this.page.url() : (this.launchUrl || 'http://localhost');
|
|
33
56
|
const u = new URL(url);
|
|
34
57
|
await this.context.addCookies([{
|
|
@@ -53,7 +76,8 @@ Given(/^(?:the cookie|cookie) "([^"]*)" is set to "([^"]*)"$/, async function (n
|
|
|
53
76
|
* When I follow "Pricing"
|
|
54
77
|
*
|
|
55
78
|
*/
|
|
56
|
-
Given(/^the cookie "([^"]*)" is removed$/, async function (name) {
|
|
79
|
+
Given(/^the cookie "((?:[^"\\]|\\.)*)" is removed$/, async function (name) {
|
|
80
|
+
name = unquote(name);
|
|
57
81
|
const cookies = await this.context.cookies();
|
|
58
82
|
const remaining = cookies.filter((c) => c.name !== name);
|
|
59
83
|
await this.context.clearCookies();
|
|
@@ -70,8 +94,8 @@ Given(/^the cookie "([^"]*)" is removed$/, async function (name) {
|
|
|
70
94
|
* Example #3: Given all cookies are cleared
|
|
71
95
|
* And I am an anonymous user
|
|
72
96
|
* Example #4: Given all cookies are cleared
|
|
73
|
-
* And
|
|
74
|
-
* And
|
|
97
|
+
* And local storage is cleared
|
|
98
|
+
* And session storage is cleared
|
|
75
99
|
* Example #5: Given all cookies are cleared
|
|
76
100
|
* When I follow "Login"
|
|
77
101
|
*
|
|
@@ -88,7 +112,8 @@ Given(/^all cookies are cleared$/, async function () {
|
|
|
88
112
|
* Set a local storage entry on the current page.
|
|
89
113
|
*
|
|
90
114
|
* The page must be loaded first (local storage is per-origin). Stored value
|
|
91
|
-
* is always a string — JSON-encode complex objects manually
|
|
115
|
+
* is always a string — JSON-encode complex objects manually, writing each
|
|
116
|
+
* double quote as \" inside the step's quotes.
|
|
92
117
|
*
|
|
93
118
|
* Example #1: Given the local storage "user" is set to "Alice"
|
|
94
119
|
* Example #2: Given I am on the homepage
|
|
@@ -102,8 +127,8 @@ Given(/^all cookies are cleared$/, async function () {
|
|
|
102
127
|
* Example #5: Given the local storage "feature_flag" is set to "enabled"
|
|
103
128
|
*
|
|
104
129
|
*/
|
|
105
|
-
Given(/^(?:the )?local storage "([^"]*)" is set to "([^"]*)"$/, async function (key, value) {
|
|
106
|
-
await this
|
|
130
|
+
Given(/^(?:the )?local storage "((?:[^"\\]|\\.)*)" is set to "((?:[^"\\]|\\.)*)"$/, async function (key, value) {
|
|
131
|
+
await inPageStorage(this, `set the local storage "${unquote(key)}"`, ({ k, v }) => localStorage.setItem(k, v), { k: unquote(key), v: unquote(value) });
|
|
107
132
|
});
|
|
108
133
|
|
|
109
134
|
/**
|
|
@@ -112,7 +137,7 @@ Given(/^(?:the )?local storage "([^"]*)" is set to "([^"]*)"$/, async function (
|
|
|
112
137
|
* Example #1: Given the local storage "user" is removed
|
|
113
138
|
* Example #2: Given local storage "theme" is removed
|
|
114
139
|
* When I reload the page
|
|
115
|
-
* Then "
|
|
140
|
+
* Then the element "body" with the attribute "class" and the value containing "theme-dark" should not exist
|
|
116
141
|
* Example #3: Given the local storage "onboarding_complete" is removed
|
|
117
142
|
* Example #4: Given the local storage "cart" is removed
|
|
118
143
|
* And I am on "/cart"
|
|
@@ -120,8 +145,8 @@ Given(/^(?:the )?local storage "([^"]*)" is set to "([^"]*)"$/, async function (
|
|
|
120
145
|
* Example #5: Given the local storage "auth_token" is removed
|
|
121
146
|
*
|
|
122
147
|
*/
|
|
123
|
-
Given(/^(?:the )?local storage "([^"]*)" is removed$/, async function (key) {
|
|
124
|
-
await this
|
|
148
|
+
Given(/^(?:the )?local storage "((?:[^"\\]|\\.)*)" is removed$/, async function (key) {
|
|
149
|
+
await inPageStorage(this, `remove the local storage "${unquote(key)}"`, (k) => localStorage.removeItem(k), unquote(key));
|
|
125
150
|
});
|
|
126
151
|
|
|
127
152
|
/**
|
|
@@ -140,7 +165,7 @@ Given(/^(?:the )?local storage "([^"]*)" is removed$/, async function (key) {
|
|
|
140
165
|
*
|
|
141
166
|
*/
|
|
142
167
|
Given(/^local storage is cleared$/, async function () {
|
|
143
|
-
await this
|
|
168
|
+
await inPageStorage(this, 'clear the local storage', () => localStorage.clear());
|
|
144
169
|
});
|
|
145
170
|
|
|
146
171
|
/**
|
|
@@ -155,8 +180,8 @@ Given(/^local storage is cleared$/, async function () {
|
|
|
155
180
|
* And I am on "/cart"
|
|
156
181
|
*
|
|
157
182
|
*/
|
|
158
|
-
Given(/^(?:the )?session storage "([^"]*)" is set to "([^"]*)"$/, async function (key, value) {
|
|
159
|
-
await this
|
|
183
|
+
Given(/^(?:the )?session storage "((?:[^"\\]|\\.)*)" is set to "((?:[^"\\]|\\.)*)"$/, async function (key, value) {
|
|
184
|
+
await inPageStorage(this, `set the session storage "${unquote(key)}"`, ({ k, v }) => sessionStorage.setItem(k, v), { k: unquote(key), v: unquote(value) });
|
|
160
185
|
});
|
|
161
186
|
|
|
162
187
|
/**
|
|
@@ -171,8 +196,8 @@ Given(/^(?:the )?session storage "([^"]*)" is set to "([^"]*)"$/, async function
|
|
|
171
196
|
* Example #5: Given the session storage "from checkout" is removed
|
|
172
197
|
*
|
|
173
198
|
*/
|
|
174
|
-
Given(/^(?:the )?session storage "([^"]*)" is removed$/, async function (key) {
|
|
175
|
-
await this
|
|
199
|
+
Given(/^(?:the )?session storage "((?:[^"\\]|\\.)*)" is removed$/, async function (key) {
|
|
200
|
+
await inPageStorage(this, `remove the session storage "${unquote(key)}"`, (k) => sessionStorage.removeItem(k), unquote(key));
|
|
176
201
|
});
|
|
177
202
|
|
|
178
203
|
/**
|
|
@@ -190,5 +215,5 @@ Given(/^(?:the )?session storage "([^"]*)" is removed$/, async function (key) {
|
|
|
190
215
|
*
|
|
191
216
|
*/
|
|
192
217
|
Given(/^session storage is cleared$/, async function () {
|
|
193
|
-
await this
|
|
218
|
+
await inPageStorage(this, 'clear the session storage', () => sessionStorage.clear());
|
|
194
219
|
});
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const { Then } = require('@cucumber/cucumber');
|
|
6
6
|
const assert = require('assert');
|
|
7
|
+
const { failure } = require('./webship');
|
|
7
8
|
|
|
8
9
|
async function getTable(page, sel) {
|
|
9
10
|
return await page.locator(sel).first().evaluate((tbl) => {
|
|
@@ -100,10 +101,66 @@ Then('the table {string} should not be empty', async function (sel) {
|
|
|
100
101
|
assert.ok(t.rowCount > 0, `Table "${sel}" should not be empty.`);
|
|
101
102
|
});
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The number a table cell reads as, or null: digits with an optional sign,
|
|
106
|
+
* thousands separators and decimals, after a currency sign or code and before
|
|
107
|
+
* a percent sign, a unit or a word (`$1,299.00`, `USD 40`, `12 %`, `3.5 kg`).
|
|
108
|
+
* A word before the number ("Item 2") makes the cell text.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} cell
|
|
111
|
+
* @returns {number|null}
|
|
112
|
+
*/
|
|
113
|
+
function cellNumber(cell) {
|
|
114
|
+
const m = String(cell).replace(/[\s\u00a0]+/g, ' ').trim()
|
|
115
|
+
.match(/^(?:[A-Z]{3} ?)?[^\p{L}\d+-]*([+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?)[^\d]*$/u);
|
|
116
|
+
return m ? parseFloat(m[1].replace(/,/g, '')) : null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The time a table cell reads as, in ms, or null when it is not a date.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} cell
|
|
123
|
+
* @returns {number|null}
|
|
124
|
+
*/
|
|
125
|
+
function cellDate(cell) {
|
|
126
|
+
const text = String(cell).trim();
|
|
127
|
+
if (!/\d/.test(text)) return null;
|
|
128
|
+
const ms = Date.parse(text);
|
|
129
|
+
return Number.isNaN(ms) ? null : ms;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* How the cells of a column compare, the way a reader orders them: as numbers
|
|
134
|
+
* when every cell reads as a number, else as dates when every cell reads as a
|
|
135
|
+
* date, else as text with letter case ignored and the numbers inside the text
|
|
136
|
+
* in number order ("Item 2" before "Item 10").
|
|
137
|
+
*
|
|
138
|
+
* @param {string[]} values
|
|
139
|
+
* @returns {{kind: string, compare: function(string, string): number}}
|
|
140
|
+
*/
|
|
141
|
+
function columnOrder(values) {
|
|
142
|
+
if (values.length && values.every((v) => cellNumber(v) !== null)) {
|
|
143
|
+
return { kind: 'numbers', compare: (a, b) => cellNumber(a) - cellNumber(b) };
|
|
144
|
+
}
|
|
145
|
+
if (values.length && values.every((v) => cellDate(v) !== null)) {
|
|
146
|
+
return { kind: 'dates', compare: (a, b) => cellDate(a) - cellDate(b) };
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
kind: 'text, letter case ignored',
|
|
150
|
+
compare: (a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
103
154
|
/**
|
|
104
155
|
* Assert a table is sorted ascending or descending by a header.
|
|
105
156
|
*
|
|
106
|
-
*
|
|
157
|
+
* The cells of the column are compared the way a reader orders them: as
|
|
158
|
+
* numbers when every cell reads as a number (`$9.99`, `1,299`, `12 %`), else
|
|
159
|
+
* as dates when every cell reads as a date, else as text with letter case
|
|
160
|
+
* ignored and the numbers inside it in number order ("Item 2" before
|
|
161
|
+
* "Item 10"). Each row is compared with the next one, so equal cells may come
|
|
162
|
+
* in any order. "descending" or "desc" checks the reverse order; any other
|
|
163
|
+
* word checks the ascending order.
|
|
107
164
|
*
|
|
108
165
|
* Example #1: Then the table "#users" should be sorted by "Name" in "ascending" order
|
|
109
166
|
* Example #2: Then the table "table.orders" should be sorted by "Total" in "descending" order
|
|
@@ -113,13 +170,31 @@ Then('the table {string} should not be empty', async function (sel) {
|
|
|
113
170
|
*
|
|
114
171
|
*/
|
|
115
172
|
Then('the table {string} should be sorted by {string} in {string} order', async function (sel, column, direction) {
|
|
173
|
+
if (!(await this.page.locator(sel).count())) {
|
|
174
|
+
throw failure(this.page, `No table matches "${sel}".`, {
|
|
175
|
+
hint: 'the first argument is a CSS selector of the table, such as "#users" or "table.orders".',
|
|
176
|
+
});
|
|
177
|
+
}
|
|
116
178
|
const t = await getTable(this.page, sel);
|
|
117
179
|
const idx = t.head.findIndex((h) => h === column);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
180
|
+
if (idx === -1) {
|
|
181
|
+
throw failure(this.page, `The table "${sel}" has no "${column}" column.`, {
|
|
182
|
+
found: t.head.length ? t.head.map((h) => `"${h}"`).join(', ') : 'no header cells (<th>)',
|
|
183
|
+
hint: 'the column is named by the exact text of its header cell.',
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const descending = ['descending', 'desc'].includes(direction.toLowerCase());
|
|
187
|
+
const values = t.body.map((r) => (r[idx] === undefined ? '' : r[idx]));
|
|
188
|
+
const order = columnOrder(values);
|
|
189
|
+
for (let i = 1; i < values.length; i++) {
|
|
190
|
+
const diff = order.compare(values[i - 1], values[i]);
|
|
191
|
+
if (descending ? diff < 0 : diff > 0) {
|
|
192
|
+
throw failure(this.page, `The table "${sel}" is not sorted by "${column}" in ${descending ? 'descending' : 'ascending'} order: row ${i} ("${values[i - 1]}") comes before row ${i + 1} ("${values[i]}").`, {
|
|
193
|
+
found: values.map((v) => `"${v}"`).join(', '),
|
|
194
|
+
hint: `the cells were compared as ${order.kind}.`,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
123
198
|
});
|
|
124
199
|
|
|
125
200
|
/**
|