@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/spectra.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { SingleElementRunner } from "./runner/single.js";
|
|
2
|
+
import { MultiElementRunner } from "./runner/collection.js";
|
|
3
|
+
import { resolveElement } from "./matchers.js";
|
|
4
|
+
/**
|
|
5
|
+
* Main Spectra testing API orchestrator.
|
|
6
|
+
* Combines direct actions, single element queries (get), and collections (getAll).
|
|
7
|
+
*/
|
|
8
|
+
export class SpectraStatic {
|
|
9
|
+
/**
|
|
10
|
+
* Targets a single element by selector or Page Object element.
|
|
11
|
+
*/
|
|
12
|
+
get(target) {
|
|
13
|
+
return new SingleElementRunner(target);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Targets multiple elements / collections by selector.
|
|
17
|
+
*/
|
|
18
|
+
getAll(selector) {
|
|
19
|
+
return new MultiElementRunner(selector);
|
|
20
|
+
}
|
|
21
|
+
// --- Browser & Navigation Actions ---
|
|
22
|
+
navigate(url) {
|
|
23
|
+
return new SingleElementRunner(undefined, async () => {
|
|
24
|
+
await browser.url(url);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
back() {
|
|
28
|
+
return new SingleElementRunner(undefined, async () => {
|
|
29
|
+
await browser.back();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
refresh() {
|
|
33
|
+
return new SingleElementRunner(undefined, async () => {
|
|
34
|
+
await browser.refresh();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
// --- Direct Interaction Actions ---
|
|
38
|
+
click(target, textOrOptions) {
|
|
39
|
+
return new SingleElementRunner(target, async () => {
|
|
40
|
+
const el = await resolveElement(target);
|
|
41
|
+
await el.click();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
doubleClick(target, textOrOptions) {
|
|
45
|
+
return new SingleElementRunner(target, async () => {
|
|
46
|
+
const el = await resolveElement(target);
|
|
47
|
+
await el.doubleClick();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
longPress(target, options) {
|
|
51
|
+
const duration = typeof options === "number" ? options : options?.duration || 1000;
|
|
52
|
+
return new SingleElementRunner(target, async () => {
|
|
53
|
+
const el = await resolveElement(target);
|
|
54
|
+
if (typeof el.touchAction === "function") {
|
|
55
|
+
await el.touchAction([
|
|
56
|
+
{ action: "longPress", ms: duration },
|
|
57
|
+
{ action: "release" },
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
await el.click();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
type(target, value, options) {
|
|
66
|
+
return new SingleElementRunner(target, async () => {
|
|
67
|
+
const el = await resolveElement(target);
|
|
68
|
+
if (options?.clearFirst) {
|
|
69
|
+
await el.clearValue();
|
|
70
|
+
}
|
|
71
|
+
await el.setValue(value);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
clear(target) {
|
|
75
|
+
return new SingleElementRunner(target, async () => {
|
|
76
|
+
const el = await resolveElement(target);
|
|
77
|
+
await el.clearValue();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
select(target, value) {
|
|
81
|
+
return new SingleElementRunner(target, async () => {
|
|
82
|
+
const el = await resolveElement(target);
|
|
83
|
+
await el.selectByVisibleText(value);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
hover(target) {
|
|
87
|
+
return new SingleElementRunner(target, async () => {
|
|
88
|
+
const el = await resolveElement(target);
|
|
89
|
+
await el.moveTo();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
pressKey(key) {
|
|
93
|
+
return new SingleElementRunner(undefined, async () => {
|
|
94
|
+
await browser.keys(key);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
dragDrop(sourceTarget, destTarget) {
|
|
98
|
+
return new SingleElementRunner(sourceTarget, async () => {
|
|
99
|
+
const source = await resolveElement(sourceTarget);
|
|
100
|
+
const target = await resolveElement(destTarget);
|
|
101
|
+
await source.dragAndDrop(target);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
// --- Gesture Actions ---
|
|
105
|
+
scroll(options) {
|
|
106
|
+
return new SingleElementRunner(options?.selector, async () => {
|
|
107
|
+
if (options?.selector) {
|
|
108
|
+
const el = await resolveElement(options.selector);
|
|
109
|
+
await el.scrollIntoView();
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
await browser.execute((direction, pixels) => {
|
|
113
|
+
const delta = pixels || 500;
|
|
114
|
+
window.scrollBy({
|
|
115
|
+
top: direction === "up" ? -delta : direction === "down" ? delta : 0,
|
|
116
|
+
left: direction === "left" ? -delta : direction === "right" ? delta : 0,
|
|
117
|
+
behavior: "smooth",
|
|
118
|
+
});
|
|
119
|
+
}, options?.direction || "down", options?.pixels || 500);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
swipe(options) {
|
|
124
|
+
return new SingleElementRunner(options.selector, async () => {
|
|
125
|
+
if (typeof browser.touchAction === "function") {
|
|
126
|
+
const distance = options.distance || 300;
|
|
127
|
+
await browser.touchAction([
|
|
128
|
+
{ action: "press", x: 200, y: 500 },
|
|
129
|
+
{
|
|
130
|
+
action: "moveTo",
|
|
131
|
+
x: options.direction === "right" ? 200 + distance : options.direction === "left" ? 200 - distance : 200,
|
|
132
|
+
y: options.direction === "down" ? 500 + distance : options.direction === "up" ? 500 - distance : 500,
|
|
133
|
+
},
|
|
134
|
+
{ action: "release" },
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// Fallback to web scroll
|
|
139
|
+
await this.scroll({ direction: options.direction, pixels: options.distance });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
// --- Timing Actions ---
|
|
144
|
+
wait(durationMs) {
|
|
145
|
+
return new SingleElementRunner(undefined, async () => {
|
|
146
|
+
await browser.pause(durationMs);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
waitForElement(target, timeoutMs = 10000) {
|
|
150
|
+
return new SingleElementRunner(target, async () => {
|
|
151
|
+
const el = await resolveElement(target);
|
|
152
|
+
await el.waitForDisplayed({ timeout: timeoutMs });
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export const Spectra = new SpectraStatic();
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Canonical Action and Assertion Types
|
|
3
|
+
* Matches backend/src/models/test_step.rs
|
|
4
|
+
*/
|
|
5
|
+
export type ActionKey = "navigate" | "click" | "type" | "clear" | "select" | "scroll" | "swipe" | "wait" | "waitForElement" | "pressKey" | "longPress" | "doubleClick" | "hover" | "dragDrop" | "back" | "refresh";
|
|
6
|
+
export type AssertionKey = "elementDisplayed" | "elementNotDisplayed" | "elementExists" | "elementEnabled" | "elementDisabled" | "textContains" | "textEquals" | "textNotContains" | "urlContains" | "urlEquals" | "valueEquals" | "valueContains" | "attributeEquals" | "attributeContains" | "hasClass" | "hasAttribute" | "isSelected" | "elementCount" | "elementCountGreaterThan" | "pageLoaded" | "noErrors";
|
|
7
|
+
export type KeyOption = "Enter" | "Tab" | "Escape" | "Backspace" | "Delete" | "ArrowUp" | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "Space";
|
|
8
|
+
export type Direction = "up" | "down" | "left" | "right";
|
|
9
|
+
export type ElementTarget = string | WebdriverIO.Element | ChainablePromiseElement;
|
|
10
|
+
export interface ScrollOptions {
|
|
11
|
+
direction?: Direction;
|
|
12
|
+
pixels?: number;
|
|
13
|
+
selector?: ElementTarget;
|
|
14
|
+
}
|
|
15
|
+
export interface SwipeOptions {
|
|
16
|
+
direction: Direction;
|
|
17
|
+
distance?: number;
|
|
18
|
+
selector?: ElementTarget;
|
|
19
|
+
}
|
|
20
|
+
export interface LongPressOptions {
|
|
21
|
+
text?: string;
|
|
22
|
+
duration?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ClickOptions {
|
|
25
|
+
text?: string;
|
|
26
|
+
clickType?: "single" | "double";
|
|
27
|
+
}
|
|
28
|
+
export interface TypeOptions {
|
|
29
|
+
clearFirst?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export type SingleElementMatcher = "be.visible" | "not.be.visible" | "exist" | "not.exist" | "be.enabled" | "be.disabled" | "be.checked" | "be.selected" | "have.value" | "contain.value" | "have.text" | "contain.text" | "have.class" | "have.attr" | "have.url" | "contain.url" | "have.title" | "contain.title";
|
|
32
|
+
export type MultiElementMatcher = "have.length" | "have.length.greaterThan" | "be.empty" | "exist";
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@testspectra/matchers",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cross-platform action commands and direct callable assertion matchers for TestSpectra",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"watch": "tsc -w",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/node": "^20.14.0",
|
|
14
|
+
"@wdio/globals": "^9.2.8",
|
|
15
|
+
"@wdio/mocha-framework": "^9.2.8",
|
|
16
|
+
"webdriverio": "^9.2.8",
|
|
17
|
+
"typescript": "^5.4.5"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"license": "MIT"
|
|
21
|
+
}
|
package/src/index.ts
ADDED
package/src/matchers.ts
ADDED
|
@@ -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
|
+
}
|
package/src/proto.ts
ADDED
|
@@ -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
|
+
}
|