Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/full-client/test/resources.test.ts
1028 views
1
import { Helpers } from '@secret-agent/testing';
2
import { ITestKoaServer } from '@secret-agent/testing/helpers';
3
import { Handler } from '../index';
4
5
let koaServer: ITestKoaServer;
6
let handler: Handler;
7
beforeAll(async () => {
8
handler = new Handler({ maxConcurrency: 2 });
9
Helpers.onClose(() => handler.close(), true);
10
koaServer = await Helpers.runKoaServer();
11
koaServer.get('/test', ctx => {
12
ctx.body = `<html>
13
<body>
14
<a onclick="clicker()" href="#nothing">Click me</a>
15
</body>
16
<script>
17
let counter = 0;
18
function clicker() {
19
fetch('/ajax?counter=' + (counter++) )
20
}
21
</script>
22
</html>`;
23
});
24
koaServer.get('/ajax', ctx => {
25
ctx.body = {
26
hi: 'there',
27
};
28
});
29
});
30
afterAll(Helpers.afterAll);
31
afterEach(Helpers.afterEach);
32
33
describe('basic resource tests', () => {
34
it('waits for a resource', async () => {
35
const exampleUrl = `${koaServer.baseUrl}/test`;
36
const agent = await handler.createAgent();
37
38
await agent.goto(exampleUrl);
39
const elem = agent.document.querySelector('a');
40
await agent.click(elem);
41
42
const resources = await agent.waitForResource({ type: 'Fetch' });
43
expect(resources).toHaveLength(1);
44
await agent.close();
45
});
46
47
it('waits for a resource loaded since a previous command id', async () => {
48
const exampleUrl = `${koaServer.baseUrl}/test`;
49
const agent = await handler.createAgent();
50
51
await agent.goto(exampleUrl);
52
let lastCommandId: number;
53
for (let i = 0; i <= 4; i += 1) {
54
const elem = agent.document.querySelector('a');
55
await agent.click(elem);
56
const resources = await agent.waitForResource(
57
{ type: 'Fetch' },
58
{ sinceCommandId: lastCommandId },
59
);
60
lastCommandId = await agent.lastCommandId;
61
expect(resources).toHaveLength(1);
62
expect(resources[0].url).toContain(`counter=${i}`);
63
}
64
await agent.close();
65
});
66
67
it('cancels a pending resource on agent close', async () => {
68
const exampleUrl = `${koaServer.baseUrl}/test`;
69
const agent = await handler.createAgent();
70
71
await agent.goto(exampleUrl);
72
73
const waitForResource = agent.waitForResource({ type: 'Fetch' });
74
// eslint-disable-next-line jest/valid-expect
75
const waitError = expect(waitForResource).rejects.toThrowError('disconnected');
76
await agent.close();
77
await waitError;
78
});
79
});
80
81