分享一下我的思路。 同样是给予 playwright 实现
测试用例是基于自然语言的
await page.goto(`
https://www.baidu.com/`);
const testWriter = new TestWriter();
await
testWriter.run('选中输入框', await buildContext(page));
await
testWriter.run('输入你好', await buildContext(page));
await
testWriter.run('点击搜索', await buildContext(page));
testWriter 在执行的时候,我会把当前页面的截图提供给 ai 。 并且提供一系列的 tools 给 ai , 让 ai 自己编写代码
const description = `
1. the code is sent to playwright's page.evaluate(code) to execute.
2. The result will be serialised to json. Only the first 500 characters will be returned.
3. The result must be serialisable.
4. Please don't define a function
5. Don't write the await function
`;
export const evaluateCodeInPage = (page: Page): RunnerTool<string> => {
return {
definition: {
type: 'function',
function: {
name: 'evaluateCodeInPage',
description: description,
parameters: {
type: 'object',
properties: {
code: {
type: 'string',
description: 'JavaScript code to evaluate in the page context. will call page.evaluate(code)',
},
},
},
},
},
toolCall: async (args: string) => {
const { code } = JSON.parse(args);
const result = await page.evaluate(code);
return {
content: JSON.stringify(result).slice(0, 1000),
};
},
};
};
ai 自动根据 evaluateCodeInPage 查找页面元素是否存在,自动编写 playweight 的脚本。
编写好脚本后,会调用 SaveCodeTool 把测试脚本保存在本地
export const SaveCodeTool: openai.ChatCompletionTool = {
type: 'function',
function: {
name: 'saveCode',
description: '保存代码',
parameters: {
type: 'object',
properties: {
fileName: { type: 'string', description: 'no extension' },
code: { type: 'string' },
},
},
},
};
下一次运行的时候,就完全不需要 AI 了。 只需要执行测试脚本即可。
有些 ai 写不对的地方,也可以人肉帮 ai 写一下。
未来会在这里开源
https://github.com/mockforge/testforge