Path: blob/main/plugins/execute-js/test/basic.test.ts
2586 views
import { Agent, LocationStatus } from 'secret-agent';1import { Helpers } from '@secret-agent/testing';2import { ITestKoaServer } from '@secret-agent/testing/helpers';3import ExecuteJsPlugin from '@secret-agent/execute-js-plugin';4import Core from '@secret-agent/core';5import ConnectionToClient from '@secret-agent/core/server/ConnectionToClient';6import CoreServer from '@secret-agent/core/server';7import ExecuteJsCorePlugin from '../lib/CorePlugin';89let koaServer: ITestKoaServer;10let connectionToClient: ConnectionToClient;11let coreServer;12beforeAll(async () => {13coreServer = new CoreServer();14await coreServer.listen({ port: 0 });15Core.use(ExecuteJsCorePlugin);16Core.allowDynamicPluginLoading = false;17koaServer = await Helpers.runKoaServer();18connectionToClient = Core.addConnection();19Helpers.onClose(() => {20connectionToClient.disconnect();21koaServer.close();22coreServer.close();23}, true);24});2526afterAll(Helpers.afterAll);27afterEach(Helpers.afterEach);2829test('it should run function in browser and return response', async () => {30koaServer.get('/test1', ctx => {31ctx.body = `<body>32<script>33window.testRun = function() {34return 'ItWorks';35}36</script>37</body>`;38});3940const userAgent =41'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';42const agent = new Agent({43userAgent,44connectionToCore: {45host: await coreServer.address,46},47});48Helpers.onClose(() => agent.close(), true);49agent.use(ExecuteJsPlugin);5051await agent.goto(`${koaServer.baseUrl}/test1`);52await agent.activeTab.waitForLoad(LocationStatus.DomContentLoaded);53const response = await agent.executeJs(() => {54// @ts-ignore55return window.testRun();56});57expect(response).toEqual('ItWorks');58await agent.close();59});6061test('it should run function in browser and return incr', async () => {62koaServer.get('/test2', ctx => {63ctx.body = `<body>64<script>65window.testRun = function(num) {66return num + 1;67}68</script>69</body>`;70});7172const userAgent =73'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';74const agent = new Agent({75userAgent,76connectionToCore: {77host: await coreServer.address,78},79});80Helpers.onClose(() => agent.close(), true);81agent.use(ExecuteJsPlugin);8283await agent.goto(`${koaServer.baseUrl}/test2`);84await agent.activeTab.waitForLoad(LocationStatus.DomContentLoaded);85const response = await agent.executeJs(num => {86// @ts-ignore87return window.testRun(num);88}, 1);89expect(response).toEqual(2);90await agent.close();91});9293test('it should run function in iframe', async () => {94koaServer.get('/iframe-host', ctx => {95ctx.body = `<body>96<h1>Iframe page</h1>97<iframe src="/iframe" id="iframe"></iframe>98<script>99window.testFunc = function() {100return "page";101}102</script>103</body>`;104});105koaServer.get('/iframe', ctx => {106ctx.body = `<body>107<script>108window.testFunc = function() {109return "iframe";110}111</script>112</body>`;113});114115const agent = new Agent({116connectionToCore: {117host: await coreServer.address,118},119});120Helpers.onClose(() => agent.close());121agent.use(ExecuteJsPlugin);122123await agent.goto(`${koaServer.baseUrl}/iframe-host`);124await agent.waitForPaintingStable();125126const iframe = await agent.getFrameEnvironment(agent.document.querySelector('iframe'));127await iframe.waitForLoad(LocationStatus.DomContentLoaded);128129await expect(130iframe.executeJs(() => {131// @ts-ignore132return window.testFunc();133}),134).resolves.toBe('iframe');135await expect(136agent.activeTab.executeJs(() => {137// @ts-ignore138return window.testFunc();139}),140).resolves.toBe('page');141await expect(142agent.executeJs(() => {143// @ts-ignore144return window.testFunc();145}),146).resolves.toBe('page');147await agent.close();148});149150151