@testspectra/cli 1.0.4 → 1.0.6

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.
@@ -0,0 +1,131 @@
1
+ import { SingleElementMatcher, MultiElementMatcher, ElementTarget } from "./types.js";
2
+
3
+ /**
4
+ * Resolves an ElementTarget (selector string, WebdriverIO.Element, or ChainablePromiseElement)
5
+ * to an actionable WebdriverIO.Element.
6
+ */
7
+ export async function resolveElement(target: ElementTarget): Promise<WebdriverIO.Element> {
8
+ if (typeof target === "string") {
9
+ const el = await $(target);
10
+ return el as unknown as WebdriverIO.Element;
11
+ }
12
+ const el = await (target as any);
13
+ return el as unknown as WebdriverIO.Element;
14
+ }
15
+
16
+ /**
17
+ * Executes a single element matcher against the WebdriverIO browser / element context.
18
+ */
19
+ export async function executeSingleMatcher(
20
+ target: ElementTarget | undefined,
21
+ matcher: SingleElementMatcher,
22
+ args: any[]
23
+ ): Promise<void> {
24
+ const isBrowserLevel =
25
+ matcher === "have.url" ||
26
+ matcher === "contain.url" ||
27
+ matcher === "have.title" ||
28
+ matcher === "contain.title";
29
+
30
+ if (isBrowserLevel) {
31
+ const expected = args[0];
32
+ switch (matcher) {
33
+ case "have.url":
34
+ await expect(browser).toHaveUrl(expected);
35
+ break;
36
+ case "contain.url":
37
+ await expect(browser).toHaveUrl(expect.stringContaining(expected));
38
+ break;
39
+ case "have.title":
40
+ await expect(browser).toHaveTitle(expected);
41
+ break;
42
+ case "contain.title":
43
+ await expect(browser).toHaveTitle(expect.stringContaining(expected));
44
+ break;
45
+ }
46
+ return;
47
+ }
48
+
49
+ if (!target) {
50
+ throw new Error(`Assertion matcher '${matcher}' requires an element target.`);
51
+ }
52
+
53
+ const el = await resolveElement(target);
54
+
55
+ switch (matcher) {
56
+ case "be.visible":
57
+ await expect(el).toBeDisplayed();
58
+ break;
59
+ case "not.be.visible":
60
+ await expect(el).not.toBeDisplayed();
61
+ break;
62
+ case "exist":
63
+ await expect(el).toExist();
64
+ break;
65
+ case "not.exist":
66
+ await expect(el).not.toExist();
67
+ break;
68
+ case "be.enabled":
69
+ await expect(el).toBeEnabled();
70
+ break;
71
+ case "be.disabled":
72
+ await expect(el).toBeDisabled();
73
+ break;
74
+ case "be.checked":
75
+ case "be.selected":
76
+ await expect(el).toBeSelected();
77
+ break;
78
+ case "have.value":
79
+ await expect(el).toHaveValue(args[0]);
80
+ break;
81
+ case "contain.value":
82
+ await expect(el).toHaveValue(expect.stringContaining(args[0]));
83
+ break;
84
+ case "have.text":
85
+ await expect(el).toHaveText(args[0]);
86
+ break;
87
+ case "contain.text":
88
+ await expect(el).toHaveText(expect.stringContaining(args[0]));
89
+ break;
90
+ case "have.class":
91
+ await expect(el).toHaveElementClass(args[0]);
92
+ break;
93
+ case "have.attr":
94
+ if (args.length >= 2) {
95
+ await expect(el).toHaveAttribute(args[0], args[1]);
96
+ } else {
97
+ await expect(el).toHaveAttribute(args[0]);
98
+ }
99
+ break;
100
+ default:
101
+ throw new Error(`Unsupported matcher: ${matcher}`);
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Executes a multi-element collection matcher against WebdriverIO $$.
107
+ */
108
+ export async function executeMultiMatcher(
109
+ selector: string,
110
+ matcher: MultiElementMatcher,
111
+ args: any[]
112
+ ): Promise<void> {
113
+ const elements = await $$(selector);
114
+
115
+ switch (matcher) {
116
+ case "have.length":
117
+ await expect(elements).toBeElementsArrayOfSize(args[0]);
118
+ break;
119
+ case "have.length.greaterThan":
120
+ await expect(elements).toBeElementsArrayOfSize({ gte: args[0] + 1 });
121
+ break;
122
+ case "be.empty":
123
+ await expect(elements).toBeElementsArrayOfSize(0);
124
+ break;
125
+ case "exist":
126
+ await expect(elements).toBeElementsArrayOfSize({ gte: 1 });
127
+ break;
128
+ default:
129
+ throw new Error(`Unsupported multi-element matcher: ${matcher}`);
130
+ }
131
+ }
@@ -0,0 +1,163 @@
1
+ import { ElementTarget } from "./types.js";
2
+ import { resolveElement } from "./matchers.js";
3
+
4
+ declare global {
5
+ namespace WebdriverIO {
6
+ interface Element {
7
+ shouldBeVisible(): Promise<Element>;
8
+ shouldNotBeVisible(): Promise<Element>;
9
+ shouldExist(): Promise<Element>;
10
+ shouldNotExist(): Promise<Element>;
11
+ shouldBeEnabled(): Promise<Element>;
12
+ shouldBeDisabled(): Promise<Element>;
13
+ shouldBeSelected(): Promise<Element>;
14
+ shouldBeChecked(): Promise<Element>;
15
+ shouldHaveValue(expectedValue: string): Promise<Element>;
16
+ shouldContainValue(expectedValue: string): Promise<Element>;
17
+ shouldHaveText(expectedText: string): Promise<Element>;
18
+ shouldContainText(expectedText: string): Promise<Element>;
19
+ shouldHaveClass(className: string): Promise<Element>;
20
+ shouldHaveAttribute(attributeName: string, expectedValue?: string): Promise<Element>;
21
+ }
22
+
23
+ interface Browser {
24
+ shouldHaveUrl(expectedUrl: string): Promise<void>;
25
+ shouldContainUrl(expectedUrl: string): Promise<void>;
26
+ shouldHaveTitle(expectedTitle: string): Promise<void>;
27
+ shouldContainTitle(expectedTitle: string): Promise<void>;
28
+ }
29
+ }
30
+
31
+ interface Promise<T> {
32
+ shouldBeVisible(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
33
+ shouldNotBeVisible(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
34
+ shouldExist(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
35
+ shouldNotExist(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
36
+ shouldBeEnabled(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
37
+ shouldBeDisabled(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
38
+ shouldBeSelected(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
39
+ shouldBeChecked(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
40
+ shouldHaveValue(this: Promise<WebdriverIO.Element>, expectedValue: string): Promise<WebdriverIO.Element>;
41
+ shouldContainValue(this: Promise<WebdriverIO.Element>, expectedValue: string): Promise<WebdriverIO.Element>;
42
+ shouldHaveText(this: Promise<WebdriverIO.Element>, expectedText: string): Promise<WebdriverIO.Element>;
43
+ shouldContainText(this: Promise<WebdriverIO.Element>, expectedText: string): Promise<WebdriverIO.Element>;
44
+ shouldHaveClass(this: Promise<WebdriverIO.Element>, className: string): Promise<WebdriverIO.Element>;
45
+ shouldHaveAttribute(this: Promise<WebdriverIO.Element>, attributeName: string, expectedValue?: string): Promise<WebdriverIO.Element>;
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Install direct callable assertion methods onto WebdriverIO browser/element and Promise prototypes.
51
+ */
52
+ export function installPrototypes() {
53
+ const elementCommands: Record<string, Function> = {
54
+ async shouldBeVisible(this: any) {
55
+ const el = await resolveElement(this);
56
+ await expect(el).toBeDisplayed();
57
+ return el;
58
+ },
59
+ async shouldNotBeVisible(this: any) {
60
+ const el = await resolveElement(this);
61
+ await expect(el).not.toBeDisplayed();
62
+ return el;
63
+ },
64
+ async shouldExist(this: any) {
65
+ const el = await resolveElement(this);
66
+ await expect(el).toExist();
67
+ return el;
68
+ },
69
+ async shouldNotExist(this: any) {
70
+ const el = await resolveElement(this);
71
+ await expect(el).not.toExist();
72
+ return el;
73
+ },
74
+ async shouldBeEnabled(this: any) {
75
+ const el = await resolveElement(this);
76
+ await expect(el).toBeEnabled();
77
+ return el;
78
+ },
79
+ async shouldBeDisabled(this: any) {
80
+ const el = await resolveElement(this);
81
+ await expect(el).toBeDisabled();
82
+ return el;
83
+ },
84
+ async shouldBeSelected(this: any) {
85
+ const el = await resolveElement(this);
86
+ await expect(el).toBeSelected();
87
+ return el;
88
+ },
89
+ async shouldBeChecked(this: any) {
90
+ const el = await resolveElement(this);
91
+ await expect(el).toBeSelected();
92
+ return el;
93
+ },
94
+ async shouldHaveValue(this: any, val: string) {
95
+ const el = await resolveElement(this);
96
+ await expect(el).toHaveValue(val);
97
+ return el;
98
+ },
99
+ async shouldContainValue(this: any, val: string) {
100
+ const el = await resolveElement(this);
101
+ await expect(el).toHaveValue(expect.stringContaining(val));
102
+ return el;
103
+ },
104
+ async shouldHaveText(this: any, text: string) {
105
+ const el = await resolveElement(this);
106
+ await expect(el).toHaveText(text);
107
+ return el;
108
+ },
109
+ async shouldContainText(this: any, text: string) {
110
+ const el = await resolveElement(this);
111
+ await expect(el).toHaveText(expect.stringContaining(text));
112
+ return el;
113
+ },
114
+ async shouldHaveClass(this: any, cls: string) {
115
+ const el = await resolveElement(this);
116
+ await expect(el).toHaveElementClass(cls);
117
+ return el;
118
+ },
119
+ async shouldHaveAttribute(this: any, name: string, val?: string) {
120
+ const el = await resolveElement(this);
121
+ if (val !== undefined) {
122
+ await expect(el).toHaveAttribute(name, val);
123
+ } else {
124
+ await expect(el).toHaveAttribute(name);
125
+ }
126
+ return el;
127
+ },
128
+ };
129
+
130
+ const browserCommands: Record<string, Function> = {
131
+ async shouldHaveUrl(url: string) {
132
+ await expect(browser).toHaveUrl(url);
133
+ },
134
+ async shouldContainUrl(url: string) {
135
+ await expect(browser).toHaveUrl(expect.stringContaining(url));
136
+ },
137
+ async shouldHaveTitle(title: string) {
138
+ await expect(browser).toHaveTitle(title);
139
+ },
140
+ async shouldContainTitle(title: string) {
141
+ await expect(browser).toHaveTitle(expect.stringContaining(title));
142
+ },
143
+ };
144
+
145
+ if (typeof browser !== "undefined" && (browser as any).addCommand) {
146
+ for (const [name, fn] of Object.entries(elementCommands)) {
147
+ (browser as any).addCommand(name, fn, true);
148
+ }
149
+ for (const [name, fn] of Object.entries(browserCommands)) {
150
+ (browser as any).addCommand(name, fn, false);
151
+ }
152
+ }
153
+
154
+ // Attach to Promise.prototype for ChainablePromiseElement unwrapping
155
+ for (const [name, fn] of Object.entries(elementCommands)) {
156
+ if (typeof (Promise.prototype as any)[name] !== "function") {
157
+ (Promise.prototype as any)[name] = async function (this: Promise<any>, ...args: any[]) {
158
+ const el = await this;
159
+ return await fn.call(el, ...args);
160
+ };
161
+ }
162
+ }
163
+ }
@@ -0,0 +1,73 @@
1
+ import { MultiElementMatcher } from "../types.js";
2
+ import { executeMultiMatcher } from "../matchers.js";
3
+ import { SingleElementRunner } from "./single.js";
4
+
5
+ type CollectionAssertion = {
6
+ matcher: MultiElementMatcher;
7
+ args: any[];
8
+ };
9
+
10
+ export class MultiElementRunner implements PromiseLike<void> {
11
+ private _selector: string;
12
+ private _assertions: CollectionAssertion[] = [];
13
+
14
+ constructor(selector: string) {
15
+ this._selector = selector;
16
+ }
17
+
18
+ // --- Collection Assertions ---
19
+
20
+ should(matcher: MultiElementMatcher, ...args: any[]): this {
21
+ this._assertions.push({ matcher, args });
22
+ return this;
23
+ }
24
+
25
+ // --- Item Navigation ---
26
+
27
+ eq(index: number): SingleElementRunner {
28
+ const indexedSelector = `(${this._selector})[${index + 1}]`;
29
+ return new SingleElementRunner(indexedSelector);
30
+ }
31
+
32
+ first(): SingleElementRunner {
33
+ return this.eq(0);
34
+ }
35
+
36
+ last(): SingleElementRunner {
37
+ const indexedSelector = `(${this._selector})[last()]`;
38
+ return new SingleElementRunner(indexedSelector);
39
+ }
40
+
41
+ // --- Iteration Callback ---
42
+
43
+ async each(
44
+ callback: (el: SingleElementRunner, index: number) => Promise<void>
45
+ ): Promise<void> {
46
+ const elements = await $$(this._selector);
47
+ const count = await (elements as any).length;
48
+ for (let i = 0; i < count; i++) {
49
+ const runner = this.eq(i);
50
+ await callback(runner, i);
51
+ }
52
+ }
53
+
54
+ // --- Execution when awaited ---
55
+
56
+ async then<TResult1 = void, TResult2 = never>(
57
+ onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null,
58
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
59
+ ): Promise<TResult1 | TResult2> {
60
+ try {
61
+ for (const assert of this._assertions) {
62
+ await executeMultiMatcher(this._selector, assert.matcher, assert.args);
63
+ }
64
+ const res = (onfulfilled ? await onfulfilled() : undefined) as TResult1;
65
+ return res;
66
+ } catch (err) {
67
+ if (onrejected) {
68
+ return onrejected(err);
69
+ }
70
+ throw err;
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,166 @@
1
+ import { SingleElementMatcher, ElementTarget } from "../types.js";
2
+ import { executeSingleMatcher, resolveElement } from "../matchers.js";
3
+
4
+ type AssertionItem = {
5
+ target?: ElementTarget;
6
+ matcher: SingleElementMatcher;
7
+ args: any[];
8
+ };
9
+
10
+ export class SingleElementRunner implements PromiseLike<void> {
11
+ private _target: ElementTarget | undefined;
12
+ private _action: (() => Promise<any>) | null = null;
13
+ private _assertions: AssertionItem[] = [];
14
+
15
+ constructor(target?: ElementTarget, action?: () => Promise<any>) {
16
+ this._target = target;
17
+ this._action = action || null;
18
+ }
19
+
20
+ // --- Chained Actions on current target ---
21
+
22
+ click(): this {
23
+ const target = this.requireTarget("click");
24
+ this._action = async () => {
25
+ const el = await resolveElement(target);
26
+ await el.click();
27
+ };
28
+ return this;
29
+ }
30
+
31
+ doubleClick(): this {
32
+ const target = this.requireTarget("doubleClick");
33
+ this._action = async () => {
34
+ const el = await resolveElement(target);
35
+ await el.doubleClick();
36
+ };
37
+ return this;
38
+ }
39
+
40
+ longPress(durationMs = 1000): this {
41
+ const target = this.requireTarget("longPress");
42
+ this._action = async () => {
43
+ const el = await resolveElement(target);
44
+ if (typeof (el as any).touchAction === "function") {
45
+ await (el as any).touchAction([
46
+ { action: "longPress", ms: durationMs },
47
+ { action: "release" },
48
+ ]);
49
+ } else {
50
+ await el.click();
51
+ }
52
+ };
53
+ return this;
54
+ }
55
+
56
+ type(value: string, options?: { clearFirst?: boolean }): this {
57
+ const target = this.requireTarget("type");
58
+ this._action = async () => {
59
+ const el = await resolveElement(target);
60
+ if (options?.clearFirst) {
61
+ await el.clearValue();
62
+ }
63
+ await el.setValue(value);
64
+ };
65
+ return this;
66
+ }
67
+
68
+ clear(): this {
69
+ const target = this.requireTarget("clear");
70
+ this._action = async () => {
71
+ const el = await resolveElement(target);
72
+ await el.clearValue();
73
+ };
74
+ return this;
75
+ }
76
+
77
+ select(value: string): this {
78
+ const target = this.requireTarget("select");
79
+ this._action = async () => {
80
+ const el = await resolveElement(target);
81
+ await el.selectByVisibleText(value);
82
+ };
83
+ return this;
84
+ }
85
+
86
+ hover(): this {
87
+ const target = this.requireTarget("hover");
88
+ this._action = async () => {
89
+ const el = await resolveElement(target);
90
+ await el.moveTo();
91
+ };
92
+ return this;
93
+ }
94
+
95
+ waitForElement(timeoutMs = 10000): this {
96
+ const target = this.requireTarget("waitForElement");
97
+ this._action = async () => {
98
+ const el = await resolveElement(target);
99
+ await el.waitForDisplayed({ timeout: timeoutMs });
100
+ };
101
+ return this;
102
+ }
103
+
104
+ // --- Fluent Assertion Matchers ---
105
+
106
+ should(matcher: SingleElementMatcher, ...args: any[]): this;
107
+ should(target: ElementTarget, matcher: SingleElementMatcher, ...args: any[]): this;
108
+ should(a: ElementTarget | SingleElementMatcher, b?: any, ...rest: any[]): this {
109
+ const isFirstArgMatcher =
110
+ typeof a === "string" &&
111
+ (a.includes(".") ||
112
+ a.includes("exist") ||
113
+ a.includes("visible") ||
114
+ a.includes("enabled") ||
115
+ a.includes("disabled") ||
116
+ a.includes("selected") ||
117
+ a.includes("checked"));
118
+
119
+ if (b !== undefined && !isFirstArgMatcher) {
120
+ // Form: should(target, matcher, ...args)
121
+ this._assertions.push({
122
+ target: a as ElementTarget,
123
+ matcher: b as SingleElementMatcher,
124
+ args: rest,
125
+ });
126
+ } else {
127
+ // Form: should(matcher, ...args) -> on current target
128
+ this._assertions.push({
129
+ target: this._target,
130
+ matcher: a as SingleElementMatcher,
131
+ args: b !== undefined ? [b, ...rest] : rest,
132
+ });
133
+ }
134
+ return this;
135
+ }
136
+
137
+ // --- Execution when awaited ---
138
+
139
+ async then<TResult1 = void, TResult2 = never>(
140
+ onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null,
141
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
142
+ ): Promise<TResult1 | TResult2> {
143
+ try {
144
+ if (this._action) {
145
+ await this._action();
146
+ }
147
+ for (const assert of this._assertions) {
148
+ await executeSingleMatcher(assert.target, assert.matcher, assert.args);
149
+ }
150
+ const res = (onfulfilled ? await onfulfilled() : undefined) as TResult1;
151
+ return res;
152
+ } catch (err) {
153
+ if (onrejected) {
154
+ return onrejected(err);
155
+ }
156
+ throw err;
157
+ }
158
+ }
159
+
160
+ private requireTarget(actionName: string): ElementTarget {
161
+ if (!this._target) {
162
+ throw new Error(`Cannot execute '${actionName}' without specifying an element target.`);
163
+ }
164
+ return this._target;
165
+ }
166
+ }