Commit fd2f1ecf authored by Cai Wei's avatar Cai Wei

feat(*): 修改测试脚本报告流程

parent adc36511
Pipeline #1329 failed
......@@ -26,8 +26,9 @@ npm run cy:run:prod # 生产环境测试
npm run cy:run:custom # 自定义环境测试
# 使用特定浏览器运行测试
npm run cy:run:chrome # Chrome浏览器
npm run cy:run:firefox # Firefox浏览器
npm run cy:run:electron # Electron浏览器(默认)
npm run cy:run:chrome # Chrome浏览器(需要单独安装)
npm run cy:run:firefox # Firefox浏览器(需要单独安装)
# 运行完整测试流程(执行测试+生成报告+生成汇总报告)
npm run test:full
......
......@@ -5,11 +5,12 @@
"type": "module",
"scripts": {
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:run:dev": "CYPRESS_BASE_URL=http://localhost:3000 cypress run",
"cy:run:staging": "CYPRESS_BASE_URL=https://staging.example.com cypress run",
"cy:run:prod": "CYPRESS_BASE_URL=https://app.example.com cypress run",
"cy:run:custom": "source .env.custom && cypress run",
"cy:run": "cypress run --browser chrome",
"cy:run:dev": "CYPRESS_BASE_URL=http://localhost:3000 cypress run --browser chrome",
"cy:run:staging": "CYPRESS_BASE_URL=https://staging.example.com cypress run --browser chrome",
"cy:run:prod": "CYPRESS_BASE_URL=https://app.example.com cypress run --browser chrome",
"cy:run:custom": "source .env.custom && cypress run --browser chrome",
"cy:run:electron": "cypress run --browser electron",
"cy:run:chrome": "cypress run --browser chrome",
"cy:run:firefox": "cypress run --browser firefox",
"test:all": "npm run cy:run:dev && npm run cy:run:staging && npm run cy:run:prod",
......
@echo off
setlocal enabledelayedexpansion
REM 参数处理
set ENV=%1
if "%ENV%"=="" set ENV=development
set BROWSER=%2
if "%BROWSER%"=="" set BROWSER=chrome
echo Running tests for %ENV% environment with %BROWSER% browser
REM 环境配置
if "%ENV%"=="development" (
set CYPRESS_BASE_URL=http://172.16.20.227/admin/dcTom/
set CYPRESS_REPORT_TITLE=Development Tests
) else if "%ENV%"=="staging" (
set CYPRESS_BASE_URL=http://172.16.20.227/admin/dcTom/
set CYPRESS_REPORT_TITLE=Staging Tests
) else if "%ENV%"=="production" (
set CYPRESS_BASE_URL=http://172.16.20.227/admin/dcTom/
set CYPRESS_REPORT_TITLE=Production Tests
) else (
if "%ENV:~0,4%"=="http" (
set CYPRESS_BASE_URL=%ENV%
set CYPRESS_REPORT_TITLE=Custom URL Tests
echo Using custom URL: %ENV%
) else (
echo Unknown environment: %ENV%
echo Supported environments: development, staging, production
echo Or specify a URL directly
exit /b 1
)
)
echo CYPRESS_BASE_URL: %CYPRESS_BASE_URL%
REM 浏览器选择和 fallback
set FALLBACK_BROWSER=electron
set CHROME_CUSTOM=chrome-custom
if /i "%BROWSER%"=="chrome" (
echo Replacing chrome with %CHROME_CUSTOM%
set BROWSER=%CHROME_CUSTOM%
)
REM 检查浏览器是否可用
npx cypress info | findstr /i "%BROWSER%" >nul
if errorlevel 1 (
echo Browser %BROWSER% not found, falling back to %FALLBACK_BROWSER%
set BROWSER=%FALLBACK_BROWSER%
)
echo Using browser: %BROWSER%
REM 查找测试文件
echo Finding test scripts...
set SPEC_FILES=
for %%f in (cypress\e2e\*.cy.js) do (
if exist "%%f" (
if "!SPEC_FILES!"=="" (
set SPEC_FILES=%%f
) else (
set SPEC_FILES=!SPEC_FILES!,%%f
)
echo - %%f
)
)
REM 创建报告目录
if not exist "cypress\reports" mkdir cypress\reports
if not exist "public\reports" mkdir public\reports
REM 运行测试
if defined SPEC_FILES (
echo Running specified test scripts...
npx cypress run --browser %BROWSER% --spec "%SPEC_FILES%"
) else (
echo No first-level test scripts found, running all tests...
npx cypress run --browser %BROWSER%
)
set TEST_EXIT_CODE=%errorlevel%
echo Test execution completed with exit code: %TEST_EXIT_CODE%
REM 合并和生成报告
echo Merging reports...
npx mochawesome-merge cypress/reports/**/*.json -o public/reports/merged-report.json
echo Generating HTML report...
npx marge public/reports/merged-report.json --reportDir public/reports --inline
echo Generating summary report...
node scripts/generate-summary-report.cjs
echo All reports generated!
echo View summary report: public/reports/summary-report.html
echo View detailed report: public/reports/merged-report.html
echo Test execution finished
@echo off
setlocal enabledelayedexpansion
REM Windows测试执行脚本
REM 用法: run-tests.bat [environment] [browser]
REM 示例: run-tests.bat staging chrome
set ENV=%1
if "%ENV%"=="" set ENV=development
set BROWSER=%2
if "%BROWSER%"=="" set BROWSER=chrome
echo 开始执行 %ENV% 环境的测试,使用 %BROWSER% 浏览器
echo Running tests for %ENV% environment with %BROWSER% browser
REM 根据环境设置变量
if "%ENV%"=="development" (
set CYPRESS_BASE_URL=https://screen.bmetech.com/admin/dctom/
set CYPRESS_REPORT_TITLE=Development Tests
set CYPRESS_REPORT_PAGE_TITLE=开发环境测试报告
) else if "%ENV%"=="dev" (
set CYPRESS_BASE_URL=https://screen.bmetech.com/admin/dctom/
set CYPRESS_REPORT_TITLE=Development Tests
set CYPRESS_REPORT_PAGE_TITLE=开发环境测试报告
) else if "%ENV%"=="staging" (
set CYPRESS_BASE_URL=https://screen.bmetech.com/admin/dctom/
set CYPRESS_REPORT_TITLE=Staging Tests
set CYPRESS_REPORT_PAGE_TITLE=预发布环境测试报告
) else if "%ENV%"=="production" (
set CYPRESS_BASE_URL=https://screen.bmetech.com/admin/dctom/
set CYPRESS_REPORT_TITLE=Production Tests
set CYPRESS_REPORT_PAGE_TITLE=生产环境测试报告
) else if "%ENV%"=="prod" (
set CYPRESS_BASE_URL=https://screen.bmetech.com/admin/dctom/
set CYPRESS_REPORT_TITLE=Production Tests
set CYPRESS_REPORT_PAGE_TITLE=生产环境测试报告
) else if "%ENV%"=="custom" (
REM 加载自定义环境变量
if exist .env.custom (
for /f "tokens=*" %%i in (.env.custom) do set %%i
echo 已加载自定义环境配置
) else (
echo 警告: 未找到 .env.custom 文件
)
) else (
REM 支持直接指定URL
echo %ENV% | findstr /C:"http" >nul
if !errorlevel! equ 0 (
if "%ENV:~0,4%"=="http" (
set CYPRESS_BASE_URL=%ENV%
set CYPRESS_REPORT_TITLE=Custom URL Tests
set CYPRESS_REPORT_PAGE_TITLE=自定义URL测试报告
echo 使用自定义URL: %ENV%
echo Using custom URL: %ENV%
) else (
echo 未知环境: %ENV%
echo 支持的环境: development, staging, production, custom
echo 或者直接指定URL,例如: run-tests.bat https://your-site.com chrome
echo Unknown environment: %ENV%
echo Supported environments: development, staging, production
echo Or specify a URL directly
exit /b 1
)
)
REM 输出环境变量以供调试
echo CYPRESS_BASE_URL: %CYPRESS_BASE_URL%
echo CYPRESS_REPORT_TITLE: %CYPRESS_REPORT_TITLE%
echo CYPRESS_REPORT_PAGE_TITLE: %CYPRESS_REPORT_PAGE_TITLE%
REM 查找并执行cypress/e2e目录下的第一层测试脚本
echo 查找cypress/e2e目录下的第一层测试脚本...
REM 检查浏览器是否可用
echo Checking if browser %BROWSER% is available...
REM 在Windows上,我们简化处理,直接使用electron作为备选
set FALLBACK_BROWSER=electron
echo Finding test scripts...
set SPEC_FILES=
for %%f in (cypress\e2e\*.cy.js) do (
if exist "%%f" (
......@@ -77,65 +51,32 @@ for %%f in (cypress\e2e\*.cy.js) do (
)
)
REM 确保报告目录存在
echo 创建报告目录...
echo Creating report directories...
if not exist "cypress\reports" mkdir cypress\reports
if not exist "public\reports" mkdir public\reports
echo 清理旧的报告文件...
del /q "cypress\reports\*.json" 2>nul
del /q "cypress\reports\*.html" 2>nul
del /q "public\reports\*.json" 2>nul
del /q "public\reports\*.html" 2>nul
if defined SPEC_FILES (
echo 执行指定的测试脚本...
echo 运行命令: npx cypress run --browser %BROWSER% --spec "%SPEC_FILES%"
echo Running specified test scripts...
npx cypress run --browser %BROWSER% --spec "%SPEC_FILES%"
) else (
echo 未找到第一层测试脚本,执行所有测试...
echo No first-level test scripts found, running all tests...
npx cypress run --browser %BROWSER%
)
REM 检查测试是否成功执行
set TEST_EXIT_CODE=%errorlevel%
echo 测试执行完成,退出码: %TEST_EXIT_CODE%
echo Test execution completed with exit code: %TEST_EXIT_CODE%
REM 检查是否有JSON报告文件生成
for /f %%i in ('dir /b /s "cypress\reports\*.json" 2^>nul ^| find /c /v ""') do set JSON_REPORT_COUNT=%%i
echo 找到 %JSON_REPORT_COUNT% 个JSON报告文件
echo Merging reports...
npx mochawesome-merge cypress/reports/**/*.json -o public/reports/merged-report.json
if %JSON_REPORT_COUNT% GTR 0 (
echo 合并报告...
npx mochawesome-merge cypress/reports/**/*.json -o public/reports/merged-report.json
if !errorlevel! neq 0 (
echo 报告合并失败,退出码: !errorlevel!
exit /b 1
)
echo 生成HTML报告...
npx marge public/reports/merged-report.json --reportDir public/reports --inline
if !errorlevel! neq 0 (
echo HTML报告生成失败,退出码: !errorlevel!
exit /b 1
)
echo 生成自定义汇总报告...
node scripts/generate-summary-report.cjs
if !errorlevel! neq 0 (
echo 自定义汇总报告生成失败,退出码: !errorlevel!
exit /b 1
)
echo 所有报告生成完成!
echo 查看汇总报告: public/reports/summary-report.html
echo 查看详细报告: public/reports/merged-report.html
) else (
echo 未找到任何JSON报告文件,跳过报告生成
if %TEST_EXIT_CODE% neq 0 (
echo 测试执行失败,请检查错误信息
exit /b 1
)
)
echo Generating HTML report...
npx marge public/reports/merged-report.json --reportDir public/reports --inline
echo Generating summary report...
node scripts/generate-summary-report.cjs
echo All reports generated!
echo View summary report: public/reports/summary-report.html
echo View detailed report: public/reports/merged-report.html
echo 测试执行完成
\ No newline at end of file
echo Test execution finished
\ No newline at end of file
......@@ -64,6 +64,16 @@ if [[ "$OSTYPE" == "msys"* ]] || [[ "$OSTYPE" == "win32" ]] || [[ "$(uname -s)"
exit 0
fi
# 检查浏览器是否可用
echo "检查浏览器 $BROWSER 是否可用..."
AVAILABLE_BROWSERS=$(npx cypress info | grep "Available browsers" -A 20 | grep -E "^\s*-" | sed 's/^\s*-\s*//' | tr '\n' ' ')
echo "系统可用的浏览器: $AVAILABLE_BROWSERS"
if [[ $AVAILABLE_BROWSERS != *"$BROWSER"* ]]; then
echo "警告: 浏览器 $BROWSER 不可用,使用默认浏览器 electron"
BROWSER="electron"
fi
# 执行测试 - 自动涵盖cypress/e2e下面的第一层测试脚本
echo "查找cypress/e2e目录下的第一层测试脚本..."
SPEC_FILES=""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment