refactor(auth): 移除未使用的 secondAccount 相关代码,简化认证逻辑

This commit is contained in:
LingandRX 2025-03-10 23:40:11 +08:00
parent 135718a8c3
commit 649efd4f78
4 changed files with 1 additions and 153 deletions

View File

@ -2,7 +2,7 @@
const { defineConfig, devices } = require('@playwright/test'); const { defineConfig, devices } = require('@playwright/test');
import dotenv from "dotenv"; import dotenv from "dotenv";
import path from 'path'; import path from 'path';
import { firstAccount, secondAccount } from './tests/common/auth'; import { firstAccount } from './tests/common/auth';
/** /**
* Read environment variables from file. * Read environment variables from file.
@ -18,7 +18,6 @@ dotenv.config({
}); });
const firstAuthFile = firstAccount.authFile; const firstAuthFile = firstAccount.authFile;
const secondAuthFile = secondAccount.authFile;
/** /**
* @see https://playwright.dev/docs/test-configuration * @see https://playwright.dev/docs/test-configuration
*/ */

View File

@ -1,6 +1,5 @@
import { test as base, expect } from '@playwright/test'; import { test as base, expect } from '@playwright/test';
import { HomeNavigation } from '@/pages/homeNavigationPage'; import { HomeNavigation } from '@/pages/homeNavigationPage';
import { writeIndexedDB } from '@/utils/indexedDBUtils';
import { firstAccount, secondAccount, AuthAccount } from '@/common/auth'; import { firstAccount, secondAccount, AuthAccount } from '@/common/auth';
type MyFixture = { type MyFixture = {
@ -27,22 +26,6 @@ export const test = base.extend<MyFixture>({
const mobileString = JSON.parse(mobileObject); const mobileString = JSON.parse(mobileObject);
const data = AuthAccount.loadIndexedDBFile(mobileString.val, [firstAccount, secondAccount]); 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.reload();
await page.getByRole('button', { name: /开\s单/ }).waitFor(); await page.getByRole('button', { name: /开\s单/ }).waitFor();

View File

@ -1,6 +1,4 @@
import { test as setup } from '@playwright/test'; import { test as setup } from '@playwright/test';
import { readIndexedDB } from '@/utils/indexedDBUtils';
import fs from 'fs';
import { firstAccount, secondAccount } from '@/common/auth'; import { firstAccount, secondAccount } from '@/common/auth';
const regex = /^https?:\/\/(?:www\.)?hlk\.meiguanjia\.net\/$/; const regex = /^https?:\/\/(?:www\.)?hlk\.meiguanjia\.net\/$/;
@ -15,7 +13,6 @@ for (let testAccount of testAccountArray) {
const account = testAccount.account; const account = testAccount.account;
const password = testAccount.password; const password = testAccount.password;
const authFile = testAccount.authFile; const authFile = testAccount.authFile;
const indexedDBFile = testAccount.indexedDBFile;
const $phonePassIcon = page const $phonePassIcon = page
.locator('div', { has: page.getByRole('textbox', { name: '请输入您的手机号码' }) }) .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录/ }).click();
await page.getByRole('button', { name: /开\s单/ }).waitFor(); 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 }); await page.context().storageState({ path: authFile });
}); });
} }

View File

@ -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));
});
}