webship-js 2.0.2 → 2.0.3

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.
Files changed (162) hide show
  1. package/AGENTS.md +109 -0
  2. package/CLAUDE.md +317 -0
  3. package/bin/generate-reports.js +181 -9
  4. package/cucumber.js +10 -4
  5. package/docs/00-quick-start.md +102 -0
  6. package/docs/01-getting-started.md +102 -0
  7. package/docs/02-bbr-smart-waits.md +77 -0
  8. package/docs/03-selector-registry.md +95 -0
  9. package/docs/04-step-reference.md +728 -0
  10. package/docs/05-web-first-assertions.md +91 -0
  11. package/docs/06-network-and-dialogs.md +83 -0
  12. package/docs/07-auth-state.md +75 -0
  13. package/docs/08-clock-mocking.md +59 -0
  14. package/docs/09-api-testing.md +114 -0
  15. package/docs/10-accessibility.md +167 -0
  16. package/docs/11-debugging.md +129 -0
  17. package/docs/12-ai-agent-guide.md +338 -0
  18. package/docs/13-faq.md +236 -0
  19. package/docs/14-recipes-cookbook.md +255 -0
  20. package/docs/15-tag-conventions.md +76 -0
  21. package/docs/advanced-screenshots/then-i-save-fullscreen-screenshot.md +36 -0
  22. package/docs/advanced-screenshots/then-i-save-width-x-height-screenshot.md +34 -0
  23. package/docs/advanced-screenshots/when-i-save-screenshot-name-filename.md +45 -0
  24. package/docs/advanced-screenshots.md +52 -0
  25. package/docs/advanced-selectors.md +18 -0
  26. package/docs/api-step-definitions.md +45 -0
  27. package/docs/diffy-step-definitions/then-compare-diffy-env1-env2.md +30 -0
  28. package/docs/diffy-step-definitions/then-create-diffy-comparison-name-value.md +21 -0
  29. package/docs/diffy-step-definitions/then-create-diffy-comparison.md +25 -0
  30. package/docs/diffy-step-definitions/then-create-diffy-screenshot-environment-name.md +35 -0
  31. package/docs/diffy-step-definitions/then-i-take-screenshot.md +21 -0
  32. package/docs/diffy-step-definitions/then-i-take-screenshots-all-breakpoints.md +27 -0
  33. package/docs/diffy-step-definitions/then-send-screenshots-diffy-name-value.md +27 -0
  34. package/docs/diffy-step-definitions/then-upload-folder-url-diffy-snapshotname.md +28 -0
  35. package/docs/diffy-step-definitions/then-wait-diffy-comparison-complete.md +21 -0
  36. package/docs/diffy-step-definitions/when-i-resize-window-number.md +29 -0
  37. package/docs/diffy-step-definitions.md +21 -0
  38. package/docs/global-settings.md +244 -0
  39. package/docs/index.md +104 -0
  40. package/docs/install-webship-js/ddev-webship-js.md +52 -0
  41. package/docs/install-webship-js.md +35 -0
  42. package/docs/overview.md +41 -0
  43. package/docs/step-definitions/given-i-am-anonymous-user.md +67 -0
  44. package/docs/step-definitions/given-i-am-homepage.md +68 -0
  45. package/docs/step-definitions/given-i-am-specific-page.md +72 -0
  46. package/docs/step-definitions/then-i-should-be.md +56 -0
  47. package/docs/step-definitions/then-i-should-see.md +62 -0
  48. package/docs/step-definitions/then-item-checkbox.md +54 -0
  49. package/docs/step-definitions/then-item-link-should-contain-url.md +84 -0
  50. package/docs/step-definitions/then-item-should-contain.md +67 -0
  51. package/docs/step-definitions/then-radio-button-value-should-be-selected.md +68 -0
  52. package/docs/step-definitions/then-response-status-code-should-be-number.md +69 -0
  53. package/docs/step-definitions/then-value-radio-button-selected.md +62 -0
  54. package/docs/step-definitions/when-i-attach-file-file-name-element.md +71 -0
  55. package/docs/step-definitions/when-i-check-checkbox.md +60 -0
  56. package/docs/step-definitions/when-i-click-link.md +51 -0
  57. package/docs/step-definitions/when-i-click-operation-text-row.md +71 -0
  58. package/docs/step-definitions/when-i-fill.md +59 -0
  59. package/docs/step-definitions/when-i-go-homepage.md +60 -0
  60. package/docs/step-definitions/when-i-go-specific-page.md +67 -0
  61. package/docs/step-definitions/when-i-move-backward-one-page.md +49 -0
  62. package/docs/step-definitions/when-i-move-forward-one-page.md +58 -0
  63. package/docs/step-definitions/when-i-press-button.md +58 -0
  64. package/docs/step-definitions/when-i-reload-page.md +51 -0
  65. package/docs/step-definitions/when-i-scroll.md +73 -0
  66. package/docs/step-definitions/when-i-select-option-select-list.md +53 -0
  67. package/docs/step-definitions/when-i-select-radio-button-value.md +62 -0
  68. package/docs/step-definitions/when-i-uncheck-checkbox.md +57 -0
  69. package/docs/step-definitions/when-i-wait-ajax-finish.md +64 -0
  70. package/docs/step-definitions/when-i-wait.md +62 -0
  71. package/docs/step-definitions.md +50 -0
  72. package/examples/a11y.html +31 -0
  73. package/examples/cookies.html +10 -0
  74. package/examples/date.html +12 -0
  75. package/examples/element.html +11 -0
  76. package/examples/field.html +24 -0
  77. package/examples/keyboard.html +11 -0
  78. package/examples/links.html +9 -0
  79. package/examples/modal.html +14 -0
  80. package/examples/path.html +3 -0
  81. package/examples/table.html +13 -0
  82. package/package.json +10 -3
  83. package/playwright.config.ts +3 -2
  84. package/tests/features/a11y.feature +39 -0
  85. package/tests/features/cookie.feature +13 -0
  86. package/tests/features/date.feature +11 -0
  87. package/tests/features/element.feature +16 -0
  88. package/tests/features/field.feature +25 -0
  89. package/tests/features/keyboard.feature +10 -0
  90. package/tests/features/link.feature +11 -0
  91. package/tests/features/modal-advanced-example.feature +1 -1
  92. package/tests/features/modal.feature +11 -0
  93. package/tests/features/path.feature +16 -0
  94. package/tests/features/response.feature +8 -0
  95. package/tests/features/responsive.feature +10 -0
  96. package/tests/features/table.feature +18 -0
  97. package/tests/features/wait.feature +7 -0
  98. package/tests/features/yaml.feature +102 -0
  99. package/tests/reports/README.md +55 -0
  100. package/tests/selectors/_canonical-keys.json +113 -0
  101. package/tests/selectors/back-end-selectors.json +7 -1
  102. package/tests/selectors/cms-contentful-admin.json +35 -0
  103. package/tests/selectors/cms-craft-admin.json +39 -0
  104. package/tests/selectors/cms-drupal-cms-gin.json +56 -0
  105. package/tests/selectors/cms-drupal-core-claro.json +50 -0
  106. package/tests/selectors/cms-generic-admin.json +34 -0
  107. package/tests/selectors/cms-ghost-admin.json +34 -0
  108. package/tests/selectors/cms-joomla-admin.json +41 -0
  109. package/tests/selectors/cms-magento2-admin.json +32 -0
  110. package/tests/selectors/cms-prestashop-admin.json +38 -0
  111. package/tests/selectors/cms-shopify-admin.json +25 -0
  112. package/tests/selectors/cms-strapi-admin.json +42 -0
  113. package/tests/selectors/cms-typo3-admin.json +41 -0
  114. package/tests/selectors/cms-woocommerce-front.json +50 -0
  115. package/tests/selectors/cms-wordpress-admin.json +31 -0
  116. package/tests/selectors/framework-ant-design.json +80 -0
  117. package/tests/selectors/framework-bootstrap.json +86 -0
  118. package/tests/selectors/framework-bulma.json +74 -0
  119. package/tests/selectors/framework-chakra.json +58 -0
  120. package/tests/selectors/framework-foundation.json +51 -0
  121. package/tests/selectors/framework-material-ui.json +72 -0
  122. package/tests/selectors/framework-shadcn.json +51 -0
  123. package/tests/selectors/framework-tailwind.json +40 -0
  124. package/tests/selectors/framework-vuetify.json +61 -0
  125. package/tests/step-definitions/a11y.steps.js +798 -0
  126. package/tests/step-definitions/action.steps.js +167 -0
  127. package/tests/step-definitions/{webship-api.js → api.steps.js} +24 -7
  128. package/tests/step-definitions/assertion.steps.js +352 -0
  129. package/tests/step-definitions/auth.steps.js +124 -0
  130. package/tests/step-definitions/clock.steps.js +128 -0
  131. package/tests/step-definitions/cookie.steps.js +216 -0
  132. package/tests/step-definitions/debug.steps.js +31 -0
  133. package/tests/step-definitions/dialog.steps.js +193 -0
  134. package/tests/step-definitions/element.steps.js +314 -0
  135. package/tests/step-definitions/field.steps.js +653 -0
  136. package/tests/step-definitions/file-download.steps.js +193 -0
  137. package/tests/step-definitions/form.steps.js +333 -0
  138. package/tests/step-definitions/iframe.steps.js +178 -0
  139. package/tests/step-definitions/input.steps.js +163 -0
  140. package/tests/step-definitions/javascript.steps.js +71 -0
  141. package/tests/step-definitions/keyboard.steps.js +78 -0
  142. package/tests/step-definitions/link.steps.js +142 -0
  143. package/tests/step-definitions/metatag.steps.js +91 -0
  144. package/tests/step-definitions/modal.steps.js +219 -0
  145. package/tests/step-definitions/navigation.steps.js +218 -0
  146. package/tests/step-definitions/network.steps.js +238 -0
  147. package/tests/step-definitions/path.steps.js +141 -0
  148. package/tests/step-definitions/response.steps.js +98 -0
  149. package/tests/step-definitions/responsive.steps.js +123 -0
  150. package/tests/step-definitions/rest.steps.js +111 -0
  151. package/tests/step-definitions/{webship-screenshot.js → screenshot.steps.js} +1 -5
  152. package/tests/step-definitions/scroll.steps.js +189 -0
  153. package/tests/step-definitions/{webship-selectors.js → selectors.steps.js} +46 -11
  154. package/tests/step-definitions/storage.steps.js +194 -0
  155. package/tests/step-definitions/table.steps.js +181 -0
  156. package/tests/step-definitions/wait.steps.js +400 -0
  157. package/tests/step-definitions/web-first.steps.js +296 -0
  158. package/tests/step-definitions/webship.js +552 -1507
  159. package/tests/step-definitions/xml.steps.js +409 -0
  160. package/tests/step-definitions/yaml.steps.js +911 -0
  161. package/tests/step-definitions-diffy/webship-diffy.js +0 -13
  162. package/tsconfig.json +19 -3
@@ -0,0 +1,409 @@
1
+ 'use strict';
2
+
3
+ // Parse and assert against an XML response loaded from a file or doc string.
4
+
5
+ const { Given, Then, When } = require('@cucumber/cucumber');
6
+ const assert = require('assert');
7
+ const fs = require('fs');
8
+ const path = require('path');
9
+
10
+ let DOMParser;
11
+ try { ({ DOMParser } = require('@xmldom/xmldom')); }
12
+ catch (e) {
13
+ try { ({ DOMParser } = require('xmldom')); }
14
+ catch (e2) { /* will throw on use */ }
15
+ }
16
+
17
+ function ensureXml(world) {
18
+ if (!DOMParser) throw new Error('XML steps require an XML DOM parser. Install @xmldom/xmldom.');
19
+ if (!world._xml) throw new Error('No XML response has been set.');
20
+ return world._xml;
21
+ }
22
+
23
+ function evalXPath(doc, xpath) {
24
+ // Minimal XPath: support /a/b and /a/b[@attr] and /a/b/text()
25
+ // For richer XPath, users can install xpath module.
26
+ let xpathLib;
27
+ try { xpathLib = require('xpath'); } catch (e) { xpathLib = null; }
28
+ if (xpathLib) return xpathLib.select(xpath, doc);
29
+ // Fallback simple traversal
30
+ const parts = xpath.split('/').filter(Boolean);
31
+ let nodes = [doc.documentElement].filter((n) => n && n.nodeName === parts[0]);
32
+ for (const part of parts.slice(1)) {
33
+ const next = [];
34
+ for (const n of nodes) {
35
+ const children = Array.from(n.childNodes || []).filter((c) => c.nodeType === 1 && c.nodeName === part);
36
+ next.push(...children);
37
+ }
38
+ nodes = next;
39
+ }
40
+ return nodes;
41
+ }
42
+
43
+ /**
44
+ * Load XML response content from a file under `tests/assets/`.
45
+ *
46
+ * Example #1: Given the response content from the file "feed.xml"
47
+ * Example #2: Given the response content from the file "rss.xml"
48
+ * Example #3: And the response content from the file "sitemap.xml"
49
+ * Example #4: Given the response content from the file "soap-response.xml"
50
+ * Example #5: Given the response content from the file "/tmp/sample.xml"
51
+ *
52
+ */
53
+ Given('the response content from the file {string}', async function (filename) {
54
+ const p = path.isAbsolute(filename) ? filename : path.join(this.assetsFolder || './tests/assets', filename);
55
+ const content = fs.readFileSync(p, 'utf-8');
56
+ this._rawResponse = content;
57
+ this._xml = new DOMParser().parseFromString(content, 'text/xml');
58
+ });
59
+
60
+ /**
61
+ * Set XML response content inline from a Gherkin doc string.
62
+ *
63
+ * Example #1: Given the response content is the following:
64
+ * """
65
+ * <root><item id="1">Hello</item></root>
66
+ * """
67
+ * Example #2: Given the response content is the following:
68
+ * """
69
+ * <feed><entry><title>Sample feed</title></entry></feed>
70
+ * """
71
+ * Example #3: And the response content is the following:
72
+ * """
73
+ * <orders><order id="1"><status>shipped</status></order></orders>
74
+ * """
75
+ * Example #4: Given the response content is the following:
76
+ * """
77
+ * <products><product><name>Laptop</name></product></products>
78
+ * """
79
+ * Example #5: Given the response content is the following:
80
+ * """
81
+ * <error code="404">Not found</error>
82
+ * """
83
+ *
84
+ */
85
+ Given('the response content is the following:', async function (docString) {
86
+ this._rawResponse = docString;
87
+ this._xml = new DOMParser().parseFromString(docString, 'text/xml');
88
+ });
89
+
90
+ /**
91
+ * Assert the loaded response parses as XML.
92
+ *
93
+ * Example #1: Then the response should be in XML format
94
+ * Example #2: Given the response content from the file "rss.xml"
95
+ * Then the response should be in XML format
96
+ * Example #3: And the response should be in XML format
97
+ * Example #4: Given the response content is the following:
98
+ * """
99
+ * <root/>
100
+ * """
101
+ * Then the response should be in XML format
102
+ * Example #5: Then the response should be in XML format
103
+ * And the XML element "/root" should exist
104
+ *
105
+ */
106
+ Then('the response should be in XML format', async function () {
107
+ assert.ok(this._rawResponse, 'No response content set.');
108
+ const doc = new DOMParser().parseFromString(this._rawResponse, 'text/xml');
109
+ assert.ok(doc && doc.documentElement, 'Response is not valid XML.');
110
+ });
111
+
112
+ /**
113
+ * Assert the loaded response is NOT XML.
114
+ *
115
+ * Example #1: Then the response should not be in XML format
116
+ * Example #2: Given the response content is the following:
117
+ * """
118
+ * {"hello": "world"}
119
+ * """
120
+ * Then the response should not be in XML format
121
+ * Example #3: And the response should not be in XML format
122
+ * Example #4: Given the response content is the following:
123
+ * """
124
+ * plain text
125
+ * """
126
+ * Then the response should not be in XML format
127
+ * Example #5: Then the response should not be in XML format
128
+ * And I print last XML response
129
+ *
130
+ */
131
+ Then('the response should not be in XML format', async function () {
132
+ if (!this._rawResponse) return;
133
+ try {
134
+ const doc = new DOMParser().parseFromString(this._rawResponse, 'text/xml');
135
+ assert.ok(!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length > 0, 'Response is XML.');
136
+ } catch (e) { /* good */ }
137
+ });
138
+
139
+ /**
140
+ * Assert at least one XML element matches an XPath.
141
+ *
142
+ * Example #1: Then the XML element "/root/item" should exist
143
+ * Example #2: Then the XML element "//book/title" should exist
144
+ * Example #3: And the XML element "/order/customer" should exist
145
+ * Example #4: Then the XML element "//status" should exist
146
+ * Example #5: Then the XML element "/feed/entry[2]" should exist
147
+ *
148
+ */
149
+ Then('the XML element {string} should exist', async function (xpath) {
150
+ const doc = ensureXml(this);
151
+ const nodes = evalXPath(doc, xpath);
152
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
153
+ });
154
+
155
+ /**
156
+ * Assert NO XML element matches an XPath.
157
+ *
158
+ * Example #1: Then the XML element "/root/missing" should not exist
159
+ * Example #2: Then the XML element "//error" should not exist
160
+ * Example #3: And the XML element "/feed/legacy" should not exist
161
+ * Example #4: Then the XML element "//deprecated" should not exist
162
+ * Example #5: Then the XML element "/order/refund" should not exist
163
+ *
164
+ */
165
+ Then('the XML element {string} should not exist', async function (xpath) {
166
+ const doc = ensureXml(this);
167
+ const nodes = evalXPath(doc, xpath);
168
+ assert.ok(!nodes || nodes.length === 0, `XML element "${xpath}" should not exist.`);
169
+ });
170
+
171
+ /**
172
+ * Assert an XML element's text content equals an expected value (trimmed).
173
+ *
174
+ * Example #1: Then the XML element "/root/item" should be equal to "Hello"
175
+ * Example #2: Then the XML element "//status" should be equal to "shipped"
176
+ * Example #3: And the XML element "/order/customer/name" should be equal to "Alice"
177
+ * Example #4: Then the XML element "/book/title" should be equal to "Sample title"
178
+ * Example #5: Then the XML element "//count" should be equal to "5"
179
+ *
180
+ */
181
+ Then('the XML element {string} should be equal to {string}', async function (xpath, text) {
182
+ const doc = ensureXml(this);
183
+ const nodes = evalXPath(doc, xpath);
184
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
185
+ const value = (nodes[0].textContent || '').trim();
186
+ assert.strictEqual(value, text);
187
+ });
188
+
189
+ /**
190
+ * Assert an XML element's text content is NOT equal to an expected value.
191
+ *
192
+ * Example #1: Then the XML element "/root/item" should not be equal to "Bye"
193
+ * Example #2: Then the XML element "//status" should not be equal to "cancelled"
194
+ * Example #3: And the XML element "/order/customer/name" should not be equal to "Anonymous"
195
+ * Example #4: Then the XML element "/book/title" should not be equal to "Untitled"
196
+ * Example #5: Then the XML element "//count" should not be equal to "0"
197
+ *
198
+ */
199
+ Then('the XML element {string} should not be equal to {string}', async function (xpath, text) {
200
+ const doc = ensureXml(this);
201
+ const nodes = evalXPath(doc, xpath);
202
+ if (!nodes || nodes.length === 0) return;
203
+ assert.notStrictEqual((nodes[0].textContent || '').trim(), text);
204
+ });
205
+
206
+ /**
207
+ * Assert an XML element's text content contains a substring.
208
+ *
209
+ * Example #1: Then the XML element "/root/item" should contain "Hello"
210
+ * Example #2: Then the XML element "//status" should contain "ship"
211
+ * Example #3: And the XML element "/order/customer/name" should contain "Alice"
212
+ * Example #4: Then the XML element "/book/title" should contain "Webship"
213
+ * Example #5: Then the XML element "/feed/entry/summary" should contain "release"
214
+ *
215
+ */
216
+ Then('the XML element {string} should contain {string}', async function (xpath, text) {
217
+ const doc = ensureXml(this);
218
+ const nodes = evalXPath(doc, xpath);
219
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
220
+ assert.ok((nodes[0].textContent || '').indexOf(text) !== -1);
221
+ });
222
+
223
+ /**
224
+ * Assert an XML element's text content does NOT contain a substring.
225
+ *
226
+ * Example #1: Then the XML element "/root/item" should not contain "Error"
227
+ * Example #2: Then the XML element "//status" should not contain "fail"
228
+ * Example #3: And the XML element "/order/customer/name" should not contain "Anonymous"
229
+ * Example #4: Then the XML element "/book/title" should not contain "Untitled"
230
+ * Example #5: Then the XML element "/feed/entry/summary" should not contain "TODO"
231
+ *
232
+ */
233
+ Then('the XML element {string} should not contain {string}', async function (xpath, text) {
234
+ const doc = ensureXml(this);
235
+ const nodes = evalXPath(doc, xpath);
236
+ if (!nodes || nodes.length === 0) return;
237
+ assert.ok((nodes[0].textContent || '').indexOf(text) === -1);
238
+ });
239
+
240
+ /**
241
+ * Assert an XML element has the named attribute.
242
+ *
243
+ * Example #1: Then the XML attribute "id" on element "/root/item" should exist
244
+ * Example #2: Then the XML attribute "lang" on element "/feed" should exist
245
+ * Example #3: And the XML attribute "type" on element "//entry" should exist
246
+ * Example #4: Then the XML attribute "version" on element "/rss" should exist
247
+ * Example #5: Then the XML attribute "code" on element "/error" should exist
248
+ *
249
+ */
250
+ Then('the XML attribute {string} on element {string} should exist', async function (attr, xpath) {
251
+ const doc = ensureXml(this);
252
+ const nodes = evalXPath(doc, xpath);
253
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
254
+ assert.ok(nodes[0].getAttribute && nodes[0].hasAttribute(attr), `Attribute "${attr}" not found on "${xpath}".`);
255
+ });
256
+
257
+ /**
258
+ * Assert an XML element does NOT have the named attribute.
259
+ *
260
+ * Example #1: Then the XML attribute "deprecated" on element "/root/item" should not exist
261
+ * Example #2: Then the XML attribute "internal" on element "/feed" should not exist
262
+ * Example #3: And the XML attribute "secret" on element "//entry" should not exist
263
+ * Example #4: Then the XML attribute "private" on element "/rss" should not exist
264
+ * Example #5: Then the XML attribute "debug" on element "/error" should not exist
265
+ *
266
+ */
267
+ Then('the XML attribute {string} on element {string} should not exist', async function (attr, xpath) {
268
+ const doc = ensureXml(this);
269
+ const nodes = evalXPath(doc, xpath);
270
+ if (!nodes || nodes.length === 0) return;
271
+ assert.ok(!nodes[0].hasAttribute(attr), `Attribute "${attr}" should not exist on "${xpath}".`);
272
+ });
273
+
274
+ /**
275
+ * Assert an XML attribute equals an expected value.
276
+ *
277
+ * Example #1: Then the XML attribute "id" on element "/root/item" should be equal to "1"
278
+ * Example #2: Then the XML attribute "lang" on element "/feed" should be equal to "en"
279
+ * Example #3: And the XML attribute "type" on element "//entry" should be equal to "html"
280
+ * Example #4: Then the XML attribute "version" on element "/rss" should be equal to "2.0"
281
+ * Example #5: Then the XML attribute "code" on element "/error" should be equal to "404"
282
+ *
283
+ */
284
+ Then('the XML attribute {string} on element {string} should be equal to {string}', async function (attr, xpath, text) {
285
+ const doc = ensureXml(this);
286
+ const nodes = evalXPath(doc, xpath);
287
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
288
+ assert.strictEqual(nodes[0].getAttribute(attr), text);
289
+ });
290
+
291
+ /**
292
+ * Assert an XML attribute is NOT equal to an expected value.
293
+ *
294
+ * Example #1: Then the XML attribute "id" on element "/root/item" should not be equal to "0"
295
+ * Example #2: Then the XML attribute "lang" on element "/feed" should not be equal to "xx"
296
+ * Example #3: And the XML attribute "type" on element "//entry" should not be equal to "binary"
297
+ * Example #4: Then the XML attribute "version" on element "/rss" should not be equal to "0.9"
298
+ * Example #5: Then the XML attribute "code" on element "/error" should not be equal to "200"
299
+ *
300
+ */
301
+ Then('the XML attribute {string} on element {string} should not be equal to {string}', async function (attr, xpath, text) {
302
+ const doc = ensureXml(this);
303
+ const nodes = evalXPath(doc, xpath);
304
+ if (!nodes || nodes.length === 0) return;
305
+ assert.notStrictEqual(nodes[0].getAttribute(attr), text);
306
+ });
307
+
308
+ /**
309
+ * Assert an XML attribute contains a substring.
310
+ *
311
+ * Example #1: Then the XML attribute "id" on element "/root/item" should contain "abc"
312
+ * Example #2: Then the XML attribute "href" on element "//link" should contain "example.com"
313
+ * Example #3: And the XML attribute "type" on element "//entry" should contain "html"
314
+ * Example #4: Then the XML attribute "version" on element "/rss" should contain "2"
315
+ * Example #5: Then the XML attribute "code" on element "/error" should contain "4"
316
+ *
317
+ */
318
+ Then('the XML attribute {string} on element {string} should contain {string}', async function (attr, xpath, text) {
319
+ const doc = ensureXml(this);
320
+ const nodes = evalXPath(doc, xpath);
321
+ assert.ok(nodes && nodes.length > 0, `XML element "${xpath}" not found.`);
322
+ assert.ok((nodes[0].getAttribute(attr) || '').indexOf(text) !== -1);
323
+ });
324
+
325
+ /**
326
+ * Assert an XML attribute does NOT contain a substring.
327
+ *
328
+ * Example #1: Then the XML attribute "id" on element "/root/item" should not contain "old"
329
+ * Example #2: Then the XML attribute "href" on element "//link" should not contain "tracking"
330
+ * Example #3: And the XML attribute "type" on element "//entry" should not contain "binary"
331
+ * Example #4: Then the XML attribute "version" on element "/rss" should not contain "alpha"
332
+ * Example #5: Then the XML attribute "code" on element "/error" should not contain "5"
333
+ *
334
+ */
335
+ Then('the XML attribute {string} on element {string} should not contain {string}', async function (attr, xpath, text) {
336
+ const doc = ensureXml(this);
337
+ const nodes = evalXPath(doc, xpath);
338
+ if (!nodes || nodes.length === 0) return;
339
+ assert.ok((nodes[0].getAttribute(attr) || '').indexOf(text) === -1);
340
+ });
341
+
342
+ /**
343
+ * Assert exactly N elements match an XPath.
344
+ *
345
+ * Example #1: Then the XML element "//entry" should have 5 elements
346
+ * Example #2: Then the XML element "/root/item" should have 1 element
347
+ * Example #3: And the XML element "//error" should have 0 elements
348
+ * Example #4: Then the XML element "/feed/entry" should have 10 elements
349
+ * Example #5: Then the XML element "/orders/order" should have 3 elements
350
+ *
351
+ */
352
+ Then('the XML element {string} should have {int} element(s)', async function (xpath, count) {
353
+ const doc = ensureXml(this);
354
+ const nodes = evalXPath(doc, xpath);
355
+ assert.strictEqual((nodes || []).length, count);
356
+ });
357
+
358
+ /**
359
+ * Assert the XML root declares an `xmlns:*` namespace URI.
360
+ *
361
+ * Example #1: Then the XML should use the namespace "http://www.w3.org/2005/Atom"
362
+ * Example #2: Then the XML should use the namespace "http://www.sitemaps.org/schemas/sitemap/0.9"
363
+ * Example #3: And the XML should use the namespace "http://schemas.xmlsoap.org/soap/envelope/"
364
+ * Example #4: Then the XML should use the namespace "http://www.w3.org/1999/xhtml"
365
+ * Example #5: Then the XML should use the namespace "http://www.opengis.net/gml"
366
+ *
367
+ */
368
+ Then('the XML should use the namespace {string}', async function (ns) {
369
+ const doc = ensureXml(this);
370
+ const root = doc.documentElement;
371
+ const found = Array.from(root.attributes || []).some((a) => a.value === ns);
372
+ assert.ok(found, `Namespace "${ns}" not declared.`);
373
+ });
374
+
375
+ /**
376
+ * Assert the XML root does NOT declare a namespace URI.
377
+ *
378
+ * Example #1: Then the XML should not use the namespace "http://example.com/legacy"
379
+ * Example #2: Then the XML should not use the namespace "http://example.com/internal"
380
+ * Example #3: And the XML should not use the namespace "http://example.com/v1"
381
+ * Example #4: Then the XML should not use the namespace "http://example.com/deprecated"
382
+ * Example #5: Then the XML should not use the namespace "http://example.com/private"
383
+ *
384
+ */
385
+ Then('the XML should not use the namespace {string}', async function (ns) {
386
+ const doc = ensureXml(this);
387
+ const root = doc.documentElement;
388
+ const found = Array.from(root.attributes || []).some((a) => a.value === ns);
389
+ assert.ok(!found, `Namespace "${ns}" should not be declared.`);
390
+ });
391
+
392
+ /**
393
+ * Print the most recently set raw XML response to stdout (debug aid).
394
+ *
395
+ * Example #1: When I print last XML response
396
+ * Example #2: When I send a REST "GET" request to "/api/feed"
397
+ * And I print last XML response
398
+ * Example #3: And I print last XML response
399
+ * Example #4: Given the response content is the following:
400
+ * """
401
+ * <root/>
402
+ * """
403
+ * When I print last XML response
404
+ * Example #5: When I print last XML response
405
+ *
406
+ */
407
+ When(/^(I |we )*print last XML response$/, async function () {
408
+ console.log(this._rawResponse || '(no response)');
409
+ });