Browse Source

feat(test): 添加dotenv支持并重构测试配置

在.gitignore中添加.env文件
添加dotenv依赖管理环境变量
重构测试用例使用环境变量代替硬编码URL
在playwright配置中加载dotenv并设置baseURL
yourname 7 months ago
parent
commit
213ffcb109
5 changed files with 22 additions and 5 deletions
  1. 2 1
      .gitignore
  2. 14 1
      test/package-lock.json
  3. 2 1
      test/package.json
  4. 2 2
      test/pages_know_info.test.ts
  5. 2 0
      test/playwright.config.ts

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
 .aider*
 node_modules
-test-results
+test-results
+.env

+ 14 - 1
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",

+ 2 - 1
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",

+ 2 - 2
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 }) => {

+ 2 - 0
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'
             },
         },
     ],