- 添加项目配置文件和环境变量设置 - 创建测试用例目录结构和命名规范 - 实现基础测试 fixture 和页面对象模型 - 添加示例测试用例和数据生成器 - 配置 playwright 和 gitignore 文件
82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import { test as base, expect } from '@playwright/test';
|
|
import { HomeNavigation } from '@/pages/homeNavigationPage';
|
|
import { writeIndexedDB } from '@/utils/indexedDBUtils';
|
|
import { readFileSync } from 'fs';
|
|
|
|
type MyFixture = {
|
|
homeNavigation: HomeNavigation;
|
|
billSet: Set<string>;
|
|
};
|
|
|
|
export const test = base.extend<MyFixture>({
|
|
page: async ({ page, baseURL }, use) => {
|
|
await page.addLocatorHandler(page.locator('.left_img > .close_btn > .close_icon > svg'), async () => {
|
|
await page.locator('.left_img > .close_btn > .close_icon > svg').click();
|
|
await expect(page.locator('.versionModal_main_content')).toBeVisible();
|
|
});
|
|
|
|
if (!baseURL) throw new Error('baseURL is required');
|
|
await page.goto(baseURL);
|
|
// await page.getByRole('button', { name: /开\s单/ }).waitFor();
|
|
|
|
const mobileObject = await page.evaluate(() => {
|
|
return window.localStorage.getItem('hlk_touch_mobile');
|
|
});
|
|
if (!mobileObject && mobileObject !== 'null') {
|
|
throw new Error('localStorage does not contain boss_account or boss_account2');
|
|
}
|
|
const mobileString = JSON.parse(mobileObject);
|
|
const bossAccount = process.env.boss_account || '';
|
|
const bossAccount2 = process.env.boss_account2 || '';
|
|
|
|
let jsonData: [];
|
|
if (mobileString.val?.includes(bossAccount)) {
|
|
jsonData = JSON.parse(readFileSync('.auth/admin_first_indexeddb.json', 'utf-8'));
|
|
} else if (mobileString.val?.includes(bossAccount2)) {
|
|
jsonData = JSON.parse(readFileSync('.auth/admin_second_indexeddb.json', 'utf-8'));
|
|
} else {
|
|
throw new Error('localStorage does not contain boss_account or boss_account2');
|
|
}
|
|
|
|
await page.evaluate(
|
|
async ({ fnString, jsonData }) => {
|
|
// 在浏览器上下文中动态创建函数
|
|
const writeIndexedDBFn = new Function('return ' + fnString)();
|
|
|
|
// 调用函数并传递参数
|
|
return await writeIndexedDBFn(jsonData);
|
|
},
|
|
{ fnString: writeIndexedDB.toString(), jsonData },
|
|
);
|
|
// await page.reload();
|
|
await page.waitForFunction(
|
|
async () => {
|
|
const databases = await indexedDB.databases(); // 获取所有数据库
|
|
return databases.some(db => db.name === 'hlk_touch_test_init'); // 判断是否存在目标数据库
|
|
},
|
|
{ timeout: 30000 }, // 设置超时时间,默认为 30 秒
|
|
);
|
|
await page.getByRole('button', { name: /开\s单/ }).waitFor();
|
|
|
|
await use(page);
|
|
},
|
|
|
|
/**
|
|
* 首页导航
|
|
*/
|
|
homeNavigation: async ({ page }, use) => {
|
|
const homeNavigation = new HomeNavigation(page);
|
|
await use(homeNavigation);
|
|
},
|
|
|
|
/**
|
|
* 流水单据集合(无重复水单)
|
|
*/
|
|
billSet: async ({ page }, use) => {
|
|
const billSet = new Set<string>();
|
|
await use(billSet);
|
|
},
|
|
});
|
|
|
|
export { expect } from '@playwright/test';
|