chore(config): 更新Playwright配置以支持自定义浏览器路径

- 修改PlaywrightManager以读取PLAYWRIGHT_BROWSERS_PATH环境变量
- 当环境变量未设置时使用默认路径并记录日志
- 添加跳过浏览器下载的环境变量配置
- 使用CreateOptions设置自定义环境变量- 引入hutool工具类进行字符串判断- 添加必要的导入语句
This commit is contained in:
2025-10-24 16:42:46 +08:00
parent 763444b9e8
commit 55e1df0789
3 changed files with 22 additions and 3 deletions

View File

@@ -1,8 +1,12 @@
package com.fantaibao.config;
import cn.hutool.core.util.StrUtil;
import com.microsoft.playwright.Playwright;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
/**
* Playwright单例管理类
* 用于全局管理和提供Playwright实例
@@ -18,7 +22,18 @@ public class PlaywrightManager {
throw new RuntimeException("请使用getInstance()方法获取实例");
}
try {
this.playwright = Playwright.create();
String playwrightBrowsersPath = System.getenv("PLAYWRIGHT_BROWSERS_PATH");
if (StrUtil.isBlank(playwrightBrowsersPath)) {
log.info("PLAYWRIGHT_BROWSERS_PATH环境变量未设置将使用默认路径");
playwright = Playwright.create();
return;
}
Map<String, String> env = new HashMap<>();
env.put("PLAYWRIGHT_BROWSERS_PATH",playwrightBrowsersPath);
env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1");
Playwright.CreateOptions options = new Playwright.CreateOptions();
options.setEnv(env);
this.playwright = Playwright.create(options);
} catch (Exception e) {
log.error("创建Playwright实例失败", e);
throw new RuntimeException("创建Playwright实例失败", e);