@testspectra/matchers 1.0.1 → 1.0.42

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.
@@ -1,21 +1,148 @@
1
- import { SingleElementMatcher, ElementTarget } from "../types.js";
1
+ import { SingleElementMatcher, ElementTarget, TypeOptions } from "../types.js";
2
+ /**
3
+ * Fluent interaction and assertion runner for a single element or browser context.
4
+ *
5
+ * Implements `PromiseLike<void>`, allowing it to be awaited directly or chained with
6
+ * multiple actions and `.should(...)` assertions.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // Direct action + chained assertion
11
+ * await Spectra.get("#submit-btn")
12
+ * .click()
13
+ * .should("not.be.visible");
14
+ *
15
+ * // Query + multiple assertions
16
+ * await Spectra.get(LoginPage.usernameInput)
17
+ * .should("be.visible")
18
+ * .should("be.enabled")
19
+ * .should("have.value", "admin");
20
+ * ```
21
+ */
2
22
  export declare class SingleElementRunner implements PromiseLike<void> {
3
23
  private _target;
4
24
  private _action;
5
25
  private _assertions;
6
26
  constructor(target?: ElementTarget, action?: () => Promise<any>);
27
+ /**
28
+ * Clicks on the target element.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * await Spectra.get("#login-btn").click();
33
+ * ```
34
+ * @returns Current runner instance for method chaining.
35
+ */
7
36
  click(): this;
37
+ /**
38
+ * Double-clicks on the target element.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * await Spectra.get(".file-row").doubleClick();
43
+ * ```
44
+ * @returns Current runner instance for method chaining.
45
+ */
8
46
  doubleClick(): this;
47
+ /**
48
+ * Performs a long-press touch or hold gesture on the target element.
49
+ *
50
+ * @param durationMs - Duration in milliseconds to hold (default: 1000).
51
+ * @example
52
+ * ```ts
53
+ * await Spectra.get("#context-menu-trigger").longPress(1500);
54
+ * ```
55
+ * @returns Current runner instance for method chaining.
56
+ */
9
57
  longPress(durationMs?: number): this;
10
- type(value: string, options?: {
11
- clearFirst?: boolean;
12
- }): this;
58
+ /**
59
+ * Types string value into the target input element.
60
+ *
61
+ * @param value - Text string to type into the element.
62
+ * @param options - Optional typing options (e.g. `clearFirst: true`).
63
+ * @example
64
+ * ```ts
65
+ * await Spectra.get("#search-input").type("iPhone 15", { clearFirst: true });
66
+ * ```
67
+ * @returns Current runner instance for method chaining.
68
+ */
69
+ type(value: string, options?: TypeOptions): this;
70
+ /**
71
+ * Clears the current value of the target input element.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * await Spectra.get("#search-input").clear();
76
+ * ```
77
+ * @returns Current runner instance for method chaining.
78
+ */
13
79
  clear(): this;
80
+ /**
81
+ * Selects an option from a `<select>` dropdown by its visible text.
82
+ *
83
+ * @param value - Visible text of the option to select.
84
+ * @example
85
+ * ```ts
86
+ * await Spectra.get("#country-select").select("United States");
87
+ * ```
88
+ * @returns Current runner instance for method chaining.
89
+ */
14
90
  select(value: string): this;
91
+ /**
92
+ * Moves the mouse cursor over the target element (hover).
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * await Spectra.get("#dropdown-menu-trigger").hover();
97
+ * ```
98
+ * @returns Current runner instance for method chaining.
99
+ */
15
100
  hover(): this;
101
+ /**
102
+ * Waits for the target element to become displayed in the DOM.
103
+ *
104
+ * @param timeoutMs - Maximum duration in milliseconds to wait (default: 10000).
105
+ * @example
106
+ * ```ts
107
+ * await Spectra.get("#modal-dialog").waitForElement(5000);
108
+ * ```
109
+ * @returns Current runner instance for method chaining.
110
+ */
16
111
  waitForElement(timeoutMs?: number): this;
112
+ /**
113
+ * Queues an assertion matcher against the current target element or page.
114
+ *
115
+ * @param matcher - Single element matcher string (e.g. `'be.visible'`, `'have.text'`).
116
+ * @param args - Additional parameters for the matcher (e.g. expected text or value).
117
+ * @example
118
+ * ```ts
119
+ * await Spectra.get("#status-badge").should("be.visible");
120
+ * await Spectra.get("#username").should("have.value", "alice");
121
+ * await Spectra.get("#banner").should("have.class", "alert-success");
122
+ * ```
123
+ * @returns Current runner instance for method chaining.
124
+ */
17
125
  should(matcher: SingleElementMatcher, ...args: any[]): this;
126
+ /**
127
+ * Queues an assertion matcher targeting a specific element.
128
+ *
129
+ * @param target - Custom element target for this specific assertion.
130
+ * @param matcher - Single element matcher string.
131
+ * @param args - Additional parameters for the matcher.
132
+ * @example
133
+ * ```ts
134
+ * await Spectra.should(LoginPage.submitBtn, "be.enabled");
135
+ * ```
136
+ * @returns Current runner instance for method chaining.
137
+ */
18
138
  should(target: ElementTarget, matcher: SingleElementMatcher, ...args: any[]): this;
139
+ /**
140
+ * Standard Promise `then` implementation.
141
+ * Executes queued action first (if any), followed sequentially by all queued assertions.
142
+ */
19
143
  then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
144
+ /**
145
+ * Helper that throws an error if an action is called without a defined target.
146
+ */
20
147
  private requireTarget;
21
148
  }
@@ -1,4 +1,24 @@
1
1
  import { executeSingleMatcher, resolveElement } from "../matchers.js";
2
+ /**
3
+ * Fluent interaction and assertion runner for a single element or browser context.
4
+ *
5
+ * Implements `PromiseLike<void>`, allowing it to be awaited directly or chained with
6
+ * multiple actions and `.should(...)` assertions.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // Direct action + chained assertion
11
+ * await Spectra.get("#submit-btn")
12
+ * .click()
13
+ * .should("not.be.visible");
14
+ *
15
+ * // Query + multiple assertions
16
+ * await Spectra.get(LoginPage.usernameInput)
17
+ * .should("be.visible")
18
+ * .should("be.enabled")
19
+ * .should("have.value", "admin");
20
+ * ```
21
+ */
2
22
  export class SingleElementRunner {
3
23
  _target;
4
24
  _action = null;
@@ -8,6 +28,15 @@ export class SingleElementRunner {
8
28
  this._action = action || null;
9
29
  }
10
30
  // --- Chained Actions on current target ---
31
+ /**
32
+ * Clicks on the target element.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * await Spectra.get("#login-btn").click();
37
+ * ```
38
+ * @returns Current runner instance for method chaining.
39
+ */
11
40
  click() {
12
41
  const target = this.requireTarget("click");
13
42
  this._action = async () => {
@@ -16,6 +45,15 @@ export class SingleElementRunner {
16
45
  };
17
46
  return this;
18
47
  }
48
+ /**
49
+ * Double-clicks on the target element.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * await Spectra.get(".file-row").doubleClick();
54
+ * ```
55
+ * @returns Current runner instance for method chaining.
56
+ */
19
57
  doubleClick() {
20
58
  const target = this.requireTarget("doubleClick");
21
59
  this._action = async () => {
@@ -24,6 +62,16 @@ export class SingleElementRunner {
24
62
  };
25
63
  return this;
26
64
  }
65
+ /**
66
+ * Performs a long-press touch or hold gesture on the target element.
67
+ *
68
+ * @param durationMs - Duration in milliseconds to hold (default: 1000).
69
+ * @example
70
+ * ```ts
71
+ * await Spectra.get("#context-menu-trigger").longPress(1500);
72
+ * ```
73
+ * @returns Current runner instance for method chaining.
74
+ */
27
75
  longPress(durationMs = 1000) {
28
76
  const target = this.requireTarget("longPress");
29
77
  this._action = async () => {
@@ -40,6 +88,17 @@ export class SingleElementRunner {
40
88
  };
41
89
  return this;
42
90
  }
91
+ /**
92
+ * Types string value into the target input element.
93
+ *
94
+ * @param value - Text string to type into the element.
95
+ * @param options - Optional typing options (e.g. `clearFirst: true`).
96
+ * @example
97
+ * ```ts
98
+ * await Spectra.get("#search-input").type("iPhone 15", { clearFirst: true });
99
+ * ```
100
+ * @returns Current runner instance for method chaining.
101
+ */
43
102
  type(value, options) {
44
103
  const target = this.requireTarget("type");
45
104
  this._action = async () => {
@@ -51,6 +110,15 @@ export class SingleElementRunner {
51
110
  };
52
111
  return this;
53
112
  }
113
+ /**
114
+ * Clears the current value of the target input element.
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * await Spectra.get("#search-input").clear();
119
+ * ```
120
+ * @returns Current runner instance for method chaining.
121
+ */
54
122
  clear() {
55
123
  const target = this.requireTarget("clear");
56
124
  this._action = async () => {
@@ -59,6 +127,16 @@ export class SingleElementRunner {
59
127
  };
60
128
  return this;
61
129
  }
130
+ /**
131
+ * Selects an option from a `<select>` dropdown by its visible text.
132
+ *
133
+ * @param value - Visible text of the option to select.
134
+ * @example
135
+ * ```ts
136
+ * await Spectra.get("#country-select").select("United States");
137
+ * ```
138
+ * @returns Current runner instance for method chaining.
139
+ */
62
140
  select(value) {
63
141
  const target = this.requireTarget("select");
64
142
  this._action = async () => {
@@ -67,6 +145,15 @@ export class SingleElementRunner {
67
145
  };
68
146
  return this;
69
147
  }
148
+ /**
149
+ * Moves the mouse cursor over the target element (hover).
150
+ *
151
+ * @example
152
+ * ```ts
153
+ * await Spectra.get("#dropdown-menu-trigger").hover();
154
+ * ```
155
+ * @returns Current runner instance for method chaining.
156
+ */
70
157
  hover() {
71
158
  const target = this.requireTarget("hover");
72
159
  this._action = async () => {
@@ -75,6 +162,16 @@ export class SingleElementRunner {
75
162
  };
76
163
  return this;
77
164
  }
165
+ /**
166
+ * Waits for the target element to become displayed in the DOM.
167
+ *
168
+ * @param timeoutMs - Maximum duration in milliseconds to wait (default: 10000).
169
+ * @example
170
+ * ```ts
171
+ * await Spectra.get("#modal-dialog").waitForElement(5000);
172
+ * ```
173
+ * @returns Current runner instance for method chaining.
174
+ */
78
175
  waitForElement(timeoutMs = 10000) {
79
176
  const target = this.requireTarget("waitForElement");
80
177
  this._action = async () => {
@@ -111,6 +208,10 @@ export class SingleElementRunner {
111
208
  return this;
112
209
  }
113
210
  // --- Execution when awaited ---
211
+ /**
212
+ * Standard Promise `then` implementation.
213
+ * Executes queued action first (if any), followed sequentially by all queued assertions.
214
+ */
114
215
  async then(onfulfilled, onrejected) {
115
216
  try {
116
217
  if (this._action) {
@@ -129,6 +230,9 @@ export class SingleElementRunner {
129
230
  throw err;
130
231
  }
131
232
  }
233
+ /**
234
+ * Helper that throws an error if an action is called without a defined target.
235
+ */
132
236
  requireTarget(actionName) {
133
237
  if (!this._target) {
134
238
  throw new Error(`Cannot execute '${actionName}' without specifying an element target.`);
package/dist/spectra.d.ts CHANGED
@@ -2,33 +2,223 @@ import { ClickOptions, ElementTarget, KeyOption, LongPressOptions, ScrollOptions
2
2
  import { SingleElementRunner } from "./runner/single.js";
3
3
  import { MultiElementRunner } from "./runner/collection.js";
4
4
  /**
5
- * Main Spectra testing API orchestrator.
6
- * Combines direct actions, single element queries (get), and collections (getAll).
5
+ * Main TestSpectra cross-platform automation and assertion orchestrator.
6
+ *
7
+ * Provides a unified, ergonomic testing API across Web, Android, and iOS.
8
+ * Features single element targeting (`.get`), collection targeting (`.getAll`),
9
+ * and built-in actions (`.click`, `.type`, `.scroll`, `.swipe`, `.navigate`).
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * // Single element interaction
14
+ * await Spectra.get("#username").type("admin");
15
+ *
16
+ * // Collection assertion
17
+ * await Spectra.getAll(".item").should("have.length", 5);
18
+ *
19
+ * // Mobile touch swipe
20
+ * await Spectra.swipe({ direction: "left", distance: 350 });
21
+ * ```
7
22
  */
8
23
  export declare class SpectraStatic {
9
24
  /**
10
- * Targets a single element by selector or Page Object element.
25
+ * Targets a single element by CSS/XPath selector or Page Object element.
26
+ *
27
+ * @param target - Selector string or Page Object element property.
28
+ * @example
29
+ * ```ts
30
+ * await Spectra.get("#login-btn").click();
31
+ * await Spectra.get(LoginPage.submitButton).should("be.visible");
32
+ * ```
33
+ * @returns `SingleElementRunner` for chaining actions and assertions.
11
34
  */
12
35
  get(target: ElementTarget): SingleElementRunner;
13
36
  /**
14
- * Targets multiple elements / collections by selector.
37
+ * Targets multiple elements or collections matching a CSS/XPath selector.
38
+ *
39
+ * @param selector - Selector string matching multiple elements.
40
+ * @example
41
+ * ```ts
42
+ * await Spectra.getAll(".product-card").should("have.length.greaterThan", 0);
43
+ * await Spectra.getAll("li.tab").first().click();
44
+ * ```
45
+ * @returns `MultiElementRunner` for collection assertions and indexing.
15
46
  */
16
47
  getAll(selector: string): MultiElementRunner;
48
+ /**
49
+ * Navigates the browser to the specified URL.
50
+ *
51
+ * @param url - Relative URL or absolute URL.
52
+ * @example
53
+ * ```ts
54
+ * await Spectra.navigate("/login");
55
+ * await Spectra.navigate("https://app.testspectra.dev/dashboard");
56
+ * ```
57
+ */
17
58
  navigate(url: string): SingleElementRunner;
59
+ /**
60
+ * Navigates back one step in the browser / app history.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * await Spectra.back();
65
+ * ```
66
+ */
18
67
  back(): SingleElementRunner;
68
+ /**
69
+ * Refreshes the current browser page.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * await Spectra.refresh();
74
+ * ```
75
+ */
19
76
  refresh(): SingleElementRunner;
77
+ /**
78
+ * Clicks on the specified element.
79
+ *
80
+ * @param target - Selector string or Page Object element property.
81
+ * @param textOrOptions - Optional text filter or click configuration.
82
+ * @example
83
+ * ```ts
84
+ * await Spectra.click("#submit-button");
85
+ * await Spectra.click(LoginPage.loginBtn);
86
+ * ```
87
+ */
20
88
  click(target: ElementTarget, textOrOptions?: string | ClickOptions): SingleElementRunner;
89
+ /**
90
+ * Performs a double-click on the specified element.
91
+ *
92
+ * @param target - Selector string or Page Object element property.
93
+ * @param textOrOptions - Optional text filter or click configuration.
94
+ * @example
95
+ * ```ts
96
+ * await Spectra.doubleClick(".grid-row");
97
+ * ```
98
+ */
21
99
  doubleClick(target: ElementTarget, textOrOptions?: string | ClickOptions): SingleElementRunner;
100
+ /**
101
+ * Performs a long-press touch or hold gesture on the specified element.
102
+ *
103
+ * @param target - Selector string or Page Object element property.
104
+ * @param options - Optional duration in ms or LongPressOptions object.
105
+ * @example
106
+ * ```ts
107
+ * await Spectra.longPress("#message-item", 1500);
108
+ * await Spectra.longPress(ChatPage.audioRecordBtn, { duration: 2000 });
109
+ * ```
110
+ */
22
111
  longPress(target: ElementTarget, options?: LongPressOptions | number): SingleElementRunner;
112
+ /**
113
+ * Types text into the specified element.
114
+ *
115
+ * @param target - Selector string or Page Object element property.
116
+ * @param value - Text string to enter.
117
+ * @param options - Optional typing options (e.g. `clearFirst: true`).
118
+ * @example
119
+ * ```ts
120
+ * await Spectra.type("#email", "user@example.com", { clearFirst: true });
121
+ * await Spectra.type(LoginPage.passwordInput, "SecretPass123");
122
+ * ```
123
+ */
23
124
  type(target: ElementTarget, value: string, options?: TypeOptions): SingleElementRunner;
125
+ /**
126
+ * Clears the input content from the specified element.
127
+ *
128
+ * @param target - Selector string or Page Object element property.
129
+ * @example
130
+ * ```ts
131
+ * await Spectra.clear("#coupon-input");
132
+ * ```
133
+ */
24
134
  clear(target: ElementTarget): SingleElementRunner;
135
+ /**
136
+ * Selects an option from a `<select>` dropdown by its visible text.
137
+ *
138
+ * @param target - Selector string or Page Object element property.
139
+ * @param value - Visible text of the option to select.
140
+ * @example
141
+ * ```ts
142
+ * await Spectra.select("#shipping-country", "Indonesia");
143
+ * ```
144
+ */
25
145
  select(target: ElementTarget, value: string): SingleElementRunner;
146
+ /**
147
+ * Hovers the mouse pointer over the specified element.
148
+ *
149
+ * @param target - Selector string or Page Object element property.
150
+ * @example
151
+ * ```ts
152
+ * await Spectra.hover("#nav-profile-menu");
153
+ * ```
154
+ */
26
155
  hover(target: ElementTarget): SingleElementRunner;
156
+ /**
157
+ * Simulates pressing a specific keyboard key.
158
+ *
159
+ * @param key - Key name string (e.g. `'Enter'`, `'Tab'`, `'Escape'`).
160
+ * @example
161
+ * ```ts
162
+ * await Spectra.pressKey("Enter");
163
+ * await Spectra.pressKey("Escape");
164
+ * ```
165
+ */
27
166
  pressKey(key: KeyOption | string): SingleElementRunner;
167
+ /**
168
+ * Drags a source element and drops it onto a destination target.
169
+ *
170
+ * @param sourceTarget - Element to drag.
171
+ * @param destTarget - Destination element or drop zone.
172
+ * @example
173
+ * ```ts
174
+ * await Spectra.dragDrop("#task-item-1", "#column-done");
175
+ * ```
176
+ */
28
177
  dragDrop(sourceTarget: ElementTarget, destTarget: ElementTarget): SingleElementRunner;
178
+ /**
179
+ * Scrolls the page or scrolls a specific element into view.
180
+ *
181
+ * @param options - Direction, pixels, or selector to scroll.
182
+ * @example
183
+ * ```ts
184
+ * await Spectra.scroll({ direction: "down", pixels: 400 });
185
+ * await Spectra.scroll({ selector: "#footer" });
186
+ * ```
187
+ */
29
188
  scroll(options?: ScrollOptions): SingleElementRunner;
189
+ /**
190
+ * Performs a touch swipe gesture in the given direction (for mobile and touch-enabled browsers).
191
+ *
192
+ * @param options - Swipe configuration with `direction` and optional `distance`.
193
+ * @example
194
+ * ```ts
195
+ * await Spectra.swipe({ direction: "left", distance: 300 });
196
+ * ```
197
+ */
30
198
  swipe(options: SwipeOptions): SingleElementRunner;
199
+ /**
200
+ * Pauses test execution for a specified duration in milliseconds.
201
+ *
202
+ * @param durationMs - Time to wait in milliseconds.
203
+ * @example
204
+ * ```ts
205
+ * await Spectra.wait(1000);
206
+ * ```
207
+ */
31
208
  wait(durationMs: number): SingleElementRunner;
209
+ /**
210
+ * Waits until the specified element is displayed on the screen.
211
+ *
212
+ * @param target - Selector string or Page Object element property.
213
+ * @param timeoutMs - Maximum time in milliseconds to wait (default: 10000).
214
+ * @example
215
+ * ```ts
216
+ * await Spectra.waitForElement("#confirmation-dialog", 5000);
217
+ * ```
218
+ */
32
219
  waitForElement(target: ElementTarget, timeoutMs?: number): SingleElementRunner;
33
220
  }
221
+ /**
222
+ * Global singleton instance of `SpectraStatic`.
223
+ */
34
224
  export declare const Spectra: SpectraStatic;