-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
121 lines (116 loc) · 3.51 KB
/
utils.ts
File metadata and controls
121 lines (116 loc) · 3.51 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { expect, type Locator, type Page } from "@playwright/test";
/**
* Get the last std output of the given type
* If expectContent is provided, it will retry to get the last std output until it matches the expected content
*/
export async function getLastStd(
page: Page,
type: "stdin" | "stdout" | "stderr",
{
expectContent,
}: {
expectContent?: string;
} = {},
) {
const locator = await page.locator(`[data-stdtype='${type}']`).last();
if (expectContent) {
const text = await locator.textContent();
if (text?.includes(expectContent)) {
return locator;
}
// if no match, do a hard expect that will fail the test with a clear error message
// Sorry you landed here, you will most likely have to add some `sleep()` in your code 🥲
await expect(locator).toHaveText(expectContent);
}
return locator;
}
/**
* Get the last std output of the given type after the given locator
* This is useful to get the last std output after a command has been submitted
* This ensures you don't have false positives when checking the last std output
*/
export async function getLastStdAfter(
page: Page,
type: "stdin" | "stdout" | "stderr",
stdLocator: Locator,
) {
const stdinIndex = await stdLocator.getAttribute("data-std-index");
return await page
.locator(`[data-std-index='${stdinIndex}'] ~ [data-stdtype='${type}']`)
.last();
}
export async function sleep(ms?: number): Promise<void> {
const DEFAULT_DELAY = 200; // taking into account the default delay necessary in the CI
return new Promise((resolve) => setTimeout(resolve, ms ?? DEFAULT_DELAY));
}
/**
* Fill the input with the command and submit it
* Pass the expected stdin, stdout and stderr to check the results
*/
export async function fillAndSubmitCommand(
page: Page,
command: string,
{
expectStdin,
expectStdout,
expectStderr,
afterSubmit,
}: {
expectStdin?: string;
expectStdout?: string;
expectStderr?: string;
afterSubmit?: () => Promise<void>;
} = {},
) {
const expectedStdin = expectStdin ?? command;
const input = await page.getByPlaceholder("Type a command...");
await input.fill(command);
await input.press("Enter");
if (afterSubmit) {
await afterSubmit();
}
const stdin = await getLastStd(page, "stdin", {
expectContent: expectedStdin,
});
if (expectStdout) {
const stdout = await getLastStdAfter(page, "stdout", stdin);
await expect(stdout).toHaveText(expectStdout);
}
if (expectStderr) {
const stderr = await getLastStdAfter(page, "stderr", stdin);
await expect(stderr).toHaveText(expectStderr);
}
}
/**
* Click the wand button and check the results
* Pass the expected stdin, stdout and stderr to check the results
*/
export async function clickWandButton(
page: Page,
command: string,
{
expectStdin,
expectStdout,
expectStderr,
}: {
expectStdin?: string;
expectStdout?: string;
expectStderr?: string;
} = {},
) {
const expectedStdin = expectStdin ?? command;
await page.getByTitle("Run example command").click({ force: true });
const input = await page.getByPlaceholder("Type a command...");
await expect(input).toHaveValue(expectedStdin);
const stdin = await getLastStd(page, "stdin", {
expectContent: expectedStdin,
});
if (expectStdout) {
const stdout = await getLastStdAfter(page, "stdout", stdin);
await expect(stdout).toHaveText(expectStdout);
}
if (expectStderr) {
const stderr = await getLastStd(page, "stderr");
await expect(stderr).toHaveText(expectStderr);
}
}