|
| 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 fs from 'node:fs'; |
| 18 | + |
| 19 | +import { test, expect } from './fixtures'; |
| 20 | + |
| 21 | +test('browser_type', async ({ startClient, server }) => { |
| 22 | + const secretsFile = test.info().outputPath('secrets.env'); |
| 23 | + await fs.promises.writeFile(secretsFile, 'X-PASSWORD=password123'); |
| 24 | + |
| 25 | + const { client } = await startClient({ |
| 26 | + args: ['--secrets', secretsFile], |
| 27 | + }); |
| 28 | + |
| 29 | + server.setContent('/', ` |
| 30 | + <!DOCTYPE html> |
| 31 | + <html> |
| 32 | + <input type='keypress' onkeypress="console.log('Key pressed:', event.key, ', Text:', event.target.value)"></input> |
| 33 | + </html> |
| 34 | + `, 'text/html'); |
| 35 | + |
| 36 | + await client.callTool({ |
| 37 | + name: 'browser_navigate', |
| 38 | + arguments: { |
| 39 | + url: server.PREFIX, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + { |
| 44 | + const response = await client.callTool({ |
| 45 | + name: 'browser_type', |
| 46 | + arguments: { |
| 47 | + element: 'textbox', |
| 48 | + ref: 'e2', |
| 49 | + text: 'X-PASSWORD', |
| 50 | + submit: true, |
| 51 | + }, |
| 52 | + }); |
| 53 | + expect(response).toHaveResponse({ |
| 54 | + code: `await page.getByRole('textbox').fill(process.env['X-PASSWORD']); |
| 55 | +await page.getByRole('textbox').press('Enter');`, |
| 56 | + pageState: expect.stringContaining(`- textbox`), |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + expect(await client.callTool({ |
| 61 | + name: 'browser_console_messages', |
| 62 | + })).toHaveResponse({ |
| 63 | + result: expect.stringContaining(`[LOG] Key pressed: Enter , Text: <secret>X-PASSWORD</secret>`), |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | + |
| 68 | +test('browser_fill_form', async ({ startClient, server }) => { |
| 69 | + const secretsFile = test.info().outputPath('secrets.env'); |
| 70 | + await fs.promises.writeFile(secretsFile, 'X-PASSWORD=password123'); |
| 71 | + |
| 72 | + const { client } = await startClient({ |
| 73 | + args: ['--secrets', secretsFile], |
| 74 | + }); |
| 75 | + |
| 76 | + server.setContent('/', ` |
| 77 | + <!DOCTYPE html> |
| 78 | + <html> |
| 79 | + <body> |
| 80 | + <form> |
| 81 | + <label> |
| 82 | + <input type="email" id="email" name="email" /> |
| 83 | + Email |
| 84 | + </label> |
| 85 | + <label> |
| 86 | + <input type="password" id="name" name="password" /> |
| 87 | + Password |
| 88 | + </label> |
| 89 | + </form> |
| 90 | + </body> |
| 91 | + </html> |
| 92 | + `, 'text/html'); |
| 93 | + |
| 94 | + await client.callTool({ |
| 95 | + name: 'browser_navigate', |
| 96 | + arguments: { url: server.PREFIX }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(await client.callTool({ |
| 100 | + name: 'browser_fill_form', |
| 101 | + arguments: { |
| 102 | + fields: [ |
| 103 | + { |
| 104 | + name: 'Email textbox', |
| 105 | + type: 'textbox', |
| 106 | + ref: 'e4', |
| 107 | + value: 'John Doe' |
| 108 | + }, |
| 109 | + { |
| 110 | + name: 'Password textbox', |
| 111 | + type: 'textbox', |
| 112 | + ref: 'e6', |
| 113 | + value: 'X-PASSWORD' |
| 114 | + }, |
| 115 | + ] |
| 116 | + }, |
| 117 | + })).toHaveResponse({ |
| 118 | + code: `await page.getByRole('textbox', { name: 'Email' }).fill('John Doe'); |
| 119 | +await page.getByRole('textbox', { name: 'Password' }).fill(process.env['X-PASSWORD']);`, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(await client.callTool({ |
| 123 | + name: 'browser_snapshot', |
| 124 | + arguments: {}, |
| 125 | + })).toHaveResponse({ |
| 126 | + pageState: expect.stringContaining(`- textbox \"Password\" [active] [ref=e6]: <secret>X-PASSWORD</secret>`), |
| 127 | + }); |
| 128 | +}); |
0 commit comments