|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { test, expect } from './fixtures'; |
| 18 | +import fs from 'fs'; |
| 19 | + |
| 20 | + |
| 21 | +for (const context of ['isolated', 'persistent']) { |
| 22 | + test(`--init-script option loads and executes script (${context})`, async ({ startClient, server }, testInfo) => { |
| 23 | + // Create a temporary init script |
| 24 | + const initScriptPath = testInfo.outputPath('init-script1.js'); |
| 25 | + const initScriptContent1 = `window.testInitScriptExecuted = true;`; |
| 26 | + await fs.promises.writeFile(initScriptPath, initScriptContent1); |
| 27 | + |
| 28 | + const initScriptPath2 = testInfo.outputPath('init-script2.js'); |
| 29 | + const initScriptContent2 = `console.log('Init script executed successfully');`; |
| 30 | + await fs.promises.writeFile(initScriptPath2, initScriptContent2); |
| 31 | + |
| 32 | + // Start the client with the init script option |
| 33 | + const { client: client } = await startClient({ |
| 34 | + args: [`--init-script=${initScriptPath}`, `--init-script=${initScriptPath2}`, ...(context === 'isolated' ? ['--isolated'] : [])] |
| 35 | + }); |
| 36 | + |
| 37 | + // Navigate to a page and verify the init script was executed |
| 38 | + await client.callTool({ |
| 39 | + name: 'browser_navigate', |
| 40 | + arguments: { url: server.HELLO_WORLD }, |
| 41 | + }); |
| 42 | + |
| 43 | + await client.callTool({ |
| 44 | + name: 'browser_evaluate', |
| 45 | + arguments: { function: '() => console.log("Custom log")' } |
| 46 | + }); |
| 47 | + |
| 48 | + // Check that the init script variables are available |
| 49 | + expect(await client.callTool({ |
| 50 | + name: 'browser_evaluate', |
| 51 | + arguments: { function: '() => window.testInitScriptExecuted' } |
| 52 | + })).toHaveResponse({ |
| 53 | + result: 'true', |
| 54 | + }); |
| 55 | + |
| 56 | + expect(await client.callTool({ |
| 57 | + name: 'browser_console_messages', |
| 58 | + })).toHaveResponse({ |
| 59 | + result: expect.stringMatching(/Init script executed successfully.*Custom log/ms), |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +test('--init-script option with non-existent file throws error', async ({ startClient }, testInfo) => { |
| 65 | + const nonExistentPath = testInfo.outputPath('non-existent-script.js'); |
| 66 | + |
| 67 | + // Attempting to start with a non-existent init script should fail |
| 68 | + await expect(startClient({ |
| 69 | + args: [`--init-script=${nonExistentPath}`] |
| 70 | + })).rejects.toThrow(); |
| 71 | +}); |
0 commit comments