前提:
(1)安装了nodejs
(2)创建了测试目录
(3)使用Vscode安装了Playwright插件
可以参考官方文档:https://playwright.dev/docs/getting-started-vscode
在vscode界面最左侧的按钮选择Explorer, 创建一个与tests目录同级的目录methods,并在methods目录下创建文件method1.ts,
目录结构如下:
在文件method1.ts中定义方法, 示例:
export async function testArea1({page}, locator1:string, locator2:string, expectText1:string){
await page.goto(https://www.google.com);
await page.click(locator1);
await expect(page.locator2).toHaveText(expectText1);
}
在tests目录下的测试case, 即.spec.ts文件中使用这个方法。
需要添加import语句,注意目录层级问题,./表示同一级目录, ../表示上一级目录
示例:
import { test, expect } from '@playwright/test';
import { testArea1 } from '../methods/method1';
test('测试case1', async ({ page }) => {
await page.goto('https://playwright.dev/');
// 这里的'div1', 'div2'为伪代码,需要换成页面元素定位locator字符串
await testArea1({page}, 'div1', 'div2', 'google');
});
注意这里的'div1', 'div2'为伪代码,需要换成页面元素定位locator字符串
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有