@testspectra/matchers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/matchers.d.ts +14 -0
- package/dist/matchers.js +114 -0
- package/dist/proto.d.ts +46 -0
- package/dist/proto.js +114 -0
- package/dist/runner/collection.d.ts +13 -0
- package/dist/runner/collection.js +51 -0
- package/dist/runner/single.d.ts +21 -0
- package/dist/runner/single.js +138 -0
- package/dist/spectra.d.ts +34 -0
- package/dist/spectra.js +156 -0
- package/dist/types.d.ts +32 -0
- package/dist/types.js +5 -0
- package/package.json +21 -0
- package/src/index.ts +6 -0
- package/src/matchers.ts +131 -0
- package/src/proto.ts +163 -0
- package/src/runner/collection.ts +73 -0
- package/src/runner/single.ts +166 -0
- package/src/spectra.ts +185 -0
- package/src/types.ts +113 -0
- package/tsconfig.json +17 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SingleElementMatcher, MultiElementMatcher, ElementTarget } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves an ElementTarget (selector string, WebdriverIO.Element, or ChainablePromiseElement)
|
|
4
|
+
* to an actionable WebdriverIO.Element.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveElement(target: ElementTarget): Promise<WebdriverIO.Element>;
|
|
7
|
+
/**
|
|
8
|
+
* Executes a single element matcher against the WebdriverIO browser / element context.
|
|
9
|
+
*/
|
|
10
|
+
export declare function executeSingleMatcher(target: ElementTarget | undefined, matcher: SingleElementMatcher, args: any[]): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Executes a multi-element collection matcher against WebdriverIO $$.
|
|
13
|
+
*/
|
|
14
|
+
export declare function executeMultiMatcher(selector: string, matcher: MultiElementMatcher, args: any[]): Promise<void>;
|
package/dist/matchers.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves an ElementTarget (selector string, WebdriverIO.Element, or ChainablePromiseElement)
|
|
3
|
+
* to an actionable WebdriverIO.Element.
|
|
4
|
+
*/
|
|
5
|
+
export async function resolveElement(target) {
|
|
6
|
+
if (typeof target === "string") {
|
|
7
|
+
const el = await $(target);
|
|
8
|
+
return el;
|
|
9
|
+
}
|
|
10
|
+
const el = await target;
|
|
11
|
+
return el;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Executes a single element matcher against the WebdriverIO browser / element context.
|
|
15
|
+
*/
|
|
16
|
+
export async function executeSingleMatcher(target, matcher, args) {
|
|
17
|
+
const isBrowserLevel = matcher === "have.url" ||
|
|
18
|
+
matcher === "contain.url" ||
|
|
19
|
+
matcher === "have.title" ||
|
|
20
|
+
matcher === "contain.title";
|
|
21
|
+
if (isBrowserLevel) {
|
|
22
|
+
const expected = args[0];
|
|
23
|
+
switch (matcher) {
|
|
24
|
+
case "have.url":
|
|
25
|
+
await expect(browser).toHaveUrl(expected);
|
|
26
|
+
break;
|
|
27
|
+
case "contain.url":
|
|
28
|
+
await expect(browser).toHaveUrl(expect.stringContaining(expected));
|
|
29
|
+
break;
|
|
30
|
+
case "have.title":
|
|
31
|
+
await expect(browser).toHaveTitle(expected);
|
|
32
|
+
break;
|
|
33
|
+
case "contain.title":
|
|
34
|
+
await expect(browser).toHaveTitle(expect.stringContaining(expected));
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!target) {
|
|
40
|
+
throw new Error(`Assertion matcher '${matcher}' requires an element target.`);
|
|
41
|
+
}
|
|
42
|
+
const el = await resolveElement(target);
|
|
43
|
+
switch (matcher) {
|
|
44
|
+
case "be.visible":
|
|
45
|
+
await expect(el).toBeDisplayed();
|
|
46
|
+
break;
|
|
47
|
+
case "not.be.visible":
|
|
48
|
+
await expect(el).not.toBeDisplayed();
|
|
49
|
+
break;
|
|
50
|
+
case "exist":
|
|
51
|
+
await expect(el).toExist();
|
|
52
|
+
break;
|
|
53
|
+
case "not.exist":
|
|
54
|
+
await expect(el).not.toExist();
|
|
55
|
+
break;
|
|
56
|
+
case "be.enabled":
|
|
57
|
+
await expect(el).toBeEnabled();
|
|
58
|
+
break;
|
|
59
|
+
case "be.disabled":
|
|
60
|
+
await expect(el).toBeDisabled();
|
|
61
|
+
break;
|
|
62
|
+
case "be.checked":
|
|
63
|
+
case "be.selected":
|
|
64
|
+
await expect(el).toBeSelected();
|
|
65
|
+
break;
|
|
66
|
+
case "have.value":
|
|
67
|
+
await expect(el).toHaveValue(args[0]);
|
|
68
|
+
break;
|
|
69
|
+
case "contain.value":
|
|
70
|
+
await expect(el).toHaveValue(expect.stringContaining(args[0]));
|
|
71
|
+
break;
|
|
72
|
+
case "have.text":
|
|
73
|
+
await expect(el).toHaveText(args[0]);
|
|
74
|
+
break;
|
|
75
|
+
case "contain.text":
|
|
76
|
+
await expect(el).toHaveText(expect.stringContaining(args[0]));
|
|
77
|
+
break;
|
|
78
|
+
case "have.class":
|
|
79
|
+
await expect(el).toHaveElementClass(args[0]);
|
|
80
|
+
break;
|
|
81
|
+
case "have.attr":
|
|
82
|
+
if (args.length >= 2) {
|
|
83
|
+
await expect(el).toHaveAttribute(args[0], args[1]);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
await expect(el).toHaveAttribute(args[0]);
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
throw new Error(`Unsupported matcher: ${matcher}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Executes a multi-element collection matcher against WebdriverIO $$.
|
|
95
|
+
*/
|
|
96
|
+
export async function executeMultiMatcher(selector, matcher, args) {
|
|
97
|
+
const elements = await $$(selector);
|
|
98
|
+
switch (matcher) {
|
|
99
|
+
case "have.length":
|
|
100
|
+
await expect(elements).toBeElementsArrayOfSize(args[0]);
|
|
101
|
+
break;
|
|
102
|
+
case "have.length.greaterThan":
|
|
103
|
+
await expect(elements).toBeElementsArrayOfSize({ gte: args[0] + 1 });
|
|
104
|
+
break;
|
|
105
|
+
case "be.empty":
|
|
106
|
+
await expect(elements).toBeElementsArrayOfSize(0);
|
|
107
|
+
break;
|
|
108
|
+
case "exist":
|
|
109
|
+
await expect(elements).toBeElementsArrayOfSize({ gte: 1 });
|
|
110
|
+
break;
|
|
111
|
+
default:
|
|
112
|
+
throw new Error(`Unsupported multi-element matcher: ${matcher}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
package/dist/proto.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
namespace WebdriverIO {
|
|
3
|
+
interface Element {
|
|
4
|
+
shouldBeVisible(): Promise<Element>;
|
|
5
|
+
shouldNotBeVisible(): Promise<Element>;
|
|
6
|
+
shouldExist(): Promise<Element>;
|
|
7
|
+
shouldNotExist(): Promise<Element>;
|
|
8
|
+
shouldBeEnabled(): Promise<Element>;
|
|
9
|
+
shouldBeDisabled(): Promise<Element>;
|
|
10
|
+
shouldBeSelected(): Promise<Element>;
|
|
11
|
+
shouldBeChecked(): Promise<Element>;
|
|
12
|
+
shouldHaveValue(expectedValue: string): Promise<Element>;
|
|
13
|
+
shouldContainValue(expectedValue: string): Promise<Element>;
|
|
14
|
+
shouldHaveText(expectedText: string): Promise<Element>;
|
|
15
|
+
shouldContainText(expectedText: string): Promise<Element>;
|
|
16
|
+
shouldHaveClass(className: string): Promise<Element>;
|
|
17
|
+
shouldHaveAttribute(attributeName: string, expectedValue?: string): Promise<Element>;
|
|
18
|
+
}
|
|
19
|
+
interface Browser {
|
|
20
|
+
shouldHaveUrl(expectedUrl: string): Promise<void>;
|
|
21
|
+
shouldContainUrl(expectedUrl: string): Promise<void>;
|
|
22
|
+
shouldHaveTitle(expectedTitle: string): Promise<void>;
|
|
23
|
+
shouldContainTitle(expectedTitle: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
interface Promise<T> {
|
|
27
|
+
shouldBeVisible(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
28
|
+
shouldNotBeVisible(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
29
|
+
shouldExist(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
30
|
+
shouldNotExist(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
31
|
+
shouldBeEnabled(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
32
|
+
shouldBeDisabled(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
33
|
+
shouldBeSelected(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
34
|
+
shouldBeChecked(this: Promise<WebdriverIO.Element>): Promise<WebdriverIO.Element>;
|
|
35
|
+
shouldHaveValue(this: Promise<WebdriverIO.Element>, expectedValue: string): Promise<WebdriverIO.Element>;
|
|
36
|
+
shouldContainValue(this: Promise<WebdriverIO.Element>, expectedValue: string): Promise<WebdriverIO.Element>;
|
|
37
|
+
shouldHaveText(this: Promise<WebdriverIO.Element>, expectedText: string): Promise<WebdriverIO.Element>;
|
|
38
|
+
shouldContainText(this: Promise<WebdriverIO.Element>, expectedText: string): Promise<WebdriverIO.Element>;
|
|
39
|
+
shouldHaveClass(this: Promise<WebdriverIO.Element>, className: string): Promise<WebdriverIO.Element>;
|
|
40
|
+
shouldHaveAttribute(this: Promise<WebdriverIO.Element>, attributeName: string, expectedValue?: string): Promise<WebdriverIO.Element>;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Install direct callable assertion methods onto WebdriverIO browser/element and Promise prototypes.
|
|
45
|
+
*/
|
|
46
|
+
export declare function installPrototypes(): void;
|
package/dist/proto.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { resolveElement } from "./matchers.js";
|
|
2
|
+
/**
|
|
3
|
+
* Install direct callable assertion methods onto WebdriverIO browser/element and Promise prototypes.
|
|
4
|
+
*/
|
|
5
|
+
export function installPrototypes() {
|
|
6
|
+
const elementCommands = {
|
|
7
|
+
async shouldBeVisible() {
|
|
8
|
+
const el = await resolveElement(this);
|
|
9
|
+
await expect(el).toBeDisplayed();
|
|
10
|
+
return el;
|
|
11
|
+
},
|
|
12
|
+
async shouldNotBeVisible() {
|
|
13
|
+
const el = await resolveElement(this);
|
|
14
|
+
await expect(el).not.toBeDisplayed();
|
|
15
|
+
return el;
|
|
16
|
+
},
|
|
17
|
+
async shouldExist() {
|
|
18
|
+
const el = await resolveElement(this);
|
|
19
|
+
await expect(el).toExist();
|
|
20
|
+
return el;
|
|
21
|
+
},
|
|
22
|
+
async shouldNotExist() {
|
|
23
|
+
const el = await resolveElement(this);
|
|
24
|
+
await expect(el).not.toExist();
|
|
25
|
+
return el;
|
|
26
|
+
},
|
|
27
|
+
async shouldBeEnabled() {
|
|
28
|
+
const el = await resolveElement(this);
|
|
29
|
+
await expect(el).toBeEnabled();
|
|
30
|
+
return el;
|
|
31
|
+
},
|
|
32
|
+
async shouldBeDisabled() {
|
|
33
|
+
const el = await resolveElement(this);
|
|
34
|
+
await expect(el).toBeDisabled();
|
|
35
|
+
return el;
|
|
36
|
+
},
|
|
37
|
+
async shouldBeSelected() {
|
|
38
|
+
const el = await resolveElement(this);
|
|
39
|
+
await expect(el).toBeSelected();
|
|
40
|
+
return el;
|
|
41
|
+
},
|
|
42
|
+
async shouldBeChecked() {
|
|
43
|
+
const el = await resolveElement(this);
|
|
44
|
+
await expect(el).toBeSelected();
|
|
45
|
+
return el;
|
|
46
|
+
},
|
|
47
|
+
async shouldHaveValue(val) {
|
|
48
|
+
const el = await resolveElement(this);
|
|
49
|
+
await expect(el).toHaveValue(val);
|
|
50
|
+
return el;
|
|
51
|
+
},
|
|
52
|
+
async shouldContainValue(val) {
|
|
53
|
+
const el = await resolveElement(this);
|
|
54
|
+
await expect(el).toHaveValue(expect.stringContaining(val));
|
|
55
|
+
return el;
|
|
56
|
+
},
|
|
57
|
+
async shouldHaveText(text) {
|
|
58
|
+
const el = await resolveElement(this);
|
|
59
|
+
await expect(el).toHaveText(text);
|
|
60
|
+
return el;
|
|
61
|
+
},
|
|
62
|
+
async shouldContainText(text) {
|
|
63
|
+
const el = await resolveElement(this);
|
|
64
|
+
await expect(el).toHaveText(expect.stringContaining(text));
|
|
65
|
+
return el;
|
|
66
|
+
},
|
|
67
|
+
async shouldHaveClass(cls) {
|
|
68
|
+
const el = await resolveElement(this);
|
|
69
|
+
await expect(el).toHaveElementClass(cls);
|
|
70
|
+
return el;
|
|
71
|
+
},
|
|
72
|
+
async shouldHaveAttribute(name, val) {
|
|
73
|
+
const el = await resolveElement(this);
|
|
74
|
+
if (val !== undefined) {
|
|
75
|
+
await expect(el).toHaveAttribute(name, val);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
await expect(el).toHaveAttribute(name);
|
|
79
|
+
}
|
|
80
|
+
return el;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
const browserCommands = {
|
|
84
|
+
async shouldHaveUrl(url) {
|
|
85
|
+
await expect(browser).toHaveUrl(url);
|
|
86
|
+
},
|
|
87
|
+
async shouldContainUrl(url) {
|
|
88
|
+
await expect(browser).toHaveUrl(expect.stringContaining(url));
|
|
89
|
+
},
|
|
90
|
+
async shouldHaveTitle(title) {
|
|
91
|
+
await expect(browser).toHaveTitle(title);
|
|
92
|
+
},
|
|
93
|
+
async shouldContainTitle(title) {
|
|
94
|
+
await expect(browser).toHaveTitle(expect.stringContaining(title));
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
if (typeof browser !== "undefined" && browser.addCommand) {
|
|
98
|
+
for (const [name, fn] of Object.entries(elementCommands)) {
|
|
99
|
+
browser.addCommand(name, fn, true);
|
|
100
|
+
}
|
|
101
|
+
for (const [name, fn] of Object.entries(browserCommands)) {
|
|
102
|
+
browser.addCommand(name, fn, false);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Attach to Promise.prototype for ChainablePromiseElement unwrapping
|
|
106
|
+
for (const [name, fn] of Object.entries(elementCommands)) {
|
|
107
|
+
if (typeof Promise.prototype[name] !== "function") {
|
|
108
|
+
Promise.prototype[name] = async function (...args) {
|
|
109
|
+
const el = await this;
|
|
110
|
+
return await fn.call(el, ...args);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MultiElementMatcher } from "../types.js";
|
|
2
|
+
import { SingleElementRunner } from "./single.js";
|
|
3
|
+
export declare class MultiElementRunner implements PromiseLike<void> {
|
|
4
|
+
private _selector;
|
|
5
|
+
private _assertions;
|
|
6
|
+
constructor(selector: string);
|
|
7
|
+
should(matcher: MultiElementMatcher, ...args: any[]): this;
|
|
8
|
+
eq(index: number): SingleElementRunner;
|
|
9
|
+
first(): SingleElementRunner;
|
|
10
|
+
last(): SingleElementRunner;
|
|
11
|
+
each(callback: (el: SingleElementRunner, index: number) => Promise<void>): Promise<void>;
|
|
12
|
+
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { executeMultiMatcher } from "../matchers.js";
|
|
2
|
+
import { SingleElementRunner } from "./single.js";
|
|
3
|
+
export class MultiElementRunner {
|
|
4
|
+
_selector;
|
|
5
|
+
_assertions = [];
|
|
6
|
+
constructor(selector) {
|
|
7
|
+
this._selector = selector;
|
|
8
|
+
}
|
|
9
|
+
// --- Collection Assertions ---
|
|
10
|
+
should(matcher, ...args) {
|
|
11
|
+
this._assertions.push({ matcher, args });
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
// --- Item Navigation ---
|
|
15
|
+
eq(index) {
|
|
16
|
+
const indexedSelector = `(${this._selector})[${index + 1}]`;
|
|
17
|
+
return new SingleElementRunner(indexedSelector);
|
|
18
|
+
}
|
|
19
|
+
first() {
|
|
20
|
+
return this.eq(0);
|
|
21
|
+
}
|
|
22
|
+
last() {
|
|
23
|
+
const indexedSelector = `(${this._selector})[last()]`;
|
|
24
|
+
return new SingleElementRunner(indexedSelector);
|
|
25
|
+
}
|
|
26
|
+
// --- Iteration Callback ---
|
|
27
|
+
async each(callback) {
|
|
28
|
+
const elements = await $$(this._selector);
|
|
29
|
+
const count = await elements.length;
|
|
30
|
+
for (let i = 0; i < count; i++) {
|
|
31
|
+
const runner = this.eq(i);
|
|
32
|
+
await callback(runner, i);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// --- Execution when awaited ---
|
|
36
|
+
async then(onfulfilled, onrejected) {
|
|
37
|
+
try {
|
|
38
|
+
for (const assert of this._assertions) {
|
|
39
|
+
await executeMultiMatcher(this._selector, assert.matcher, assert.args);
|
|
40
|
+
}
|
|
41
|
+
const res = (onfulfilled ? await onfulfilled() : undefined);
|
|
42
|
+
return res;
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
if (onrejected) {
|
|
46
|
+
return onrejected(err);
|
|
47
|
+
}
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SingleElementMatcher, ElementTarget } from "../types.js";
|
|
2
|
+
export declare class SingleElementRunner implements PromiseLike<void> {
|
|
3
|
+
private _target;
|
|
4
|
+
private _action;
|
|
5
|
+
private _assertions;
|
|
6
|
+
constructor(target?: ElementTarget, action?: () => Promise<any>);
|
|
7
|
+
click(): this;
|
|
8
|
+
doubleClick(): this;
|
|
9
|
+
longPress(durationMs?: number): this;
|
|
10
|
+
type(value: string, options?: {
|
|
11
|
+
clearFirst?: boolean;
|
|
12
|
+
}): this;
|
|
13
|
+
clear(): this;
|
|
14
|
+
select(value: string): this;
|
|
15
|
+
hover(): this;
|
|
16
|
+
waitForElement(timeoutMs?: number): this;
|
|
17
|
+
should(matcher: SingleElementMatcher, ...args: any[]): this;
|
|
18
|
+
should(target: ElementTarget, matcher: SingleElementMatcher, ...args: any[]): this;
|
|
19
|
+
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
20
|
+
private requireTarget;
|
|
21
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { executeSingleMatcher, resolveElement } from "../matchers.js";
|
|
2
|
+
export class SingleElementRunner {
|
|
3
|
+
_target;
|
|
4
|
+
_action = null;
|
|
5
|
+
_assertions = [];
|
|
6
|
+
constructor(target, action) {
|
|
7
|
+
this._target = target;
|
|
8
|
+
this._action = action || null;
|
|
9
|
+
}
|
|
10
|
+
// --- Chained Actions on current target ---
|
|
11
|
+
click() {
|
|
12
|
+
const target = this.requireTarget("click");
|
|
13
|
+
this._action = async () => {
|
|
14
|
+
const el = await resolveElement(target);
|
|
15
|
+
await el.click();
|
|
16
|
+
};
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
doubleClick() {
|
|
20
|
+
const target = this.requireTarget("doubleClick");
|
|
21
|
+
this._action = async () => {
|
|
22
|
+
const el = await resolveElement(target);
|
|
23
|
+
await el.doubleClick();
|
|
24
|
+
};
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
longPress(durationMs = 1000) {
|
|
28
|
+
const target = this.requireTarget("longPress");
|
|
29
|
+
this._action = async () => {
|
|
30
|
+
const el = await resolveElement(target);
|
|
31
|
+
if (typeof el.touchAction === "function") {
|
|
32
|
+
await el.touchAction([
|
|
33
|
+
{ action: "longPress", ms: durationMs },
|
|
34
|
+
{ action: "release" },
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
await el.click();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
type(value, options) {
|
|
44
|
+
const target = this.requireTarget("type");
|
|
45
|
+
this._action = async () => {
|
|
46
|
+
const el = await resolveElement(target);
|
|
47
|
+
if (options?.clearFirst) {
|
|
48
|
+
await el.clearValue();
|
|
49
|
+
}
|
|
50
|
+
await el.setValue(value);
|
|
51
|
+
};
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
clear() {
|
|
55
|
+
const target = this.requireTarget("clear");
|
|
56
|
+
this._action = async () => {
|
|
57
|
+
const el = await resolveElement(target);
|
|
58
|
+
await el.clearValue();
|
|
59
|
+
};
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
select(value) {
|
|
63
|
+
const target = this.requireTarget("select");
|
|
64
|
+
this._action = async () => {
|
|
65
|
+
const el = await resolveElement(target);
|
|
66
|
+
await el.selectByVisibleText(value);
|
|
67
|
+
};
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
hover() {
|
|
71
|
+
const target = this.requireTarget("hover");
|
|
72
|
+
this._action = async () => {
|
|
73
|
+
const el = await resolveElement(target);
|
|
74
|
+
await el.moveTo();
|
|
75
|
+
};
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
waitForElement(timeoutMs = 10000) {
|
|
79
|
+
const target = this.requireTarget("waitForElement");
|
|
80
|
+
this._action = async () => {
|
|
81
|
+
const el = await resolveElement(target);
|
|
82
|
+
await el.waitForDisplayed({ timeout: timeoutMs });
|
|
83
|
+
};
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
should(a, b, ...rest) {
|
|
87
|
+
const isFirstArgMatcher = typeof a === "string" &&
|
|
88
|
+
(a.includes(".") ||
|
|
89
|
+
a.includes("exist") ||
|
|
90
|
+
a.includes("visible") ||
|
|
91
|
+
a.includes("enabled") ||
|
|
92
|
+
a.includes("disabled") ||
|
|
93
|
+
a.includes("selected") ||
|
|
94
|
+
a.includes("checked"));
|
|
95
|
+
if (b !== undefined && !isFirstArgMatcher) {
|
|
96
|
+
// Form: should(target, matcher, ...args)
|
|
97
|
+
this._assertions.push({
|
|
98
|
+
target: a,
|
|
99
|
+
matcher: b,
|
|
100
|
+
args: rest,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// Form: should(matcher, ...args) -> on current target
|
|
105
|
+
this._assertions.push({
|
|
106
|
+
target: this._target,
|
|
107
|
+
matcher: a,
|
|
108
|
+
args: b !== undefined ? [b, ...rest] : rest,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
// --- Execution when awaited ---
|
|
114
|
+
async then(onfulfilled, onrejected) {
|
|
115
|
+
try {
|
|
116
|
+
if (this._action) {
|
|
117
|
+
await this._action();
|
|
118
|
+
}
|
|
119
|
+
for (const assert of this._assertions) {
|
|
120
|
+
await executeSingleMatcher(assert.target, assert.matcher, assert.args);
|
|
121
|
+
}
|
|
122
|
+
const res = (onfulfilled ? await onfulfilled() : undefined);
|
|
123
|
+
return res;
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
if (onrejected) {
|
|
127
|
+
return onrejected(err);
|
|
128
|
+
}
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
requireTarget(actionName) {
|
|
133
|
+
if (!this._target) {
|
|
134
|
+
throw new Error(`Cannot execute '${actionName}' without specifying an element target.`);
|
|
135
|
+
}
|
|
136
|
+
return this._target;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ClickOptions, ElementTarget, KeyOption, LongPressOptions, ScrollOptions, SwipeOptions, TypeOptions } from "./types.js";
|
|
2
|
+
import { SingleElementRunner } from "./runner/single.js";
|
|
3
|
+
import { MultiElementRunner } from "./runner/collection.js";
|
|
4
|
+
/**
|
|
5
|
+
* Main Spectra testing API orchestrator.
|
|
6
|
+
* Combines direct actions, single element queries (get), and collections (getAll).
|
|
7
|
+
*/
|
|
8
|
+
export declare class SpectraStatic {
|
|
9
|
+
/**
|
|
10
|
+
* Targets a single element by selector or Page Object element.
|
|
11
|
+
*/
|
|
12
|
+
get(target: ElementTarget): SingleElementRunner;
|
|
13
|
+
/**
|
|
14
|
+
* Targets multiple elements / collections by selector.
|
|
15
|
+
*/
|
|
16
|
+
getAll(selector: string): MultiElementRunner;
|
|
17
|
+
navigate(url: string): SingleElementRunner;
|
|
18
|
+
back(): SingleElementRunner;
|
|
19
|
+
refresh(): SingleElementRunner;
|
|
20
|
+
click(target: ElementTarget, textOrOptions?: string | ClickOptions): SingleElementRunner;
|
|
21
|
+
doubleClick(target: ElementTarget, textOrOptions?: string | ClickOptions): SingleElementRunner;
|
|
22
|
+
longPress(target: ElementTarget, options?: LongPressOptions | number): SingleElementRunner;
|
|
23
|
+
type(target: ElementTarget, value: string, options?: TypeOptions): SingleElementRunner;
|
|
24
|
+
clear(target: ElementTarget): SingleElementRunner;
|
|
25
|
+
select(target: ElementTarget, value: string): SingleElementRunner;
|
|
26
|
+
hover(target: ElementTarget): SingleElementRunner;
|
|
27
|
+
pressKey(key: KeyOption | string): SingleElementRunner;
|
|
28
|
+
dragDrop(sourceTarget: ElementTarget, destTarget: ElementTarget): SingleElementRunner;
|
|
29
|
+
scroll(options?: ScrollOptions): SingleElementRunner;
|
|
30
|
+
swipe(options: SwipeOptions): SingleElementRunner;
|
|
31
|
+
wait(durationMs: number): SingleElementRunner;
|
|
32
|
+
waitForElement(target: ElementTarget, timeoutMs?: number): SingleElementRunner;
|
|
33
|
+
}
|
|
34
|
+
export declare const Spectra: SpectraStatic;
|