ui5-playwright-gherkin 0.1.0 → 0.3.1

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 (91) hide show
  1. package/README.md +98 -35
  2. package/bin/ui5-playwright-gherkin.js +38 -2
  3. package/docs/api.md +193 -0
  4. package/package.json +72 -30
  5. package/src/api.js +24 -0
  6. package/src/catalog.js +22 -12
  7. package/src/config.js +14 -2
  8. package/src/extend.js +21 -0
  9. package/src/find/Areas.js +33 -62
  10. package/src/find/Field.js +67 -6
  11. package/src/find/Finder.js +98 -12
  12. package/src/find/Kind.js +158 -0
  13. package/src/fixtures.js +27 -4
  14. package/src/globals.d.ts +8 -0
  15. package/src/grammar/Sentence.js +24 -11
  16. package/src/grammar/TargetReference.js +85 -0
  17. package/src/index.js +4 -10
  18. package/src/macros-text.js +93 -0
  19. package/src/macros.js +11 -0
  20. package/src/page/Ui5Page.js +337 -20
  21. package/src/page/Waiting.js +29 -1
  22. package/src/record/Recorder.js +40 -6
  23. package/src/record/capture.js +8 -5
  24. package/src/record/count.js +22 -0
  25. package/src/record/describe.js +48 -49
  26. package/src/record/facts.js +5 -1
  27. package/src/record/index.js +1 -0
  28. package/src/reporters/short-failures.js +158 -0
  29. package/src/sharedContext.js +47 -7
  30. package/src/state/ElementState.js +21 -12
  31. package/src/steps/given.js +18 -1
  32. package/src/steps/index.js +17 -3
  33. package/src/steps/macros.js +5 -0
  34. package/src/steps/then.js +28 -32
  35. package/src/steps/when.js +51 -7
  36. package/src/table/Table.js +39 -6
  37. package/src/vocabulary/area.js +68 -0
  38. package/src/vocabulary/index.js +17 -0
  39. package/src/vocabulary/kind.js +83 -0
  40. package/src/vocabulary/location.js +119 -0
  41. package/src/vocabulary/state.js +17 -0
  42. package/src/vocabulary/target.js +178 -0
  43. package/steps.json +65 -7
  44. package/types/api.d.ts +13 -0
  45. package/types/catalog.d.ts +28 -0
  46. package/types/config.d.ts +16 -0
  47. package/types/expect/playwright.d.ts +1 -0
  48. package/types/extend.d.ts +15 -0
  49. package/types/find/Areas.d.ts +31 -0
  50. package/types/find/Field.d.ts +24 -0
  51. package/types/find/Finder.d.ts +80 -0
  52. package/types/find/Kind.d.ts +75 -0
  53. package/types/find/click.d.ts +2 -0
  54. package/types/fixtures.d.ts +2 -0
  55. package/types/grammar/Sentence.d.ts +34 -0
  56. package/types/grammar/TargetReference.d.ts +58 -0
  57. package/types/index.d.ts +5 -0
  58. package/types/macros-text.d.ts +9 -0
  59. package/types/macros.d.ts +4 -0
  60. package/types/page/Sites.d.ts +15 -0
  61. package/types/page/Ui5Page.d.ts +234 -0
  62. package/types/page/Waiting.d.ts +34 -0
  63. package/types/record/Recorder.d.ts +40 -0
  64. package/types/record/capture.d.ts +8 -0
  65. package/types/record/count.d.ts +8 -0
  66. package/types/record/describe.d.ts +32 -0
  67. package/types/record/facts.d.ts +20 -0
  68. package/types/record/index.d.ts +5 -0
  69. package/types/record/steps.d.ts +2 -0
  70. package/types/registry.d.ts +9 -0
  71. package/types/reporters/short-failures.d.ts +64 -0
  72. package/types/sharedContext.d.ts +2 -0
  73. package/types/state/ElementState.d.ts +33 -0
  74. package/types/steps/given.d.ts +4 -0
  75. package/types/steps/index.d.ts +15 -0
  76. package/types/steps/macros.d.ts +1 -0
  77. package/types/steps/playwright.d.ts +1 -0
  78. package/types/steps/then.d.ts +4 -0
  79. package/types/steps/when.d.ts +4 -0
  80. package/types/table/Table.d.ts +37 -0
  81. package/types/vocabulary/area.d.ts +36 -0
  82. package/types/vocabulary/index.d.ts +5 -0
  83. package/types/vocabulary/kind.d.ts +25 -0
  84. package/types/vocabulary/location.d.ts +56 -0
  85. package/types/vocabulary/state.d.ts +8 -0
  86. package/types/vocabulary/target.d.ts +90 -0
  87. package/src/expect/browser.js +0 -2
  88. package/src/find/ControlKind.js +0 -130
  89. package/src/grammar/Location.js +0 -87
  90. package/src/grammar/Reference.js +0 -41
  91. package/src/grammar/Target.js +0 -115
package/README.md CHANGED
@@ -19,7 +19,7 @@ Feature: Orders
19
19
  ```
20
20
 
21
21
  - **One pattern, few words.** Steps are *verb · target · location*, and every combination works: easy to
22
- learn for testers, easy to write for an AI. The word names the kind of control as a user sees it
22
+ learn for testers, easy to write for an AI. The kind names the control as a user sees it
23
23
  ("button", "row", "tab"), not the UI5 class; `I press "Save"` presses anything with that text.
24
24
  - **Areas.** Each step can be narrowed to a part of the page: `at the "page footer"`, `at the "dialog"`,
25
25
  `at the "table toolbar"`, or any region by its title: `at the "Shipping address"`. Areas know
@@ -77,37 +77,80 @@ Cookies, localStorage and sessionStorage are cleared after each test. It pays of
77
77
  the network, as in end-to-end tests against a real system; served from localhost, starting UI5 on each
78
78
  page load is the cost and the cache saves little. Other state (IndexedDB, service workers) is not cleared.
79
79
 
80
- Your own steps get the same `ui5` fixture, a `Ui5Page`, and can use the package's classes:
80
+ **Journeys** (needs `reuseContext`): the scenarios of a feature tagged `@journey @mode:serial` are steps
81
+ of one process. They run in order in the same page with the same cookies, storage and remembered values;
82
+ after the first, each starts with `Given I navigate to the site "…"` and goes on where the one before
83
+ ended. `@mode:serial` (playwright-bdd) skips the rest after a failure and retries them together.
84
+
85
+ **Your own steps.** When no step says what you need, write one with the API the package's own steps
86
+ are made of (they use nothing else). The full guide is [docs/api.md](docs/api.md).
81
87
 
82
88
  ```js
83
89
  import { expect } from "@playwright/test";
84
- import { When, Then, defineSentence, ControlKind, Field, ElementState } from "ui5-playwright-gherkin";
90
+ import { When, Then, defineSentence } from "ui5-playwright-gherkin";
85
91
 
86
- // a plain step
92
+ // a plain step: find like a user, then act
87
93
  When("I log in as {string}", async ({ ui5 }, user) => {
88
- const scope = await ui5.scope(); // the open dialog, the area looked at, or the page
89
- await ui5.write(await Field.find(scope, "User"), user);
90
- await ui5.press(await ControlKind.of("button").find(scope, "Log On"));
94
+ await ui5.write(await ui5.findField("User", { editable: true }), user);
95
+ await ui5.press(await ui5.findControl("button", "Log On", { strict: true }));
91
96
  });
92
97
 
93
- // a sentence that takes every target and location, like the package's own steps
98
+ // a sentence that takes every <target> and <location>, like the package's own steps
94
99
  defineSentence(Then, "I expect that <target> is required", async ({ ui5 }, target) => {
95
- const field = await target.resolve({ input: true, strict: true });
96
- expect(await new ElementState(field).aria("required")).toBe("true");
100
+ const field = await target.findField({ strict: true });
101
+ expect(await ui5.stateOf(field).aria("required")).toBe("true");
102
+ });
103
+
104
+ // or write the target as in a step
105
+ When("I open the details of {string}", async ({ ui5 }, id) => {
106
+ await ui5.press(await ui5.locate(`the link "Details" in the row "${id}"`).find({ strict: true }));
97
107
  });
98
108
  ```
99
109
 
100
- | Class | What it is |
110
+ | | |
101
111
  |---|---|
102
- | `Ui5Page` | the app in a scenario (`ui5` fixture): `open`, `scope`, `area`, `lookAt`, `press`, `write`, `select`, `type`, `pressKey`, `useDevice`, `resize` |
103
- | `defineSentence` | registers a sentence with `<target>` (every target and location) or `<location>` |
104
- | `Reference` | a target in a location, as passed to sentences: `resolve()`, `root()`, `isVisible()` |
105
- | `ControlKind` | a word such as `button` or `row`: `find(root, text)`, `nth(root, n)`, `all(root)` |
106
- | `Field` | an input by label, accessible name or placeholder |
107
- | `Areas` | named page areas and regions by title |
108
- | `Table` | rows, counts, columns and cells of a table or list |
109
- | `ElementState` | `is("checked")`, `value()`, `valueState()` of an element |
110
- | `Finder` | the ranked, strict, explaining search the others use |
112
+ | `ui5.locate(text)` | a target and location from step text: `ui5.locate('the button "Save" at the "dialog"')` |
113
+ | `ui5.findControl(kind, text, options)` · `findText` · `findField` · `findNth` · `findArea` | find what a user means; options `at`, `inRow`, `strict`, `timeout`, `editable` |
114
+ | `ui5.press` · `write` · `select` · `hover` · `type` · `pressKey` · `open` · `navigate` · `goBack` · `lookAt` | act like a user, then wait for UI5 |
115
+ | `ui5.stateOf(element).is(state)` · `valueOf` · `count` · `table` | read what a user sees |
116
+ | `defineSentence(Keyword, sentence, fn, options)` | one sentence with `<target>` or `<location>` becomes a step for every target and location |
117
+ | `TargetReference` | what a sentence gets for `<target>`: `find()`, `findField()`, `isVisible()`, `root()` |
118
+ | `KIND_ROLES`, `PAGE_AREAS`, `STATES`, `TARGETS`, `LOCATIONS` … | every constant a step can name, one file per placeholder in [src/vocabulary/](src/vocabulary/) |
119
+ | `Kind`, `Field`, `Areas`, `Finder`, `Table`, `ElementState` | the finders underneath, for special cases |
120
+
121
+ The sources carry JSDoc types: VS Code shows parameters and docs as you type, with no setup. The
122
+ published package also ships `.d.ts` files for TypeScript projects.
123
+
124
+ **Macro steps.** A repo's own sentences, made of core steps, without code: a
125
+ `macros.feature` whose scenario names are the new sentences (`"<name>"` is a parameter):
126
+
127
+ ```gherkin
128
+ Feature: Our sentences
129
+
130
+ Scenario: I record a travel "<description>"
131
+ When I press the button "Create"
132
+ And I write "070001" into "Agency"
133
+ And I write "<description>" into "Description"
134
+ And I press the button "Create" at the "page footer"
135
+ ```
136
+
137
+ `defineUi5BddConfig({ ..., macros: "steps/macros.feature" })` registers them; then
138
+ `When I record a travel "Trip to Rome"` works in any scenario. Every line must be a core step (checked
139
+ when the file loads, with file and line); remembered values (`$name`) work; data tables don't.
140
+
141
+ **Extending.** A package for other UI libraries (such as the licensed SAPUI5 one) adds where its
142
+ controls put what a user sees, with no new steps and no fork. Call `extend` from a steps file, so
143
+ it runs in every worker:
144
+
145
+ ```js
146
+ import { extend } from "ui5-playwright-gherkin";
147
+
148
+ extend({
149
+ areas: { "filter bar": ".sapUiCompFilterBar" }, // a known area gets more controls; a new name is a new area
150
+ popupAreas: {}, // the same for popups, found anywhere in the document
151
+ kinds: { tile: ["link", "article"] } // <kind>s and the ARIA roles they stand for
152
+ });
153
+ ```
111
154
 
112
155
  ## In the browser: sandbox-playwright-gherkin
113
156
 
@@ -136,10 +179,7 @@ const summary = await runFeatures([{ uri: "orders.feature", source }], {
136
179
  }).result;
137
180
  ```
138
181
 
139
- Bundle for browsers with the `browser` condition, so `#expect` resolves to sandbox-playwright-gherkin's `expect`.
140
-
141
- The steps and classes import `expect` as `#expect` (package.json `imports`): Playwright's in Node,
142
- sandbox-playwright-gherkin's in browsers. `defineSteps({ Given, When, Then })` registers the steps with any Cucumber.
182
+ The steps and classes import `expect` as `#expect` (package.json `imports`), which is Playwright's. A browser bundle must map `#expect` to its own `expect` (an alias in the bundler). `defineSteps({ Given, When, Then })` registers the steps with any Cucumber.
143
183
 
144
184
  ## Recording
145
185
 
@@ -147,10 +187,11 @@ sandbox-playwright-gherkin's in browsers. `defineSteps({ Given, When, Then })` r
147
187
  playground, a CLI): only from what a user sees (role, accessible name, label, text), never from UI5.
148
188
 
149
189
  ```js
150
- import { describe, toSteps } from "ui5-playwright-gherkin/record";
190
+ import { countCheck, describe, toSteps } from "ui5-playwright-gherkin/record";
151
191
 
152
192
  const { text } = await describe(ui5, element); // 'the button "Delete" in the row "Monitor"'
153
193
  const { text: field } = await describe(ui5, input, { input: true }); // '"Name"' (by its label)
194
+ await countCheck(ui5, element); // in or at a table: 'Then I should see 2 rows' (what the table reports), else null
154
195
  toSteps([{ kind: "press", target: text, role: "button" }, { kind: "write", field, value: "Tablet" }]);
155
196
  ```
156
197
 
@@ -158,7 +199,9 @@ toSteps([{ kind: "press", target: text, role: "button" }, { kind: "write", field
158
199
  title or the row) and keeps the first that the steps' own resolver maps to exactly this element,
159
200
  strictly; only when no name is unique it uses a position. So a recorded step replays. `toSteps`
160
201
  merges what belongs together (opening a select box and picking an option is one `I select`) and
161
- writes Given/When/Then/And. `test/unit/record.spec.js` checks the round trip on demo kit samples.
202
+ writes Given/When/Then/And. `countCheck` counts like the step does: the counter in the title
203
+ ("Products (2)"), the growing trigger, `aria-rowcount`, else the rendered rows, so it holds for
204
+ tables that load or render only part of their rows. `test/unit/record.spec.js` checks the round trip on demo kit samples.
162
205
 
163
206
  ### Recording from the command line (early preview)
164
207
 
@@ -190,15 +233,15 @@ Every verb works with every target and every location.
190
233
 
191
234
  | Targets | |
192
235
  |---|---|
193
- | `the <word> "<text>"` | a control of a kind with that text or label: `the button "Save"`, `the tab "Items"`, `the row "HT-1000"` |
194
- | `the <n>. <word>` | by position: `the 2. row`, `the 1. input`, `the 3. button` |
236
+ | `the <kind> "<text>"` | a control of a kind with that text or label: `the button "Save"`, `the tab "Items"`, `the row "HT-1000"` |
237
+ | `the <n>. <kind>` | by position: `the 2. row`, `the 1. input`, `the 3. button` |
195
238
  | `"<text>"` | anything with that text or label: `"Save"`, `"Email"` |
196
- | `a <word>` / `an <word>` | any of a kind (only with *see* / *not see*): `a dialog`, `an option` |
239
+ | `a <kind>` / `an <kind>` | any of a kind (only with *see* / *not see*): `a dialog`, `an option` |
197
240
 
198
241
  | Locations | |
199
242
  |---|---|
200
243
  | *(none)* | the open dialog or popover, else the area looked at, else the page |
201
- | `at the "<area>"` | `page header`, `page title`, `page footer`, `page content`, `navigation`, `toolbar`, `table toolbar`, `tab bar`, `table`, `list`, `form`, `dialog`, `popover`, `message toast`, `message popover`, `view`, `page`, or any region by its title: `at the "Shipping address"` |
244
+ | `at the "<area>"` | `page header`, `page title`, `page footer`, `page content`, `navigation`, `toolbar`, `table toolbar`, `filter bar`, `tab bar`, `table`, `list`, `form`, `dialog`, `popover`, `message toast`, `message popover`, `view`, `page`, or any region by its title: `at the "Shipping address"` |
202
245
  | `in the <n>. row` | inside a row of the table or list |
203
246
  | `in the row "<text>"` | inside the row that contains the text |
204
247
 
@@ -207,11 +250,13 @@ Every verb works with every target and every location.
207
250
  | `When I press <target>` | click |
208
251
  | `When I hover over <target>` | |
209
252
  | `When I write "<value>" into <target>` | input by label, accessible name, placeholder, or position (`into the 2. input`) |
210
- | `When I select "<value>" in <target>` | Select, ComboBox, MultiComboBox |
253
+ | `When I select "<value>" in <target>` | Select, ComboBox, MultiComboBox, a field with a dropdown value help (Fiori elements V4) |
211
254
  | `Then I should see <target>` | |
212
255
  | `Then I should not see <target>` | |
213
256
  | `Then I expect that <target> is "<state>"` | see states below |
214
- | `Then the value of <target> is "<value>"` | an input's value, a Select's shown text |
257
+ | `Then the value of <target> is "<value>"` | an input's value, a Select's shown text, or a display field's text (next to its label) |
258
+ | `When I remember the value of <target> as "<name>"` | keeps the value for later steps: `$name` in any text |
259
+ | `When I remember the ID of <target> as "<name>"` | of "Description (ID)", as Fiori elements shows keys, only the ID |
215
260
 
216
261
  A few steps outside the pattern:
217
262
 
@@ -219,11 +264,14 @@ A few steps outside the pattern:
219
264
  |---|---|
220
265
  | `Given I open the site "<name>"` | a name from `ui5Sites`, or a URL/path |
221
266
  | `Given I open the site "<name>" with the parameters: "a", "b"` | fills `{0}`, `{1}` of the site's URL |
267
+ | `Given I am logged in as "<role>"` | the test user of a role from `ui5Users` (`{ employee: { username, password } }`): sent with every request to the app's host (basic auth, as test systems use); comes before `I open the site` |
268
+ | `Given I navigate to the site "<name>"` | goes there without starting over: stays when it is shown, changes only the `#hash` when the page is loaded (the launchpad switches apps) |
222
269
  | `Given I use a "phone"` | window size: `phone`, `tablet`, `desktop` |
270
+ | `When I go back` | the browser's back button (in the launchpad: the app before) |
223
271
  | `When I write "<text>"` | types into the focused field, keeps the focus (suggestions, search) |
224
272
  | `When I press "<key>" key` | `Enter`, `Escape`, `Tab`, `ArrowDown`, `Control+A` … |
225
273
  | `When I look at the "<area>"` / `When I look at the whole page` | following steps look only at that area |
226
- | `Then I should see <n> <words>` (+ location) | counts: `I should see 3 buttons at the "toolbar"`; rows count what the table reports: the counter in its title ("Products (123)"), the growing trigger ("More [ 20 / 123 ]") or `aria-rowcount` (sap.ui.table renders only the visible rows), else the rendered rows |
274
+ | `Then I should see <n> <kinds>` (+ location) | counts: `I should see 3 buttons at the "toolbar"`; rows count what the table reports: the counter in its title ("Products (123)"), the growing trigger ("More [ 20 / 123 ]") or `aria-rowcount` (sap.ui.table renders only the visible rows), else the rendered rows |
227
275
  | `Then I should see rows` (+ `at the "<area>"`) | rows like these exist, cells by column header, see below |
228
276
  | `Then I should see the columns` (+ `at the "<area>"`) | visible column headers in order; with a first column `phone`/`tablet`/`desktop`, each row is checked at that window size |
229
277
  | `Then the URL should contain "<text>"` | |
@@ -236,6 +284,17 @@ Then I should see rows
236
284
  | Notebook Basic 15 | 956.00 | Success |
237
285
  ```
238
286
 
287
+ **Remembered values:** `$name` in any text of a later step is the value remembered as `"name"` in the
288
+ same scenario (or journey). A name nothing was remembered as fails the step.
289
+
290
+ ```gherkin
291
+ When I remember the ID of "Travel" as "travel"
292
+ And I open the site "Approve Travels"
293
+ And I write "$travel" into "Travel"
294
+ And I press the button "Go"
295
+ And I press the row "$travel"
296
+ ```
297
+
239
298
  Each line must match some row, wherever it is: by column header, the cell contains the text; empty
240
299
  cells are not checked. Add `$row` (1-based) to pin a line to that row; `$highlight` is the row's highlight.
241
300
 
@@ -249,14 +308,17 @@ Then I should see the columns
249
308
  `not selected`, `pressed`, `expanded`, `collapsed`, `focused`, `editable`, `read only`; value states
250
309
  `Error`, `Warning`, `Success`, `Information`; any other word is a UI5 look, e.g. `emphasized`.
251
310
 
252
- **Words:** `button`, `link`, `tab`, `checkbox`, `radio`, `switch`, `input`/`field`, `select`, `combobox`,
311
+ **Kinds:** `button`, `link`, `tab`, `checkbox`, `radio`, `switch`, `input`/`field`, `select`, `combobox`,
253
312
  `searchbox`, `slider`, `row`/`item`, `option`, `menuitem`, `cell`, `node`, `title`/`heading`, `dialog`,
254
313
  `popover`, `image`, `icon`, `tile`, `avatar`, `step`, `toolbar`, `list`, `table`; `text`, `label`,
255
314
  `message` mean any visible text. Any other word is tried as an ARIA role.
256
315
 
316
+ Each placeholder (`<target>`, `<location>`, `<kind>`, `<area>`, `<state>`) has a file of the same name in
317
+ [src/vocabulary/](src/vocabulary/) with its constants, and the same key in `steps.json`.
318
+
257
319
  ### For testers and AI assistants
258
320
 
259
- [`steps.json`](steps.json) has the whole grammar as data: rules, verbs, targets, locations, words, states,
321
+ [`steps.json`](steps.json) has the whole grammar as data: rules, verbs, targets, locations, kinds, states,
260
322
  areas and every step expression. Give it to an AI assistant together with what the app shows, and it
261
323
  can write valid features. It is written by `npm run catalog`; a unit test fails when it is out of date.
262
324
 
@@ -278,6 +340,7 @@ Writing good steps:
278
340
  | sort in a view settings dialog | `When I press "Descending" at the "dialog"` · `And I press "Price" at the "dialog"` · `And I press "OK" at the "dialog"` |
279
341
  | pick a suggestion | `When I press the textbox "Product"` · `And I write "Note"` · `And I press the option "Notebook Basic 15"` |
280
342
  | a cell's link | `When I press the link "Details" in the row "HT-1000"` |
343
+ | find what you created, in another app | `When I remember the ID of "Travel" as "travel"` · `And I open the site "Approve Travels"` · `And I write "$travel" into "Travel"` · `And I press the button "Go"` · `And I press the row "$travel"` |
281
344
 
282
345
  ## Limitations
283
346
 
@@ -6,8 +6,11 @@
6
6
  // Early preview: see README "Recording".
7
7
 
8
8
  import { existsSync } from "node:fs";
9
- import { appendFile, writeFile } from "node:fs/promises";
9
+ import { appendFile, glob, writeFile } from "node:fs/promises";
10
+ import { resolve } from "node:path";
11
+ import { pathToFileURL } from "node:url";
10
12
  import { parseArgs } from "node:util";
13
+ import { Ui5Page } from "../src/page/Ui5Page.js";
11
14
  import { Recorder } from "../src/record/Recorder.js";
12
15
 
13
16
  const USAGE = `Usage: ui5-playwright-gherkin record <site or URL> [options]
@@ -45,9 +48,41 @@ if (!browserType?.launch) {
45
48
  process.exit(1);
46
49
  }
47
50
 
51
+ async function loadProjectConfig() {
52
+ for (const name of ["playwright.config.js", "playwright.config.mjs", "playwright.config.ts"]) {
53
+ const file = resolve(name);
54
+ if (!existsSync(file)) continue;
55
+ try {
56
+ const loaded = await import(pathToFileURL(file).href);
57
+ const use = loaded?.default?.use ?? {};
58
+ for (const pattern of JSON.parse(process.env.UI5_STEPS || "[]")) {
59
+ for await (const match of glob(pattern)) {
60
+ await import(pathToFileURL(resolve(match)).href).catch(() => {});
61
+ }
62
+ }
63
+ return use;
64
+ } catch {
65
+ return {};
66
+ }
67
+ }
68
+ return {};
69
+ }
70
+
71
+ const use = await loadProjectConfig();
48
72
  const browser = await browserType.launch({ headless: false });
49
- const context = await browser.newContext({ viewport: null });
73
+ const context = await browser.newContext({
74
+ viewport: null,
75
+ ...(use.baseURL ? { baseURL: use.baseURL } : {}),
76
+ ...(use.locale ? { locale: use.locale } : {})
77
+ });
50
78
  const page = await context.newPage();
79
+ const ui5 = new Ui5Page(page, {
80
+ sites: use.ui5Sites,
81
+ deviceSizes: use.ui5DeviceSizes,
82
+ language: use.ui5Language,
83
+ users: use.ui5Users,
84
+ baseURL: use.baseURL
85
+ });
51
86
 
52
87
  // Only the last step can still change (clicking into a field, then writing into it, is one step):
53
88
  // steps before it are printed once; in a terminal the last one is shown, dimmed, until it is final.
@@ -55,6 +90,7 @@ const tty = process.stdout.isTTY;
55
90
  let printed = 0;
56
91
  let tentative = false;
57
92
  const recorder = new Recorder(page, {
93
+ ui5,
58
94
  onSteps: (steps, { final = false } = {}) => {
59
95
  if (tentative) process.stdout.write("\x1b[1A\x1b[2K");
60
96
  const upTo = final ? steps.length : steps.length - 1;
package/docs/api.md ADDED
@@ -0,0 +1,193 @@
1
+ # Writing your own steps
2
+
3
+ When no step says what you need, write one with the API this package's own steps are made of.
4
+ Every step in [`src/steps/`](../src/steps/) uses only this API, and a unit test keeps it that way, so
5
+ each step there is also an example.
6
+
7
+ ```js
8
+ // features/steps/approve.js, listed in defineUi5BddConfig({ steps: "features/steps/*.js" })
9
+ import { When, Then, defineSentence } from "ui5-playwright-gherkin";
10
+ import { expect } from "@playwright/test";
11
+
12
+ // A fixed sentence
13
+ When("I approve the travel", async ({ ui5 }) => {
14
+ await ui5.press(await ui5.findControl("button", "Approve", { at: "page footer", strict: true }));
15
+ await ui5.press(await ui5.findControl("button", "OK", { at: "dialog", strict: true }));
16
+ });
17
+
18
+ // A sentence with <target>: one step for every target and every location, like the built-in ones
19
+ // When I approve the row "4711" · When I approve the 2. row at the "Travels"
20
+ defineSentence(When, "I approve <target>", async ({ ui5 }, target) => {
21
+ await ui5.press(await target.find({ strict: true }));
22
+ await ui5.press(await ui5.findControl("button", "Approve", { strict: true }));
23
+ });
24
+
25
+ // Your own check, written in step text
26
+ Then("the travel is approved", async ({ ui5 }) => {
27
+ const status = await ui5.locate('the text "Approved" at the "page header"').find();
28
+ await expect(status).toBeVisible();
29
+ });
30
+ ```
31
+
32
+ In the browser (sandbox-playwright-gherkin), import from `ui5-playwright-gherkin/api`: it has
33
+ everything below without Playwright's test runner.
34
+
35
+ ## How it fits together
36
+
37
+ ```
38
+ step text When I press the button "Save" at the "page footer"
39
+ verb <target> <location>
40
+ │ │
41
+ src/vocabulary/ target.js ─────┘ location.js ───┘ what a step can say (data and grammar)
42
+ kind.js area.js state.js
43
+ │
44
+ src/grammar/ defineSentence → TargetReference one sentence → all its steps
45
+ │
46
+ src/find/ … Kind Field Areas Finder Table ElementState finding as a user would
47
+ │
48
+ src/page/Ui5Page.js ui5.find…() · ui5.press() · ui5.write() … the `ui5` fixture
49
+ ```
50
+
51
+ Each `<placeholder>` of a step has a file of the same name in [`src/vocabulary/`](../src/vocabulary/)
52
+ and the same key in [`steps.json`](../steps.json):
53
+
54
+ | In a step | File | Constants | `steps.json` |
55
+ |---|---|---|---|
56
+ | `<target>` | [target.js](../src/vocabulary/target.js) | `TARGETS`, `SEEABLE_TARGETS`; classes `KindWithText`, `KindAtPosition`, `AnyWithText`, `AnyOfKind`; `quote` | `targets` |
57
+ | `<location>` | [location.js](../src/vocabulary/location.js) | `LOCATIONS`, `TABLE_LOCATIONS`; classes `InScope`, `AtArea`, `InRowAt`, `InRowContaining` | `locations` |
58
+ | `<kind>` | [kind.js](../src/vocabulary/kind.js) | `KIND_ROLES`, `TEXT_KINDS`, `POPUP_KINDS`, `ROW_KINDS`, `INPUT_KINDS`, `INPUT_ROLES`, `TYPEABLE_SELECTOR`, `ROLE_KINDS` | `kinds` |
59
+ | `<area>` | [area.js](../src/vocabulary/area.js) | `PAGE_AREAS`, `POPUP_AREAS`, `SPECIAL_AREAS`, `AREA_ALIASES`, `REGION_ROLES`, `OPEN_POPUPS`, `OPENED` | `areas` |
60
+ | `<state>` | [state.js](../src/vocabulary/state.js) | `STATES`, `ARIA_STATES`, `VALUE_STATES`, `INPUT_STATES` | `states` |
61
+
62
+ All of them are exported by the package: `import { KIND_ROLES, PAGE_AREAS } from "ui5-playwright-gherkin"`.
63
+
64
+ ## The `ui5` fixture (`Ui5Page`)
65
+
66
+ Every step gets `{ ui5, page }`: `ui5` is the app as a scenario sees it, `page` Playwright's page.
67
+
68
+ ### Finding what a user means
69
+
70
+ All find methods wait until the element shows (default 10 s, `timeout` in ms), and look where a step
71
+ without a location looks (the open dialog, else the area looked at, else the page) unless you say
72
+ where. They return a Playwright `Locator`.
73
+
74
+ | Method | Like in a step | |
75
+ |---|---|---|
76
+ | `ui5.locate(text)` | any `<target><location>` | a `TargetReference` from step text: `ui5.locate('the link "Details" in the row "HT-1000"')` |
77
+ | `ui5.findControl(kind, text, options)` | `the button "Save"` | a control of a `<kind>` with that text or label |
78
+ | `ui5.findText(text, options)` | `"Save"` | anything with that text: a control named so, a label, a text |
79
+ | `ui5.findField(label, options)` | `I write "x" into "Name"` | an input by label, accessible name, placeholder, tooltip; `editable: false` takes display fields too |
80
+ | `ui5.findNth(kind, position, options)` | `the 2. row` | the n-th (1-based) visible one of a kind |
81
+ | `ui5.findArea(name)` | `at the "page footer"` | an `<area>`, or a region by its title |
82
+ | `ui5.scope()` | (no location) | where steps look without a location |
83
+
84
+ Options, all optional:
85
+
86
+ | Option | Means | Like in a step |
87
+ |---|---|---|
88
+ | `at: "page footer"` | inside this area | `at the "page footer"` |
89
+ | `inRow: "HT-1000"` | inside the row containing this text | `in the row "HT-1000"` |
90
+ | `inRow: 2` | inside the 2. row | `in the 2. row` |
91
+ | `strict: true` | fail when several elements match equally well, instead of taking the first | (every action) |
92
+ | `timeout: 5000` | wait this long, in ms | |
93
+ | `editable: true` | `findField` only: inputs a user can type into | `I write` |
94
+
95
+ ### Acting
96
+
97
+ After each action the page is waited for: not busy, popups opened, animations ended. Do not add waits.
98
+
99
+ | Method | |
100
+ |---|---|
101
+ | `ui5.press(element)` | clicks like a user (on the control when a part of it covers the element) |
102
+ | `ui5.hover(element)` | |
103
+ | `ui5.write(input, value)` | fills and leaves the field (UI5's change event) |
104
+ | `ui5.select(box, value)` | opens a Select / ComboBox / value help and picks the option |
105
+ | `ui5.type(text)` | types into the focused field, keeps the focus |
106
+ | `ui5.pressKey(key)` | `"Enter"`, `"Escape"`, `"Control+A"` … |
107
+ | `ui5.openToolbarOverflowIfPresent()` | opens the `"Additional Options"` menu when the toolbar overflow trigger is visible |
108
+ | `ui5.open(site, parameters)` · `ui5.navigate(site)` · `ui5.goBack()` | sites from `ui5Sites` |
109
+ | `ui5.lookAt(area)` | following finds look only there; `null` for the whole page |
110
+ | `ui5.logInAs(role)` · `ui5.useDevice(name)` · `ui5.resize(size)` | |
111
+ | `ui5.remember(name, value)` · `ui5.recall(text)` | `$name` values |
112
+
113
+ ### Reading and checking
114
+
115
+ These do not wait; poll them: `await expect.poll(() => …).toBe(…)`.
116
+
117
+ | Method | |
118
+ |---|---|
119
+ | `ui5.stateOf(element).is(state)` | a `<state>`: `"disabled"`, `"selected"`, `"Error"`, a UI5 look like `"emphasized"` |
120
+ | `ui5.valueOf(element)` | an input's value, a Select's text, a display field's text |
121
+ | `ui5.count(kind, where)` | visible elements of a kind; rows count what the table reports |
122
+ | `ui5.table(where)` | a `Table`: `expectSomeRow({ Product: "Notebook" })`, `expectRow(2, {...})`, `visibleColumns()` |
123
+ | `reference.isVisible()` | is a `TargetReference` visible right now? |
124
+
125
+ ```js
126
+ const save = await ui5.findControl("button", "Save");
127
+ await expect.poll(() => ui5.stateOf(save).is("enabled")).toBe(true);
128
+ await expect.poll(() => ui5.count("row", { at: "Products" })).toBe(3);
129
+ ```
130
+
131
+ ## `defineSentence`: one sentence, every target and location
132
+
133
+ ```js
134
+ defineSentence(Keyword, sentence, async ({ ui5, page }, target, ...values) => {}, options)
135
+ ```
136
+
137
+ - `sentence` contains `<target>` (a target and a location) or `<location>` (a location only), plus
138
+ Cucumber parameters of its own: `"I write {string} into <target>"`.
139
+ - It registers one step per target and location: `I approve the {word} {string}`,
140
+ `… at the {string}`, `I approve the {int}. {word} in the row {string}` …
141
+ - The function gets the fixtures, a `TargetReference`, then the values of the sentence's own parameters
142
+ (and a data table, with `dataTable: true`).
143
+ - `options.targets`: `TARGETS` (default) or `SEEABLE_TARGETS` (also `a dialog`); `options.locations`:
144
+ `LOCATIONS` (default) or `TABLE_LOCATIONS` (page or area only).
145
+
146
+ A `TargetReference` is the `<target>` in its `<location>`:
147
+
148
+ | | |
149
+ |---|---|
150
+ | `await target.find({ strict: true })` | the element it means |
151
+ | `await target.findField({ editable: true, strict: true })` | the input it means (by label, or `the 2. input`) |
152
+ | `await target.isVisible()` | visible right now? |
153
+ | `await target.root()` | where it is looked for (for `<location>`-only sentences) |
154
+ | `String(target)` | as written in the step: `the button "Save" at the "dialog"`, for messages |
155
+ | `target.target`, `target.location` | the parts: `target.target instanceof AnyWithText` |
156
+
157
+ ## More kinds and areas without new steps: `extend`
158
+
159
+ ```js
160
+ import { extend } from "ui5-playwright-gherkin/extend";
161
+
162
+ extend({
163
+ kinds: { card: ["article", "group"] }, // the card "Sales"
164
+ areas: { "filter bar": ".sapUiCompFilterBar" }, // at the "filter bar" (added to the known one)
165
+ popupAreas: { "side panel": ".mySidePanel" } // found anywhere in the document
166
+ });
167
+ ```
168
+
169
+ Call it where your steps file loads, so it runs in every worker. The added names show up in
170
+ `steps.json` after `npm run catalog`.
171
+
172
+ ## Underneath, for special cases
173
+
174
+ When a `ui5` method is not enough, the classes it uses are exported too:
175
+
176
+ | Class | |
177
+ |---|---|
178
+ | `Kind.of("button")` | `.find(root, text)`, `.findNth(root, n)`, `.findAll(root)`, `.count(root)`, `.candidates(root, text)`, `.roles` |
179
+ | `Field.find(root, label, options)` | the input finder behind `findField` |
180
+ | `Areas.find(page, within, name)` | the area finder behind `findArea` |
181
+ | `Finder.findOne(candidates, options)` | the first visible match of locators tried best first; `Finder.asPartialMatch(locators)` marks weaker ones; `Finder.findAll`, `Finder.isAnyVisible` |
182
+ | `Table.in(root)`, `Table.rows(root)`, `Table.count(root)` | tables and lists |
183
+ | `new ElementState(locator)` | `.is(state)`, `.value()`, `.aria(name)` |
184
+
185
+ The rule stays the same as for the built-in steps: the DOM only. Find elements by ARIA role,
186
+ accessible name, label and visible text, never through UI5's runtime.
187
+
188
+ ## Types in the editor
189
+
190
+ The sources carry JSDoc types. VS Code reads them without setup: autocomplete, parameter hints, docs on
191
+ hover. In this repository, `jsconfig.json` also checks `src/` as you type. For TypeScript projects,
192
+ the published package ships `.d.ts` files, which `npm run types` writes to `types/` before publishing
193
+ (it needs `typescript` installed).
package/package.json CHANGED
@@ -1,17 +1,27 @@
1
1
  {
2
2
  "name": "ui5-playwright-gherkin",
3
- "version": "0.1.0",
3
+ "version": "0.3.1",
4
4
  "description": "Gherkin (Cucumber) steps for testing SAPUI5/OpenUI5 apps with Playwright, driving the UI through the DOM only",
5
+ "homepage": "https://bkrrrr.github.io/ui5-playwright-gherkin/",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/bkrrrr/ui5-playwright-gherkin.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/bkrrrr/ui5-playwright-gherkin/issues"
12
+ },
5
13
  "main": "./src/index.js",
6
14
  "scripts": {
7
15
  "bddgen": "bddgen",
8
- "test": "bddgen && playwright test",
9
- "test:samples": "bddgen && playwright test --grep @smoke",
16
+ "test": "bddgen && playwright test --max-failures=10",
17
+ "test:samples": "bddgen && playwright test --grep @smoke --max-failures=10",
18
+ "test:steps": "bddgen && playwright test --grep-invert @smoke --max-failures=10",
10
19
  "serve": "node test/host/server.js",
11
20
  "features:samples": "node scripts/generate-sample-features.js",
12
21
  "lint": "eslint .",
13
- "test:steps": "bddgen && playwright test --grep-invert @smoke",
14
- "catalog": "node scripts/write-catalog.js"
22
+ "catalog": "node scripts/write-catalog.js",
23
+ "types": "tsc -p tsconfig.types.json",
24
+ "prepublishOnly": "npm run types"
15
25
  },
16
26
  "keywords": [
17
27
  "ui5",
@@ -25,43 +35,71 @@
25
35
  "license": "PolyForm-Noncommercial-1.0.0",
26
36
  "type": "module",
27
37
  "devDependencies": {
28
- "@cucumber/cucumber-expressions": "19.0.0",
29
- "@eslint/js": "9.39.5",
30
- "@playwright/test": "1.64.1",
31
- "eslint": "9.39.5",
32
- "globals": "17.13.0",
33
- "playwright-bdd": "9.2.1"
38
+ "@eslint/js": "10.0.1",
39
+ "@playwright/test": "1.65.1",
40
+ "@types/node": "^26.6.11",
41
+ "@types/punycode": "^2.1.4",
42
+ "eslint": "10.16.0",
43
+ "globals": "17.14.0",
44
+ "playwright-bdd": "9.2.1",
45
+ "typescript": "^7.0.2"
34
46
  },
35
47
  "exports": {
36
- ".": "./src/index.js",
37
- "./steps/*": "./src/steps/*.js",
38
- "./dom": "./src/dom.js",
39
- "./steps.json": "./steps.json",
40
- "./catalog": "./src/catalog.js",
41
- "./page": "./src/page/Ui5Page.js",
42
- "./record": "./src/record/index.js"
48
+ ".": {
49
+ "types": "./types/index.d.ts",
50
+ "default": "./src/index.js"
51
+ },
52
+ "./api": {
53
+ "types": "./types/api.d.ts",
54
+ "default": "./src/api.js"
55
+ },
56
+ "./catalog": {
57
+ "types": "./types/catalog.d.ts",
58
+ "default": "./src/catalog.js"
59
+ },
60
+ "./extend": {
61
+ "types": "./types/extend.d.ts",
62
+ "default": "./src/extend.js"
63
+ },
64
+ "./macros": {
65
+ "types": "./types/macros.d.ts",
66
+ "browser": "./src/macros-text.js",
67
+ "default": "./src/macros.js"
68
+ },
69
+ "./macros-text": {
70
+ "types": "./types/macros-text.d.ts",
71
+ "default": "./src/macros-text.js"
72
+ },
73
+ "./page": {
74
+ "types": "./types/page/Ui5Page.d.ts",
75
+ "default": "./src/page/Ui5Page.js"
76
+ },
77
+ "./record": {
78
+ "types": "./types/record/index.d.ts",
79
+ "default": "./src/record/index.js"
80
+ },
81
+ "./steps/*": {
82
+ "types": "./types/steps/*.d.ts",
83
+ "default": "./src/steps/*.js"
84
+ },
85
+ "./steps.json": "./steps.json"
43
86
  },
44
87
  "imports": {
45
- "#expect": {
46
- "browser": "./src/expect/browser.js",
47
- "default": "./src/expect/playwright.js"
48
- }
88
+ "#expect": "./src/expect/playwright.js"
49
89
  },
50
90
  "files": [
51
91
  "bin",
52
92
  "src",
93
+ "types",
94
+ "docs",
53
95
  "steps.json",
54
96
  "LICENSE.md"
55
97
  ],
56
98
  "peerDependencies": {
57
99
  "@playwright/test": ">=1.50",
58
- "playwright-bdd": ">=8",
59
- "sandbox-playwright-gherkin": ">=0.1.0"
100
+ "playwright-bdd": ">=8"
60
101
  },
61
102
  "peerDependenciesMeta": {
62
- "sandbox-playwright-gherkin": {
63
- "optional": true
64
- },
65
103
  "@playwright/test": {
66
104
  "optional": true
67
105
  },
@@ -70,9 +108,13 @@
70
108
  }
71
109
  },
72
110
  "engines": {
73
- "node": ">=20"
111
+ "node": ">=26"
74
112
  },
75
113
  "bin": {
76
- "ui5-playwright-gherkin": "./bin/ui5-playwright-gherkin.js"
77
- }
114
+ "ui5-playwright-gherkin": "bin/ui5-playwright-gherkin.js"
115
+ },
116
+ "dependencies": {
117
+ "@cucumber/cucumber-expressions": "^20.2.0"
118
+ },
119
+ "types": "./types/index.d.ts"
78
120
  }