|
| 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 { noColors } from 'playwright-core/lib/utils'; |
| 18 | + |
| 19 | +import { z } from '../sdk/bundle'; |
| 20 | +import { terminalScreen } from '../../reporters/base'; |
| 21 | +import ListReporter from '../../reporters/list'; |
| 22 | +import ListModeReporter from '../../reporters/listModeReporter'; |
| 23 | + |
| 24 | +import { defineTool } from './tool'; |
| 25 | +import { StringWriteStream } from './streams'; |
| 26 | + |
| 27 | +export const listTests = defineTool({ |
| 28 | + schema: { |
| 29 | + name: 'playwright_test_list_tests', |
| 30 | + title: 'List tests', |
| 31 | + description: 'List tests', |
| 32 | + inputSchema: z.object({}), |
| 33 | + type: 'readOnly', |
| 34 | + }, |
| 35 | + |
| 36 | + handle: async context => { |
| 37 | + const { screen, stream } = createScreen(); |
| 38 | + const reporter = new ListModeReporter({ screen, includeTestId: true }); |
| 39 | + const testRunner = await context.createTestRunner(); |
| 40 | + await testRunner.listTests(reporter, {}); |
| 41 | + |
| 42 | + return { |
| 43 | + content: [{ type: 'text', text: stream.content() }], |
| 44 | + }; |
| 45 | + }, |
| 46 | +}); |
| 47 | + |
| 48 | +export const runTests = defineTool({ |
| 49 | + schema: { |
| 50 | + name: 'playwright_test_run_tests', |
| 51 | + title: 'Run tests', |
| 52 | + description: 'Run tests', |
| 53 | + inputSchema: z.object({ |
| 54 | + locations: z.array(z.string()).describe('Folder, file or location to run: "test/e2e" or "test/e2e/file.spec.ts" or "test/e2e/file.spec.ts:20"'), |
| 55 | + projects: z.array(z.string()).optional().describe('Projects to run, projects from playwright.config.ts, by default runs all projects. Running with "chromium" is a good start'), |
| 56 | + }), |
| 57 | + type: 'readOnly', |
| 58 | + }, |
| 59 | + |
| 60 | + handle: async (context, params) => { |
| 61 | + const { screen, stream } = createScreen(); |
| 62 | + const configDir = context.configLocation.configDir; |
| 63 | + const reporter = new ListReporter({ configDir, screen, includeTestId: true }); |
| 64 | + const testRunner = await context.createTestRunner(); |
| 65 | + const result = await testRunner.runTests(reporter, { |
| 66 | + locations: params.locations, |
| 67 | + projects: params.projects, |
| 68 | + }); |
| 69 | + |
| 70 | + const text = stream.content(); |
| 71 | + return { |
| 72 | + content: [ |
| 73 | + { type: 'text', text }, |
| 74 | + ], |
| 75 | + isError: result.status !== 'passed', |
| 76 | + }; |
| 77 | + }, |
| 78 | +}); |
| 79 | + |
| 80 | +export const debugTest = defineTool({ |
| 81 | + schema: { |
| 82 | + name: 'playwright_test_debug_test', |
| 83 | + title: 'Debug single test', |
| 84 | + description: 'Debug single test', |
| 85 | + inputSchema: z.object({ |
| 86 | + test: z.object({ |
| 87 | + id: z.string().describe('Test ID to debug.'), |
| 88 | + title: z.string().describe('Human readable test title for granting permission to debug the test.'), |
| 89 | + }), |
| 90 | + }), |
| 91 | + type: 'readOnly', |
| 92 | + }, |
| 93 | + |
| 94 | + handle: async (context, params) => { |
| 95 | + const stream = new StringWriteStream(); |
| 96 | + const screen = { |
| 97 | + ...terminalScreen, |
| 98 | + isTTY: false, |
| 99 | + colors: noColors, |
| 100 | + stdout: stream as unknown as NodeJS.WriteStream, |
| 101 | + stderr: stream as unknown as NodeJS.WriteStream, |
| 102 | + }; |
| 103 | + const configDir = context.configLocation.configDir; |
| 104 | + const reporter = new ListReporter({ configDir, screen }); |
| 105 | + const testRunner = await context.createTestRunner(); |
| 106 | + process.env.PLAYWRIGHT_DEBUGGER_ENABLED = '1'; |
| 107 | + const result = await testRunner.runTests(reporter, { |
| 108 | + headed: true, |
| 109 | + testIds: [params.test.id], |
| 110 | + // For automatic recovery |
| 111 | + timeout: 0, |
| 112 | + workers: 1, |
| 113 | + }).finally(() => { |
| 114 | + process.env.PLAYWRIGHT_DEBUGGER_ENABLED = undefined; |
| 115 | + }); |
| 116 | + |
| 117 | + const text = stream.content(); |
| 118 | + return { |
| 119 | + content: [ |
| 120 | + { type: 'text', text }, |
| 121 | + ], |
| 122 | + isError: result.status !== 'passed', |
| 123 | + }; |
| 124 | + }, |
| 125 | +}); |
| 126 | + |
| 127 | +function createScreen() { |
| 128 | + const stream = new StringWriteStream(); |
| 129 | + const screen = { |
| 130 | + ...terminalScreen, |
| 131 | + isTTY: false, |
| 132 | + colors: noColors, |
| 133 | + stdout: stream as unknown as NodeJS.WriteStream, |
| 134 | + stderr: stream as unknown as NodeJS.WriteStream, |
| 135 | + }; |
| 136 | + return { screen, stream }; |
| 137 | +} |
0 commit comments