Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/default-browser-emulator/test/plugins.test.ts
1029 views
1
import * as Fs from 'fs';
2
import * as Path from 'path';
3
import { inspect } from 'util';
4
import * as Helpers from '@secret-agent/testing/helpers';
5
import Puppet from '@secret-agent/puppet';
6
import Log from '@secret-agent/commons/Logger';
7
import CorePlugins from '@secret-agent/core/lib/CorePlugins';
8
import { IBoundLog } from '@secret-agent/interfaces/ILog';
9
import BrowserEmulator from '../index';
10
import * as pluginsChrome from './plugins-Chrome.json';
11
import { getOverrideScript } from '../lib/DomOverridesBuilder';
12
import DomExtractor = require('./DomExtractor');
13
14
const { log } = Log(module);
15
const selectBrowserMeta = BrowserEmulator.selectBrowserMeta();
16
17
const navigatorJsonPath = Path.resolve(
18
__dirname,
19
'../data/as-chrome-88-0/as-mac-os-10-14/window-navigator.json',
20
);
21
22
const { navigator } = JSON.parse(Fs.readFileSync(navigatorJsonPath, 'utf8')) as any;
23
24
let puppet: Puppet;
25
beforeAll(async () => {
26
puppet = new Puppet(selectBrowserMeta.browserEngine);
27
Helpers.onClose(() => puppet.close(), true);
28
await puppet.start();
29
});
30
31
afterAll(Helpers.afterAll);
32
afterEach(Helpers.afterEach);
33
34
const debug = process.env.DEBUG || false;
35
36
test('it should override plugins in a browser window', async () => {
37
const httpServer = await Helpers.runHttpServer();
38
const plugins = new CorePlugins({ selectBrowserMeta }, log as IBoundLog);
39
const context = await puppet.newContext(plugins, log);
40
Helpers.onClose(() => context.close());
41
const page = await context.newPage();
42
43
page.on('page-error', console.log);
44
if (debug) {
45
page.on('console', console.log);
46
}
47
await page.addNewDocumentScript(
48
getOverrideScript('navigator.plugins', {
49
mimeTypes: [
50
{
51
type: 'application/pdf',
52
suffixes: 'pdf',
53
description: '',
54
__pluginName: 'Chrome PDF Viewer',
55
},
56
{
57
type: 'application/x-google-chrome-pdf',
58
suffixes: 'pdf',
59
description: 'Portable Document Format',
60
__pluginName: 'Chrome PDF Plugin',
61
},
62
{
63
type: 'application/x-nacl',
64
suffixes: '',
65
description: 'Native Client Executable',
66
__pluginName: 'Native Client',
67
},
68
{
69
type: 'application/x-pnacl',
70
suffixes: '',
71
description: 'Portable Native Client Executable',
72
__pluginName: 'Native Client',
73
},
74
],
75
plugins: [
76
{
77
name: 'Chrome PDF Plugin',
78
filename: 'internal-pdf-viewer',
79
description: 'Portable Document Format',
80
},
81
{
82
name: 'Chrome PDF Viewer',
83
filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai',
84
description: '',
85
},
86
{
87
name: 'Native Client',
88
filename: 'internal-nacl-plugin',
89
description: '',
90
},
91
],
92
}).script,
93
false,
94
);
95
await Promise.all([
96
page.navigate(httpServer.url),
97
page.mainFrame.waitOn('frame-lifecycle', ev => ev.name === 'DOMContentLoaded'),
98
]);
99
100
const hasPlugins = await page.mainFrame.evaluate(
101
`'plugins' in navigator && 'mimeTypes' in navigator`,
102
false,
103
);
104
expect(hasPlugins).toBe(true);
105
106
const pluginCount = await page.mainFrame.evaluate(`navigator.plugins.length`, false);
107
expect(pluginCount).toBe(3);
108
109
const plugin1Count = await page.mainFrame.evaluate(
110
`(() => {
111
let mimes = [];
112
for(const mime of navigator.plugins[0]) {
113
mimes.push(mime.type);
114
}
115
return mimes;
116
})()`,
117
false,
118
);
119
expect(plugin1Count).toStrictEqual(['application/x-google-chrome-pdf']);
120
121
const mimecount = await page.mainFrame.evaluate(`navigator.mimeTypes.length`, false);
122
expect(mimecount).toBe(4);
123
124
const structure = JSON.parse(
125
(await page.mainFrame.evaluate(
126
`new (${DomExtractor.toString()})('window').run(window, 'window', ['Plugin', 'PluginArray', 'MimeType', 'MimeTypeArray','navigator'])`,
127
false,
128
)) as any,
129
).window;
130
131
for (const proto of ['Plugin', 'PluginArray', 'MimeType', 'MimeTypeArray']) {
132
if (debug) console.log(proto, inspect(structure[proto], false, null, true));
133
expect(structure[proto]).toStrictEqual(pluginsChrome[proto]);
134
}
135
const navigatorStructure = structure.navigator;
136
if (debug) console.log(inspect(navigatorStructure.mimeTypes, false, null, true));
137
expect(navigatorStructure.mimeTypes).toStrictEqual(navigator.mimeTypes);
138
139
if (debug) console.log(inspect(navigatorStructure.plugins, false, null, true));
140
expect(navigatorStructure.plugins).toStrictEqual(navigator.plugins);
141
}, 60e3);
142
143