Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/execute-js/test/basic.test.ts
2586 views
1
import { Agent, LocationStatus } from 'secret-agent';
2
import { Helpers } from '@secret-agent/testing';
3
import { ITestKoaServer } from '@secret-agent/testing/helpers';
4
import ExecuteJsPlugin from '@secret-agent/execute-js-plugin';
5
import Core from '@secret-agent/core';
6
import ConnectionToClient from '@secret-agent/core/server/ConnectionToClient';
7
import CoreServer from '@secret-agent/core/server';
8
import ExecuteJsCorePlugin from '../lib/CorePlugin';
9
10
let koaServer: ITestKoaServer;
11
let connectionToClient: ConnectionToClient;
12
let coreServer;
13
beforeAll(async () => {
14
coreServer = new CoreServer();
15
await coreServer.listen({ port: 0 });
16
Core.use(ExecuteJsCorePlugin);
17
Core.allowDynamicPluginLoading = false;
18
koaServer = await Helpers.runKoaServer();
19
connectionToClient = Core.addConnection();
20
Helpers.onClose(() => {
21
connectionToClient.disconnect();
22
koaServer.close();
23
coreServer.close();
24
}, true);
25
});
26
27
afterAll(Helpers.afterAll);
28
afterEach(Helpers.afterEach);
29
30
test('it should run function in browser and return response', async () => {
31
koaServer.get('/test1', ctx => {
32
ctx.body = `<body>
33
<script>
34
window.testRun = function() {
35
return 'ItWorks';
36
}
37
</script>
38
</body>`;
39
});
40
41
const userAgent =
42
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.165 Safari/537.36';
43
const agent = new Agent({
44
userAgent,
45
connectionToCore: {
46
host: await coreServer.address,
47
},
48
});
49
Helpers.onClose(() => agent.close(), true);
50
agent.use(ExecuteJsPlugin);
51
52
await agent.goto(`${koaServer.baseUrl}/test1`);
53
await agent.activeTab.waitForLoad(LocationStatus.DomContentLoaded);
54
const response = await agent.executeJs(() => {
55
// @ts-ignore
56
return window.testRun();
57
});
58
expect(response).toEqual('ItWorks');
59
await agent.close();
60
});
61
62
test('it should run function in browser and return incr', async () => {
63
koaServer.get('/test2', ctx => {
64
ctx.body = `<body>
65
<script>
66
window.testRun = function(num) {
67
return num + 1;
68
}
69
</script>
70
</body>`;
71
});
72
73
const userAgent =
74
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.165 Safari/537.36';
75
const agent = new Agent({
76
userAgent,
77
connectionToCore: {
78
host: await coreServer.address,
79
},
80
});
81
Helpers.onClose(() => agent.close(), true);
82
agent.use(ExecuteJsPlugin);
83
84
await agent.goto(`${koaServer.baseUrl}/test2`);
85
await agent.activeTab.waitForLoad(LocationStatus.DomContentLoaded);
86
const response = await agent.executeJs(num => {
87
// @ts-ignore
88
return window.testRun(num);
89
}, 1);
90
expect(response).toEqual(2);
91
await agent.close();
92
});
93
94
test('it should run function in iframe', async () => {
95
koaServer.get('/iframe-host', ctx => {
96
ctx.body = `<body>
97
<h1>Iframe page</h1>
98
<iframe src="/iframe" id="iframe"></iframe>
99
<script>
100
window.testFunc = function() {
101
return "page";
102
}
103
</script>
104
</body>`;
105
});
106
koaServer.get('/iframe', ctx => {
107
ctx.body = `<body>
108
<script>
109
window.testFunc = function() {
110
return "iframe";
111
}
112
</script>
113
</body>`;
114
});
115
116
const agent = new Agent({
117
connectionToCore: {
118
host: await coreServer.address,
119
},
120
});
121
Helpers.onClose(() => agent.close());
122
agent.use(ExecuteJsPlugin);
123
124
await agent.goto(`${koaServer.baseUrl}/iframe-host`);
125
await agent.waitForPaintingStable();
126
127
const iframe = await agent.getFrameEnvironment(agent.document.querySelector('iframe'));
128
await iframe.waitForLoad(LocationStatus.DomContentLoaded);
129
130
await expect(
131
iframe.executeJs(() => {
132
// @ts-ignore
133
return window.testFunc();
134
}),
135
).resolves.toBe('iframe');
136
await expect(
137
agent.activeTab.executeJs(() => {
138
// @ts-ignore
139
return window.testFunc();
140
}),
141
).resolves.toBe('page');
142
await expect(
143
agent.executeJs(() => {
144
// @ts-ignore
145
return window.testFunc();
146
}),
147
).resolves.toBe('page');
148
await agent.close();
149
});
150
151