ui自动化执行问题处理

This commit is contained in:
2025-05-28 13:54:58 +08:00
parent 66c627d33a
commit 8636842735
2 changed files with 137 additions and 130 deletions

View File

@@ -207,7 +207,7 @@ public class PerformanceTestController extends BaseController {
// return success(l1);
} catch (Exception e) {
log.error("执行失败!", e);
return error("执行失败!");
return error("执行失败!"+e);
}
} catch (Exception e) {
log.error("修改并执行失败!", e);

View File

@@ -102,44 +102,40 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
// 配置 ChromeOptions
ChromeOptions options = new ChromeOptions();
// 基本设置
// Docker 环境特定配置
options.addArguments("--headless=new"); // 使用新的无头模式
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox"); // 在 Docker 中必需
options.addArguments("--disable-dev-shm-usage"); // 避免 Docker 中的内存问题
options.addArguments("--remote-allow-origins=*");
// 禁用各种功能以提高稳定
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
// 性能优化
options.addArguments("--disable-gpu"); // 在无头模式中禁用 GPU
options.addArguments("--disable-extensions"); // 禁用扩展
options.addArguments("--disable-plugins");
options.addArguments("--disable-software-rasterizer");
options.addArguments("--disable-browser-side-navigation");
options.addArguments("--disable-infobars");
options.addArguments("--disable-notifications");
// 设置窗口大小和内存限制
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--incognito"); // 无痕模式
// 添加性能和稳定性相关的参数
options.addArguments("--disable-background-networking");
options.addArguments("--disable-background-timer-throttling");
options.addArguments("--disable-client-side-phishing-detection");
options.addArguments("--disable-default-apps");
options.addArguments("--disable-hang-monitor");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-prompt-on-repost");
options.addArguments("--disable-sync");
options.addArguments("--metrics-recording-only");
options.addArguments("--no-first-run");
options.addArguments("--safebrowsing-disable-auto-update");
options.addArguments("--password-store=basic");
options.addArguments("--use-mock-keychain");
// 错误和日志处理
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-notifications");
options.addArguments("--log-level=3"); // 最小化日志
options.addArguments("--silent");
// 创建 ChromeDriver
WebDriver driver = new ChromeDriver(options);
// 内存优化
options.addArguments("--aggressive-cache-discard");
options.addArguments("--disable-cache");
options.addArguments("--disable-application-cache");
options.addArguments("--disable-offline-load-stale-cache");
options.addArguments("--disk-cache-size=0");
// 自动化检测绕过
options.addArguments("--disable-blink-features=AutomationControlled");
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = null;
try {
driver = new ChromeDriver(options);
SeleniumUtils seleniumUtils = new SeleniumUtils(driver);
try {
// 3. 执行所有步骤
@@ -216,8 +212,6 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
uiReport.setFaiiRate(
stepsErrorNumber == 0 ? "0%" :
String.format("%.2f%%", (double) stepsErrorNumber / steps.size() * 100)
);
if (1 == uiReport.getStatus()){
uiReport.setStatus(4);
@@ -237,7 +231,20 @@ public class UiSceneStepsServiceImpl implements IUiSceneStepsService {
uiAutomationMapper.updateUiAutomation(uiAutomation1);
//关闭浏览器
seleniumUtils.quit();
if (driver != null) {
try {
driver.quit();
} catch (Exception e) {
log.error("关闭浏览器失败", e);
}
}
}
} catch (SessionNotCreatedException e) {
log.error("ChromeDriver 会话创建失败: {}", e.getMessage());
throw new RuntimeException("浏览器启动失败,请检查 Chrome 和 ChromeDriver 版本是否匹配", e);
} catch (WebDriverException e) {
log.error("WebDriver 异常: {}", e.getMessage());
throw new RuntimeException("浏览器操作异常: " + e.getMessage(), e);
}
return result;
}