You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何一键自动化执行Selenium测试批处理及邮件通知PowerShell脚本?

嘿,这个需求很常见,咱们可以用几种简单的方法来实现一键执行这三个文件,我给你整理了两个最实用的方案:

方案1:用主批处理文件串联所有步骤

这是最直接的方式,适合习惯用批处理的场景。你只需要新建一个批处理文件(比如命名为full-automation.bat),把三个文件的执行逻辑按顺序写进去就行。

@echo off
:: 关闭命令行的默认输出,让日志更清晰
echo ==============================================
echo 开始执行Selenium自动化测试套件...
echo ==============================================
:: 调用第一个测试批处理,必须用call,否则执行完第一个bat会直接退出主脚本
call .\run-tests.bat

:: 检查上一步是否执行成功(可选,根据你的需求调整)
if %errorlevel% neq 0 (
    echo 测试执行失败,终止流程
    pause
    exit /b 1
)

echo ==============================================
echo 开始生成测试报告...
echo ==============================================
call .\generate-report.bat

if %errorlevel% neq 0 (
    echo 报告生成失败,终止流程
    pause
    exit /b 1
)

echo ==============================================
echo 开始发送测试报告邮件...
echo ==============================================
:: 调用PowerShell脚本,用Bypass绕过执行策略限制
powershell -ExecutionPolicy Bypass -File .\send-report.ps1

if %errorlevel% neq 0 (
    echo 邮件发送失败
    pause
    exit /b 1
)

echo ==============================================
echo 所有自动化步骤执行完成!
echo ==============================================
pause

关键细节说明:

  • 一定要用call命令调用其他批处理,否则主批处理会在执行完第一个子批处理后直接退出,不会继续后续步骤。
  • %errorlevel%用来获取上一步命令的执行状态,0代表成功,非0代表失败,你可以根据需要决定是否终止流程。
  • powershell -ExecutionPolicy Bypass是为了绕过PowerShell默认的执行策略限制,避免因为Restricted策略导致脚本无法运行。
方案2:用PowerShell脚本作为入口

如果你更习惯用PowerShell,也可以写一个主PowerShell脚本(比如full-automation.ps1)来串联所有操作:

Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "开始执行Selenium自动化测试套件..." -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan

# 调用第一个批处理,Wait参数会等待批处理执行完成,NoNewWindow在当前窗口运行
Start-Process -FilePath ".\run-tests.bat" -Wait -NoNewWindow
if ($LASTEXITCODE -ne 0) {
    Write-Host "测试执行失败,终止流程" -ForegroundColor Red
    exit 1
}

Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "开始生成测试报告..." -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan

Start-Process -FilePath ".\generate-report.bat" -Wait -NoNewWindow
if ($LASTEXITCODE -ne 0) {
    Write-Host "报告生成失败,终止流程" -ForegroundColor Red
    exit 1
}

Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "开始发送测试报告邮件..." -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan

# 执行邮件脚本
& ".\send-report.ps1"
if ($LASTEXITCODE -ne 0) {
    Write-Host "邮件发送失败" -ForegroundColor Red
    exit 1
}

Write-Host "==============================================" -ForegroundColor Green
Write-Host "所有自动化步骤执行完成!" -ForegroundColor Green
Write-Host "==============================================" -ForegroundColor Green

额外注意事项:

  • 确保所有文件(主脚本和三个目标文件)都在同一个目录下,或者用绝对路径指定文件位置(比如call "C:\automation\run-tests.bat")。
  • 如果你的PowerShell执行策略限制严格,除了用-ExecutionPolicy Bypass,也可以提前以管理员身份打开PowerShell,执行Set-ExecutionPolicy RemoteSigned(允许本地脚本运行)。
  • 你可以根据实际需求调整错误逻辑,比如即使某一步失败,是否继续执行后续步骤,或者添加失败通知的逻辑。

内容的提问来源于stack exchange,提问作者rooster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:18:55