Browse Source

test(e2e): add build script for e2e testing and update e2e testing instructions in documentation

dengdx 3 weeks ago
parent
commit
7db8190f00
2 changed files with 51 additions and 3 deletions
  1. 46 0
      .build/h5_for_local_e2e.js
  2. 5 3
      README.md

+ 46 - 0
.build/h5_for_local_e2e.js

@@ -0,0 +1,46 @@
+// 执行脚本 taro build --type h5  ,并且把环境变量 TARO_API_URL  传递过去
+
+const { execSync } = require('child_process');
+const fs = require('fs');
+const path = require('path');
+
+function isCI() {
+    const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
+    return isCI;
+}
+
+function getGhToken() {
+    // 1. 在 GitHub Actions 里官方会注入 ACTIONS_RUNTIME_TOKEN 或 CI=true
+    const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
+
+    if (isCI) {
+        console.log(`you are running in ic and the GH_token is ${process.env.GH_TOKEN}`)
+        // CI 环境:直接拿 secret(工作流里必须 env:{GH_TOKEN:${{secrets.GH_TOKEN}}})
+        const token = process.env.GH_TOKEN;
+        if (!token) throw new Error('CI 环境缺少 GH_TOKEN,请检查 workflow yaml');
+        return token;
+    }
+
+    // 2. 本地开发:优先读本地 ~/.gh-token(自己放的),没有再读环境变量,还没有就空
+    try {
+        return readFileSync(join(require('os').homedir(), '.gh-token'), 'utf8').trim();
+    } catch {
+        return process.env.GH_TOKEN || '';
+    }
+}
+
+const TARO_API_URL = ''; // 远程地址,这里写死,要做成部署后可配置
+const TARO_MQTT_URL = 'ws://localhost:8083/mqtt';
+const rootDir = path.join(__dirname, '..');          // 项目根目录
+
+execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL, TARO_MQTT_URL } }, (error, stdout, stderr) => {
+    if (error) {
+        console.error(`Error executing command: ${error.message}`);
+        return;
+    }
+    if (stderr) {
+        console.error(`Command stderr: ${stderr}`);
+        return;
+    }
+    console.log(`Command stdout: ${stdout}`);
+});

+ 5 - 3
README.md

@@ -86,10 +86,12 @@ npm run pkg
 
 ### 启动
 
-在执行启动命令之前,需要先启动应用的 H5 端开发模式
+构建用于e2e的静态资源文件
 
-- npm run dev:h5
+- node .build/h5_for_local_e2e.js
 
 然后执行测试命令:
 
-- npm run e2e
+- npx cypress open --e2e
+
+最后人工选择要执行的用例