From 649efd4f78103c54a446976cf8cb8972aef0cb43 Mon Sep 17 00:00:00 2001 From: LingandRX Date: Mon, 10 Mar 2025 23:40:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor(auth):=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=20secondAccount=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=EF=BC=8C=E7=AE=80=E5=8C=96=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playwright.config.js | 3 +- tests/fixtures/baseFixture.ts | 17 ----- tests/setup/boss_auth.setup.ts | 8 --- tests/utils/indexedDBUtils.ts | 126 --------------------------------- 4 files changed, 1 insertion(+), 153 deletions(-) delete mode 100644 tests/utils/indexedDBUtils.ts diff --git a/playwright.config.js b/playwright.config.js index 6d21cc2..2fa2f25 100644 --- a/playwright.config.js +++ b/playwright.config.js @@ -2,7 +2,7 @@ const { defineConfig, devices } = require('@playwright/test'); import dotenv from "dotenv"; import path from 'path'; -import { firstAccount, secondAccount } from './tests/common/auth'; +import { firstAccount } from './tests/common/auth'; /** * Read environment variables from file. @@ -18,7 +18,6 @@ dotenv.config({ }); const firstAuthFile = firstAccount.authFile; -const secondAuthFile = secondAccount.authFile; /** * @see https://playwright.dev/docs/test-configuration */ diff --git a/tests/fixtures/baseFixture.ts b/tests/fixtures/baseFixture.ts index 2af3b52..992fd70 100644 --- a/tests/fixtures/baseFixture.ts +++ b/tests/fixtures/baseFixture.ts @@ -1,6 +1,5 @@ import { test as base, expect } from '@playwright/test'; import { HomeNavigation } from '@/pages/homeNavigationPage'; -import { writeIndexedDB } from '@/utils/indexedDBUtils'; import { firstAccount, secondAccount, AuthAccount } from '@/common/auth'; type MyFixture = { @@ -27,22 +26,6 @@ export const test = base.extend({ const mobileString = JSON.parse(mobileObject); const data = AuthAccount.loadIndexedDBFile(mobileString.val, [firstAccount, secondAccount]); - await page.evaluate( - async ({ fnString, data }) => { - const writeIndexedDBFn = new Function('return ' + fnString)(); - return await writeIndexedDBFn(data); - }, - { fnString: writeIndexedDB.toString(), data }, - ); - - await page.waitForFunction( - async () => { - const databases = await indexedDB.databases(); // 获取所有数据库 - return databases.some(db => db.name === 'hlk_touch_test_init'); // 判断是否存在目标数据库 - }, - { timeout: 30000 }, - ); - await page.reload(); await page.getByRole('button', { name: /开\s单/ }).waitFor(); diff --git a/tests/setup/boss_auth.setup.ts b/tests/setup/boss_auth.setup.ts index 4e227de..20ffd91 100644 --- a/tests/setup/boss_auth.setup.ts +++ b/tests/setup/boss_auth.setup.ts @@ -1,6 +1,4 @@ import { test as setup } from '@playwright/test'; -import { readIndexedDB } from '@/utils/indexedDBUtils'; -import fs from 'fs'; import { firstAccount, secondAccount } from '@/common/auth'; const regex = /^https?:\/\/(?:www\.)?hlk\.meiguanjia\.net\/$/; @@ -15,7 +13,6 @@ for (let testAccount of testAccountArray) { const account = testAccount.account; const password = testAccount.password; const authFile = testAccount.authFile; - const indexedDBFile = testAccount.indexedDBFile; const $phonePassIcon = page .locator('div', { has: page.getByRole('textbox', { name: '请输入您的手机号码' }) }) @@ -44,11 +41,6 @@ for (let testAccount of testAccountArray) { await page.getByRole('button', { name: /登\s录/ }).click(); await page.getByRole('button', { name: /开\s单/ }).waitFor(); } - const result = await page.evaluate(async readIndexedDBFnString => { - const readIndexedDBFn = new Function('return ' + readIndexedDBFnString)(); - return await readIndexedDBFn(); - }, readIndexedDB.toString()); - fs.writeFileSync(indexedDBFile, JSON.stringify(result)); await page.context().storageState({ path: authFile }); }); } diff --git a/tests/utils/indexedDBUtils.ts b/tests/utils/indexedDBUtils.ts deleted file mode 100644 index f6bc553..0000000 --- a/tests/utils/indexedDBUtils.ts +++ /dev/null @@ -1,126 +0,0 @@ -export async function readIndexedDB() { - const databases = await indexedDB.databases(); - const allData = []; - - for (const db of databases) { - const databaseName = db.name; - - // 打开数据库 - const request = indexedDB.open(databaseName); - - const databaseData = await new Promise((resolve, reject) => { - request.onsuccess = (event) => { - const db = event.target.result; - const transaction = db.transaction(db.objectStoreNames, 'readonly'); - const objectStore = transaction.objectStore(db.objectStoreNames[0]); - const cursorRequest = objectStore.openCursor(); - - const storeData = []; - - cursorRequest.onsuccess = (event) => { - const cursor = event.target.result; - if (cursor) { - storeData.push({ key: cursor.key, value: cursor.value }); - cursor.continue(); - } else { - resolve(storeData); // 执行 resolve,返回数据 - } - }; - - cursorRequest.onerror = (event) => { - reject(event.target.error); // 发生错误时 reject - }; - }; - - request.onerror = (event) => { - reject(event.target.error); // 发生错误时 reject - }; - }); - - // 将数据库数据存入 allData - allData.push({ - databaseName, - data: databaseData, - }); - } - - return allData; -} - -export async function writeIndexedDB(jsonData) { - return new Promise((resolve, reject) => { - const processDatabase = async (database, objectStoreNames) => { - try { - // 动态获取数据库版本号 - const version = database.version || 2; - const openRequest = indexedDB.open(database.databaseName, version); - - // 处理数据库版本升级 - openRequest.onupgradeneeded = event => { - const db = event.target.result; - objectStoreNames.forEach(storeName => { - if (!db.objectStoreNames.contains(storeName)) { - db.createObjectStore(storeName); - } - }); - }; - - // 处理数据库打开成功 - openRequest.onsuccess = async event => { - const db = event.target.result; - const table = database.data; - const transaction = db.transaction(objectStoreNames, 'readwrite'); - const objectStore = transaction.objectStore(objectStoreNames[0]); - - // 添加事务错误处理 - transaction.onerror = event => { - console.error('事务失败:', event.target.error); - reject('事务失败'); - }; - - let completedOperations = 0; - const totalOperations = table.length; - - // 使用 Promise.all 来确保所有的写入操作完成 - const writeOperations = table.map((item, index) => { - return new Promise((innerResolve, innerReject) => { - const addObject = objectStore.put(item.value, item.key); - addObject.onsuccess = () => { - completedOperations++; - if (completedOperations === totalOperations) { - innerResolve('数据写入成功'); - } - }; - addObject.onerror = event => { - console.error('数据添加失败:', event.target.error); - innerReject('数据添加失败'); - }; - }); - }); - - // 等待所有写入操作完成 - try { - await Promise.all(writeOperations); - resolve('所有数据写入完成'); - } catch (error) { - reject('数据写入失败'); - } - }; - - // 错误处理 - openRequest.onerror = event => { - console.error('数据库打开失败:', event.target.error); - reject('数据库打开失败'); - }; - } catch (error) { - reject('数据库操作失败: ' + error); - } - }; - - // 遍历所有数据库 - const objectStoreNames = ['keyvaluepairs', 'local-forage-detect-blob-support']; - Promise.all(jsonData.map(database => processDatabase(database, objectStoreNames))) - .then(() => resolve('所有数据库操作完成')) - .catch(error => reject('操作失败: ' + error)); - }); -}