Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/test/waitForElement.test.ts
1029 views
1
import { Helpers } from '@secret-agent/testing';
2
import { ITestKoaServer } from '@secret-agent/testing/helpers';
3
import ISessionCreateOptions from '@secret-agent/interfaces/ISessionCreateOptions';
4
import Core, { Tab } from '../index';
5
import ConnectionToClient from '../server/ConnectionToClient';
6
import Session from '../lib/Session';
7
8
let koaServer: ITestKoaServer;
9
let connection: ConnectionToClient;
10
beforeAll(async () => {
11
connection = Core.addConnection();
12
await connection.connect();
13
Helpers.onClose(() => connection.disconnect(), true);
14
koaServer = await Helpers.runKoaServer();
15
});
16
afterAll(Helpers.afterAll);
17
afterEach(Helpers.afterEach);
18
19
describe('basic waitForElement tests', () => {
20
it('waits for an element', async () => {
21
koaServer.get('/waitForElementTest1', ctx => {
22
ctx.body = `<body>
23
<script>
24
setTimeout(function() {
25
const elem = document.createElement('A');
26
elem.setAttribute('href', '/waitForElementTest2');
27
document.body.append(elem)
28
}, 500);
29
</script>
30
</body>`;
31
});
32
33
const { tab } = await createSession();
34
await tab.goto(`${koaServer.baseUrl}/waitForElementTest1`);
35
36
await expect(tab.waitForElement(['document', ['querySelector', 'a']])).resolves.toBe(true);
37
});
38
39
it('times out waiting for an element', async () => {
40
koaServer.get('/waitForElementTest2', ctx => {
41
ctx.body = `<body><a>Nothing really here</a></body>`;
42
});
43
const { tab } = await createSession();
44
await tab.goto(`${koaServer.baseUrl}/waitForElementTest2`);
45
await tab.waitForLoad('DomContentLoaded');
46
47
await expect(
48
tab.waitForElement(['document', ['querySelector', 'a#notthere']], { timeoutMs: 500 }),
49
).rejects.toThrowError(/Timeout waiting for element to be visible/);
50
});
51
52
it('will wait for an element to be visible', async () => {
53
koaServer.get('/waitForElementTest3', ctx => {
54
ctx.body = `<body>
55
<a id="waitToShow" href="/anywhere" style="display: none">Link</a>
56
<script>
57
setTimeout(function() {
58
document.querySelector('a#waitToShow').style.display = 'block';
59
}, 150);
60
</script>
61
</body>`;
62
});
63
const { tab } = await createSession();
64
await tab.goto(`${koaServer.baseUrl}/waitForElementTest3`);
65
66
await expect(
67
tab.waitForElement(['document', ['querySelector', 'a#waitToShow']], {
68
waitForVisible: true,
69
}),
70
).resolves.toBe(true);
71
});
72
73
it('will yield an error for a bad querySelector', async () => {
74
koaServer.get('/waitForElementBadQs', ctx => {
75
ctx.body = `<body><div>Middle</div></body>`;
76
});
77
const { tab } = await createSession();
78
await tab.goto(`${koaServer.baseUrl}/waitForElementBadQs`);
79
80
await expect(
81
tab.waitForElement(['document', ['querySelector', 'button-title="test"']], {
82
waitForVisible: true,
83
}),
84
).rejects.toThrowError('valid selector');
85
});
86
87
it('will wait for a valid path to exist', async () => {
88
koaServer.get('/waitForElementValidPath', ctx => {
89
ctx.body = `<body><ul>
90
<li>1</li>
91
<li>2</li>
92
</ul>
93
<script>
94
setTimeout(function() {
95
const child = document.createElement('li');
96
child.innerText='3';
97
document.querySelector('ul').append(child);
98
}, 150);
99
</script>
100
101
</body>`;
102
});
103
const { tab } = await createSession();
104
await tab.goto(`${koaServer.baseUrl}/waitForElementValidPath`);
105
106
await expect(
107
tab.waitForElement(['document', ['querySelector', 'ul'], 'children', ['item', 2]], {
108
waitForVisible: true,
109
timeoutMs: 5e3,
110
}),
111
).resolves.toBe(true);
112
});
113
114
it('will find the correct center of an element', async () => {
115
koaServer.get('/waitForElementCenter', ctx => {
116
ctx.body = `<body>
117
<div id="wrapper" style="padding: 10px;">
118
<div id="elem1" style="width: 50px; height: 25px; margin: 15px">I am 1</div>
119
<div id="elem2" style="width: 50px; height: 25px; margin: 15px">I am 2</div>
120
</div>
121
</body>`;
122
});
123
const { tab } = await createSession();
124
await tab.goto(`${koaServer.baseUrl}/waitForElementCenter`);
125
126
await expect(
127
tab.waitForElement(['document', ['querySelector', '#wrapper']], {
128
waitForVisible: true,
129
}),
130
).resolves.toBe(true);
131
132
await expect(
133
tab.waitForElement(['document', ['querySelector', '#elem1']], {
134
waitForVisible: true,
135
}),
136
).resolves.toBe(true);
137
138
await expect(
139
tab.waitForElement(['document', ['querySelector', '#elem2']], {
140
waitForVisible: true,
141
}),
142
).resolves.toBe(true);
143
});
144
145
it('will wait for an element above the fold to be on screen', async () => {
146
koaServer.get('/waitForElementTestOnScreen', ctx => {
147
ctx.body = `<body>
148
<a id="waitToShow" href="/anywhere" style="display:block; position: absolute; top: -100px">Link</a>
149
<script>
150
setTimeout(function() {
151
document.querySelector('a#waitToShow').style.top = 0;
152
}, 150);
153
</script>
154
</body>`;
155
});
156
const { tab } = await createSession();
157
await tab.goto(`${koaServer.baseUrl}/waitForElementTestOnScreen`);
158
159
await expect(
160
tab.waitForElement(['document', ['querySelector', 'a#waitToShow']], {
161
waitForVisible: true,
162
}),
163
).resolves.toBe(true);
164
});
165
166
it('will wait until an element off the bottom of the page', async () => {
167
koaServer.get('/waitForElementTestOffBottom', ctx => {
168
ctx.body = `<body>
169
<div style="height: 2000px; position: relative">
170
<a id="waitToShow" href="/anywhere" style="position: relative; top: 1990px">Link</a>
171
</div>
172
<script>
173
setTimeout(function() {
174
document.querySelector('a#waitToShow').scrollIntoView({ behavior: 'smooth'})
175
}, 150);
176
</script>
177
</body>`;
178
});
179
const { tab } = await createSession();
180
await tab.goto(`${koaServer.baseUrl}/waitForElementTestOffBottom`);
181
182
await expect(
183
tab.waitForElement(['document', ['querySelector', 'a#waitToShow']], {
184
waitForVisible: true,
185
}),
186
).resolves.toBe(true);
187
});
188
});
189
190
async function createSession(
191
options?: ISessionCreateOptions,
192
): Promise<{ session: Session; tab: Tab }> {
193
const meta = await connection.createSession(options);
194
const tab = Session.getTab(meta);
195
Helpers.needsClosing.push(tab.session);
196
return { session: tab.session, tab };
197
}
198
199