Skip to content

Commit 9bf2d87

Browse files
feat(playwright): enhance logging and directory checks during Playwright initialization
- Added detailed logging for class loader switching and Playwright instance creation. - Implemented checks for browser and temporary directories post-creation. - Enhanced environment setup with additional logging for system properties related to Playwright.
1 parent bdce88c commit 9bf2d87

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/tool/browser/SpringBootPlaywrightInitializer.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,44 @@ public Playwright createPlaywright() {
4646

4747
try {
4848
// Use this class's classloader (LaunchedClassLoader in Spring Boot)
49-
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
50-
return Playwright.create();
49+
ClassLoader newCL = this.getClass().getClassLoader();
50+
log.info("Switching to ClassLoader: {} [{}]", newCL.getClass().getSimpleName(), newCL.toString());
51+
Thread.currentThread().setContextClassLoader(newCL);
52+
53+
log.info("About to call Playwright.create()...");
54+
Playwright playwright = Playwright.create();
55+
log.info("Playwright.create() successful! Instance: {}", playwright.getClass().getName());
56+
57+
// Check what was actually downloaded after creation
58+
log.info("=== Post-Creation Directory Check ===");
59+
String browserPath = System.getProperty("playwright.browsers.path");
60+
String tempDir = System.getProperty("playwright.driver.tmpdir");
61+
62+
try {
63+
Path browsersPath = Paths.get(browserPath);
64+
if (Files.exists(browsersPath)) {
65+
log.info("Browsers directory content:");
66+
Files.walk(browsersPath, 2).forEach(path -> {
67+
if (!path.equals(browsersPath)) {
68+
log.info(" - {}", browsersPath.relativize(path));
69+
}
70+
});
71+
}
72+
73+
// Check temp directory for playwright files
74+
Path tempPath = Paths.get(tempDir);
75+
if (Files.exists(tempPath)) {
76+
Files.list(tempPath)
77+
.filter(path -> path.getFileName().toString().contains("playwright"))
78+
.forEach(path -> log.info("Temp playwright file: {}", path));
79+
}
80+
}
81+
catch (Exception e) {
82+
log.warn("Could not list post-creation directories: {}", e.getMessage());
83+
}
84+
log.info("=====================================");
85+
86+
return playwright;
5187
}
5288
finally {
5389
// Always restore original class loader
@@ -64,6 +100,25 @@ public Playwright createPlaywright() {
64100
* Set up environment properties for Spring Boot
65101
*/
66102
private void setupSpringBootEnvironment() {
103+
// Print detailed class loader information
104+
ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
105+
ClassLoader thisCL = this.getClass().getClassLoader();
106+
107+
log.info("=== Playwright Class Loader Analysis ===");
108+
log.info("Current thread context ClassLoader: {} [{}]", currentCL.getClass().getSimpleName(),
109+
currentCL.toString());
110+
log.info("This class ClassLoader: {} [{}]", thisCL.getClass().getSimpleName(), thisCL.toString());
111+
112+
// Print classpath information
113+
String classPath = System.getProperty("java.class.path");
114+
log.info("Java classpath: {}", classPath);
115+
116+
// Print loader path if exists
117+
String loaderPath = System.getProperty("loader.path");
118+
if (loaderPath != null) {
119+
log.info("Spring Boot loader.path: {}", loaderPath);
120+
}
121+
67122
// Set browser path
68123
String browserPath = System.getProperty("user.home") + "/.cache/ms-playwright";
69124
System.setProperty("playwright.browsers.path", browserPath);
@@ -85,6 +140,37 @@ private void setupSpringBootEnvironment() {
85140
log.info("Spring Boot Playwright environment configured:");
86141
log.info(" - Browser path: {}", browserPath);
87142
log.info(" - Temp directory: {}", tempDir);
143+
144+
// Print all Playwright-related system properties
145+
log.info("=== Playwright Runtime Directories ===");
146+
log.info(" - playwright.browsers.path: {}", System.getProperty("playwright.browsers.path"));
147+
log.info(" - playwright.driver.tmpdir: {}", System.getProperty("playwright.driver.tmpdir"));
148+
log.info(" - PLAYWRIGHT_BROWSERS_PATH env: {}", System.getenv("PLAYWRIGHT_BROWSERS_PATH"));
149+
log.info(" - PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: {}", System.getProperty("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD"));
150+
151+
// Check actual directories
152+
String[] checkPaths = { browserPath, browserPath + "/chromium-*", browserPath + "/firefox-*",
153+
browserPath + "/webkit-*", tempDir + "/playwright-java-*" };
154+
155+
for (String path : checkPaths) {
156+
try {
157+
Path p = Paths.get(path.replace("*", ""));
158+
if (Files.exists(p)) {
159+
log.info(" ✓ Directory exists: {}", path);
160+
if (Files.isDirectory(p)) {
161+
Files.list(p).forEach(subPath -> log.info(" - {}", subPath.getFileName()));
162+
}
163+
}
164+
else {
165+
log.info(" ✗ Directory not found: {}", path);
166+
}
167+
}
168+
catch (Exception e) {
169+
log.warn(" ? Could not check path {}: {}", path, e.getMessage());
170+
}
171+
}
172+
173+
log.info("==========================================");
88174
}
89175

90176
/**

0 commit comments

Comments
 (0)