-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpect.ts
More file actions
96 lines (74 loc) · 2.45 KB
/
expect.ts
File metadata and controls
96 lines (74 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Expect/Assert functionality
*/
import { SentienceBrowser } from './browser';
import { Element, QuerySelector } from './types';
import { waitFor } from './wait';
import { query } from './query';
import { snapshot } from './snapshot';
export class Expectation {
constructor(
private browser: SentienceBrowser,
private selector: QuerySelector
) {}
async toBeVisible(timeout: number = 10000): Promise<Element> {
const result = await waitFor(this.browser, this.selector, timeout);
if (!result.found) {
throw new Error(
`Element not found: ${this.selector} (timeout: ${timeout}ms)`
);
}
const element = result.element!;
if (!element.in_viewport) {
throw new Error(
`Element found but not visible in viewport: ${this.selector}`
);
}
return element;
}
async toExist(timeout: number = 10000): Promise<Element> {
const result = await waitFor(this.browser, this.selector, timeout);
if (!result.found) {
throw new Error(
`Element does not exist: ${this.selector} (timeout: ${timeout}ms)`
);
}
return result.element!;
}
async toHaveText(expectedText: string, timeout: number = 10000): Promise<Element> {
const result = await waitFor(this.browser, this.selector, timeout);
if (!result.found) {
throw new Error(
`Element not found: ${this.selector} (timeout: ${timeout}ms)`
);
}
const element = result.element!;
if (!element.text || !element.text.includes(expectedText)) {
throw new Error(
`Element text mismatch. Expected '${expectedText}', got '${element.text}'`
);
}
return element;
}
async toHaveCount(expectedCount: number, timeout: number = 10000): Promise<void> {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const snap = await snapshot(this.browser);
const matches = query(snap, this.selector);
if (matches.length === expectedCount) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 250));
}
// Final check
const snap = await snapshot(this.browser);
const matches = query(snap, this.selector);
const actualCount = matches.length;
throw new Error(
`Element count mismatch. Expected ${expectedCount}, got ${actualCount}`
);
}
}
export function expect(browser: SentienceBrowser, selector: QuerySelector): Expectation {
return new Expectation(browser, selector);
}