Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/test/frames.test.ts
1029 views
1
import { ITestKoaServer } from '@secret-agent/testing/helpers';
2
import { Helpers } from '@secret-agent/testing';
3
import Core, { Session } from '../index';
4
5
let koaServer: ITestKoaServer;
6
beforeAll(async () => {
7
await Core.start();
8
koaServer = await Helpers.runKoaServer(true);
9
});
10
afterAll(Helpers.afterAll);
11
afterEach(Helpers.afterEach);
12
13
test('can wait for sub-frames to load', async () => {
14
const connection = Core.addConnection();
15
Helpers.onClose(() => connection.disconnect());
16
const meta = await connection.createSession();
17
const tab = Session.getTab(meta);
18
koaServer.get('/main', ctx => {
19
ctx.body = `
20
<body>
21
<h1>Iframe Page</h1>
22
<iframe name="sub" src="/delay"></iframe>
23
</body>
24
`;
25
});
26
27
koaServer.get('/delay', async ctx => {
28
await new Promise(resolve => setTimeout(resolve, 500));
29
ctx.body = `
30
<body>
31
<h1>SubPage</h1>
32
</body>
33
`;
34
});
35
await tab.goto(`${koaServer.baseUrl}/main`);
36
await tab.waitForElement(['document', ['querySelector', 'iframe']]);
37
38
const frames = await tab.getFrameEnvironments();
39
expect(frames).toHaveLength(2);
40
const frameMeta = frames.find(x => x.parentFrameId !== null);
41
const subFrame = tab.frameEnvironmentsById.get(frameMeta.id);
42
43
await expect(subFrame.waitForLoad('PaintingStable')).resolves.toBe(undefined);
44
45
await tab.close();
46
});
47
48
test('should allow query selectors in cross-domain frames', async () => {
49
const connection = Core.addConnection();
50
Helpers.onClose(() => connection.disconnect());
51
const meta = await connection.createSession();
52
53
const session = Session.get(meta.sessionId);
54
const tab = Session.getTab(meta);
55
koaServer.get('/iframePage', ctx => {
56
ctx.body = `
57
<body>
58
<h1>Iframe Page</h1>
59
<iframe src="http://framesy.org/page"></iframe>
60
</body>
61
`;
62
});
63
64
session.mitmRequestSession.blockedResources = {
65
urls: ['http://framesy.org/page'],
66
types: [],
67
handlerFn: (request, response) => {
68
response.end(`<html lang="en"><body>
69
<h1>Framesy Page</h1>
70
<div>This is content inside the frame</div>
71
</body></html>`);
72
return true;
73
},
74
};
75
76
await tab.goto(`${koaServer.baseUrl}/iframePage`);
77
78
const outerH1 = await tab.execJsPath([
79
'window',
80
'document',
81
['querySelector', 'h1'],
82
'textContent',
83
]);
84
expect(outerH1.value).toBe('Iframe Page');
85
86
// should not allow cross-domain access
87
await expect(
88
tab.execJsPath([
89
'window',
90
'document',
91
['querySelector', 'iframe'],
92
'contentDocument',
93
['querySelector', 'h1'],
94
'textContent',
95
]),
96
).rejects.toThrowError();
97
98
const frameMetas = await tab.getFrameEnvironments();
99
const frames = await Promise.all(
100
frameMetas.map(async x => {
101
if (x.url) return x;
102
103
const env = tab.frameEnvironmentsById.get(x.id);
104
await env.waitForLoad('DomContentLoaded');
105
return env.toJSON();
106
}),
107
);
108
const innerFrameMeta = frames.find(x => x.url === 'http://framesy.org/page');
109
const innerFrame = tab.frameEnvironmentsById.get(innerFrameMeta.id);
110
111
const innerH1 = await innerFrame.execJsPath([
112
'window',
113
'document',
114
['querySelector', 'h1'],
115
'textContent',
116
]);
117
expect(innerH1.value).toBe('Framesy Page');
118
119
await tab.close();
120
});
121
122