From f158ff6ff5615f9c41ab4ab3a6e50ad0f6c86aed Mon Sep 17 00:00:00 2001 From: yourname Date: Tue, 6 May 2025 06:24:04 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat(mobile):=20=E5=8C=BA=E5=88=86=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E7=AB=AFtoken=E5=AD=98=E5=82=A8=E9=94=AE=E5=90=8D?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0Playwright=E6=B5=8B=E8=AF=95=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9mobile=E7=AB=AFtoken=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E9=94=AE=E5=90=8D=E4=B8=BA'mobile=5Ftoken'=E4=BB=A5=E5=8C=BA?= =?UTF-8?q?=E5=88=86=E4=B8=8D=E5=90=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0Playwright=E6=B5=8B=E8=AF=95=E5=A5=97?= =?UTF-8?q?=E4=BB=B6=E7=94=A8=E4=BA=8E=E7=9F=A5=E8=AF=86=E5=BA=93=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD=E6=B5=8B=E8=AF=95=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0.gitignore=E5=BF=BD=E7=95=A5node=5Fmodules=E5=92=8Ctes?= =?UTF-8?q?t-results=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + client/mobile/hooks.tsx | 2 +- test/package-lock.json | 75 ++++++++++++++++++++++++++++++++++++ test/package.json | 14 +++++++ test/pages_know_info.test.ts | 42 ++++++++++++++++++++ test/playwright.config.ts | 13 +++++++ 6 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 test/package-lock.json create mode 100644 test/package.json create mode 100644 test/pages_know_info.test.ts create mode 100644 test/playwright.config.ts diff --git a/.gitignore b/.gitignore index b0ac3ed..c6842fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .aider* +node_modules +test-results \ No newline at end of file diff --git a/client/mobile/hooks.tsx b/client/mobile/hooks.tsx index 9b4e1ee..fcffb21 100644 --- a/client/mobile/hooks.tsx +++ b/client/mobile/hooks.tsx @@ -27,7 +27,7 @@ const ThemeContext = createContext(null); // 认证提供者组件 export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [user, setUser] = useState(null); - const [token, setToken] = useState(getLocalStorageWithExpiry('token')); + const [token, setToken] = useState(getLocalStorageWithExpiry('mobile_token')); const [isAuthenticated, setIsAuthenticated] = useState(false); const queryClient = useQueryClient(); diff --git a/test/package-lock.json b/test/package-lock.json new file mode 100644 index 0000000..07f03fe --- /dev/null +++ b/test/package-lock.json @@ -0,0 +1,75 @@ +{ + "name": "test", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "test", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@playwright/test": "1.51.1" + } + }, + "node_modules/@playwright/test": { + "version": "1.51.1", + "resolved": "https://registry.npmmirror.com/@playwright/test/-/test-1.51.1.tgz", + "integrity": "sha512-nM+kEaTSAoVlXmMPH10017vn3FSiFqr/bh4fKg9vmAdMfd9SDqRZNvPSiAHADc/itWak+qPvMPZQOPwCBW7k7Q==", + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.51.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/playwright": { + "version": "1.51.1", + "resolved": "https://registry.npmmirror.com/playwright/-/playwright-1.51.1.tgz", + "integrity": "sha512-kkx+MB2KQRkyxjYPc3a0wLZZoDczmppyGJIvQ43l+aZihkaVvmu/21kiyaHeHjiFxjxNNFnUncKmcGIyOojsaw==", + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.51.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.51.1", + "resolved": "https://registry.npmmirror.com/playwright-core/-/playwright-core-1.51.1.tgz", + "integrity": "sha512-/crRMj8+j/Nq5s8QcvegseuyeZPxpQCZb6HNk3Sos3BlZyAknRjoyJPFWkpNn8v0+P3WiwqFF8P+zQo4eqiNuw==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + } + } +} diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..ebf45cf --- /dev/null +++ b/test/package.json @@ -0,0 +1,14 @@ +{ + "name": "test", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "@playwright/test":"1.51.1" + }, + "author": "", + "license": "ISC", + "description": "" +} diff --git a/test/pages_know_info.test.ts b/test/pages_know_info.test.ts new file mode 100644 index 0000000..23f043c --- /dev/null +++ b/test/pages_know_info.test.ts @@ -0,0 +1,42 @@ +import { test, expect } from '@playwright/test'; + +test.describe('知识库管理CRUD测试', () => { + test.beforeEach(async ({ page }) => { + // 先登录获取有效token + await page.goto('https://pre-117-77-template.r.d8d.fun/admin/login'); + await page.fill('input[placeholder="用户名"]', 'admin'); + await page.fill('input[placeholder="密码"]', 'admin123'); + await page.click('button:has-text("登 录")'); + await expect(page).toHaveURL(/\/admin\/dashboard/); + + // 导航到测试页面 + await page.goto('https://pre-117-77-template.r.d8d.fun/admin/know-info'); + }); + + test('添加测试文章', async ({ page }) => { + await page.click('button:has-text("添加文章")'); + await page.fill('input[placeholder="请输入文章标题"]', '测试文章-自动化测试'); + await page.click('button:has-text("确 定")'); + await expect(page.locator('text=测试文章-自动化测试')).toBeVisible(); + }); + + test('搜索测试文章', async ({ page }) => { + await page.fill('input[placeholder="要搜索的文章标题"]', '测试文章-自动化测试'); + await page.click('button:has-text("搜 索")'); + await expect(page.locator('text=测试文章-自动化测试')).toBeVisible(); + await page.click('button:has-text("重 置")'); + }); + + test('修改测试文章', async ({ page }) => { + await page.click('tr:has-text("测试文章-自动化测试") >> button:has-text("编辑")'); + await page.fill('input[placeholder="请输入文章标题"]', '修改后的测试标题'); + await page.click('button:has-text("确 定")'); + await expect(page.locator('text=修改后的测试标题')).toBeVisible(); + }); + + test('删除测试文章', async ({ page }) => { + await page.click('tr:has-text("修改后的测试标题") >> button:has-text("删除")'); + await page.click('.ant-btn-primary:has-text("确 定")'); + await expect(page.locator('text=修改后的测试标题')).not.toBeVisible(); + }); +}); \ No newline at end of file diff --git a/test/playwright.config.ts b/test/playwright.config.ts new file mode 100644 index 0000000..a5e2df1 --- /dev/null +++ b/test/playwright.config.ts @@ -0,0 +1,13 @@ +// playwright.config.js +const { defineConfig } = require('@playwright/test'); + +module.exports = defineConfig({ + projects: [ + { + name: 'chromium', + use: { + browserName: 'chromium', + }, + }, + ], +}); \ No newline at end of file From 213ffcb109e1fbba7b031a0ec136b79d36a88638 Mon Sep 17 00:00:00 2001 From: yourname Date: Tue, 6 May 2025 06:46:10 +0000 Subject: [PATCH 2/2] =?UTF-8?q?feat(test):=20=E6=B7=BB=E5=8A=A0dotenv?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B9=B6=E9=87=8D=E6=9E=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在.gitignore中添加.env文件 添加dotenv依赖管理环境变量 重构测试用例使用环境变量代替硬编码URL 在playwright配置中加载dotenv并设置baseURL --- .gitignore | 3 ++- test/package-lock.json | 15 ++++++++++++++- test/package.json | 3 ++- test/pages_know_info.test.ts | 4 ++-- test/playwright.config.ts | 2 ++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c6842fb..75abfdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .aider* node_modules -test-results \ No newline at end of file +test-results +.env \ No newline at end of file diff --git a/test/package-lock.json b/test/package-lock.json index 07f03fe..17ca25e 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@playwright/test": "1.51.1" + "@playwright/test": "^1.51.1", + "dotenv": "^16.5.0" } }, "node_modules/@playwright/test": { @@ -27,6 +28,18 @@ "node": ">=18" } }, + "node_modules/dotenv": { + "version": "16.5.0", + "resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.5.0.tgz", + "integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/fsevents": { "version": "2.3.2", "resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz", diff --git a/test/package.json b/test/package.json index ebf45cf..986b7b6 100644 --- a/test/package.json +++ b/test/package.json @@ -6,7 +6,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { - "@playwright/test":"1.51.1" + "@playwright/test": "^1.51.1", + "dotenv": "^16.5.0" }, "author": "", "license": "ISC", diff --git a/test/pages_know_info.test.ts b/test/pages_know_info.test.ts index 23f043c..999f77e 100644 --- a/test/pages_know_info.test.ts +++ b/test/pages_know_info.test.ts @@ -3,14 +3,14 @@ import { test, expect } from '@playwright/test'; test.describe('知识库管理CRUD测试', () => { test.beforeEach(async ({ page }) => { // 先登录获取有效token - await page.goto('https://pre-117-77-template.r.d8d.fun/admin/login'); + await page.goto(`${process.env.BASE_URL}/admin/login`); await page.fill('input[placeholder="用户名"]', 'admin'); await page.fill('input[placeholder="密码"]', 'admin123'); await page.click('button:has-text("登 录")'); await expect(page).toHaveURL(/\/admin\/dashboard/); // 导航到测试页面 - await page.goto('https://pre-117-77-template.r.d8d.fun/admin/know-info'); + await page.goto(`${process.env.BASE_URL}/admin/know-info`); }); test('添加测试文章', async ({ page }) => { diff --git a/test/playwright.config.ts b/test/playwright.config.ts index a5e2df1..ad5f6d4 100644 --- a/test/playwright.config.ts +++ b/test/playwright.config.ts @@ -1,4 +1,5 @@ // playwright.config.js +require('dotenv').config(); const { defineConfig } = require('@playwright/test'); module.exports = defineConfig({ @@ -7,6 +8,7 @@ module.exports = defineConfig({ name: 'chromium', use: { browserName: 'chromium', + baseURL: process.env.BASE_URL || 'http://localhost:3000' }, }, ],