-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest.js
132 lines (108 loc) · 3.95 KB
/
test.js
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
122
123
124
125
126
127
128
129
130
131
132
const {_electron: electron} = require('playwright');
const libnut = require("../..");
const {POS_X, POS_Y, WIDTH, HEIGTH, TITLE} = require("./constants");
const sleep = async (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
let app;
let page;
let windowHandle;
const APP_TIMEOUT = 10000;
beforeEach(async () => {
app = await electron.launch({args: ['main.js']});
page = await app.firstWindow({timeout: APP_TIMEOUT});
windowHandle = await app.browserWindow(page);
await page.waitForLoadState("domcontentloaded");
await windowHandle.evaluate((win) => {
win.minimize();
win.restore();
win.focus();
});
});
describe("getWindows", () => {
it("should list our started application window", () => {
// GIVEN
// WHEN
const windowNames = libnut.getWindows().map(handle => libnut.getWindowTitle(handle));
// THEN
expect(windowNames).toContain(TITLE);
});
});
describe("getActiveWindow", () => {
it("should return our started application window", () => {
// GIVEN
// WHEN
const activeWindowHandle = libnut.getActiveWindow();
const activeWindowName = libnut.getWindowTitle(activeWindowHandle);
// THEN
expect(activeWindowName).toBe(TITLE);
});
it("should determine correct coordinates for our application", () => {
// GIVEN
// WHEN
const activeWindowHandle = libnut.getActiveWindow();
const activeWindowRect = libnut.getWindowRect(activeWindowHandle);
// THEN
expect(activeWindowRect.x).toBe(POS_X);
expect(activeWindowRect.y).toBe(POS_Y);
expect(activeWindowRect.width).toBe(WIDTH);
expect(activeWindowRect.height).toBe(HEIGTH);
});
it("should determine correct coordinates for our application after moving the window", async () => {
// GIVEN
const xPosition = 250;
const yPosition = 300;
// WHEN
const activeWindowHandle = libnut.getActiveWindow();
libnut.moveWindow(activeWindowHandle, {x: xPosition, y: yPosition});
await sleep(100);
const activeWindowRect = libnut.getWindowRect(activeWindowHandle);
// THEN
expect(activeWindowRect.x).toBe(xPosition);
expect(activeWindowRect.y).toBe(yPosition);
});
it("should determine correct window size for our application after resizing the window", async () => {
// GIVEN
const newWidth = 800;
const newHeight = 650;
// WHEN
const activeWindowHandle = libnut.getActiveWindow();
libnut.resizeWindow(activeWindowHandle, {width: newWidth, height: newHeight});
await sleep(100);
const activeWindowRect = libnut.getWindowRect(activeWindowHandle);
// THEN
expect(activeWindowRect.width).toBe(newWidth);
expect(activeWindowRect.height).toBe(newHeight);
});
});
describe("focusWindow", () => {
it("should properly focus the correct window", async () => {
// GIVEN
const openWindowHandle = libnut.getActiveWindow();
// WHEN
const secondApp = await electron.launch({args: ['second.js']});
const secondPage = await secondApp.firstWindow({timeout: APP_TIMEOUT});
const handle = await secondApp.browserWindow(secondPage);
await secondPage.waitForLoadState("domcontentloaded");
await handle.evaluate((win) => {
win.minimize();
win.restore();
win.focus();
});
const result = libnut.focusWindow(openWindowHandle);
// THEN
await sleep(1500);
const activeWindowHandle = libnut.getActiveWindow();
const activeWindowName = libnut.getWindowTitle(activeWindowHandle);
expect(activeWindowName).toBe(TITLE);
expect(result).toBeTruthy();
if (secondApp) {
await secondApp.close();
}
});
});
afterEach(async () => {
if (app) {
await app.close();
}
});