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
|
import { Locator, Page, _electron as electron, ElectronApplication } from 'playwright';
export interface StartAppResponse {
app: ElectronApplication;
page: Page;
util: TestUtils;
}
export interface TestUtils {
getByTestId: (id: string) => Locator;
currentRoute: () => Promise<void>;
nextRoute: () => Promise<string>;
}
interface History {
entries: Array<{ pathname: string }>;
index: number;
}
export const startApp = async (
options: Parameters<typeof electron.launch>[0],
): Promise<StartAppResponse> => {
process.env.CI = 'e2e';
const app = await electron.launch(options);
await app.evaluate(({ webContents }) => {
return new Promise((resolve) => {
webContents.getAllWebContents()[0].on('did-finish-load', resolve);
});
});
const page = await app.firstWindow();
page.on('pageerror', (error) => {
console.log(error);
});
page.on('console', (msg) => {
console.log(msg.text());
});
const util: TestUtils = {
getByTestId: (id: string) => page.locator(`data-test-id=${id}`),
currentRoute: currentRouteFactory(app),
nextRoute: nextRouteFactory(app),
};
return { app, page, util };
};
export const currentRouteFactory = (app: ElectronApplication) => {
return async () => {
return await app.evaluate(({ webContents }) => {
return webContents.getAllWebContents()[0].executeJavaScript('window.e2e.location');
});
};
}
export const nextRouteFactory = (app: ElectronApplication) => {
return async () => {
const nextRoute: string = await app.evaluate(({ ipcMain }) => {
return new Promise((resolve) => {
ipcMain.once('navigation-setHistory', (_event, history: History) => {
resolve(history.entries[history.index].pathname);
});
});
});
// TODO: Disable view transitions and shorten timeout or remove completely.
await new Promise((resolve) => setTimeout(resolve, 1000));
return nextRoute;
};
};
const getStyleProperty = (locator: Locator, property: string) => {
return locator.evaluate(
(el, { property }) => {
return window.getComputedStyle(el).getPropertyValue(property);
},
{ property },
);
};
export const getColor = (locator: Locator) => {
return getStyleProperty(locator, 'color');
};
export const getBackgroundColor = (locator: Locator) => {
return getStyleProperty(locator, 'background-color');
};
|