Commit 8bcb1d59 authored by youxiaoji's avatar youxiaoji

* [playwright 增加浏览器类型,支持连接远程浏览器]

parent 42060fe1
...@@ -4,35 +4,48 @@ import com.microsoft.playwright.Browser; ...@@ -4,35 +4,48 @@ import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType; import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Playwright; import com.microsoft.playwright.Playwright;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Slf4j @Slf4j
@Configuration @Configuration
public class PlaywrightConfig { public class PlaywrightConfig {
@Value("${pangea.browser.url:ws://localhost:3000}")
private String browserUrl;
@Value("${pangea.browser.type:local}")
private String browserType;
@Bean @Bean
public Playwright playwright() { public Playwright playwright() {
log.info("playwright init success"); log.info("playwright init success");
return Playwright.create(); return Playwright.create();
} }
@Bean @Bean
public Browser browser(Playwright playwright) { public Browser browser(Playwright playwright) {
Browser browser = null; Browser browser = switch (browserType) {
browser = playwright.chromium().launch(new BrowserType.LaunchOptions() case "local" -> playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(true) .setHeadless(true)
.setArgs(java.util.Arrays.asList( .setArgs(java.util.Arrays.asList(
"--no-sandbox", "--no-sandbox",
"--disable-dev-shm-usage", "--disable-dev-shm-usage",
"--disable-gpu", "--disable-gpu",
"--remote-allow-origins=*"))); "--remote-allow-origins=*")));
browser.onDisconnected((tmp) -> { case "remote" -> playwright.chromium().connect(browserUrl);
log.info("浏览器实例已断开连接"); default -> null;
// 发送监听事件 };
tmp.close(); if(browser != null) {
browser.onDisconnected((tmp) -> {
}); log.info("浏览器实例已断开连接");
// 发送监听事件
tmp.close();
});
}
return browser; return browser;
} }
......
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