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

Linux下设置PLAYWRIGHT_JAVA_SRC环境变量解决Playwright Trace报错

问题描述

在Eclipse中使用Java运行Playwright Trace时触发报错,错误提示启用源码收集时必须通过PLAYWRIGHT_JAVA_SRC环境变量指定源码根目录。

相关代码如下:

final Browser.NewContextOptions browserContextOptions = new Browser.NewContextOptions().setIgnoreHTTPSErrors(true).setViewportSize(width,height);
context = browser.newContext(browserContextOptions);
//context = browser.newContext(new Browser.NewContextOptions().setViewportSize(width, height));

// Start tracing before creating / navigating a page.
context.tracing().start(new Tracing.StartOptions()
        .setScreenshots(true)
        .setSnapshots(true)
        .setSources(true));
page = context.newPage();

错误栈信息:

[RemoteTestNG] detected TestNG version 7.4.0 FAILED CONFIGURATION:
@BeforeMethod doLaunchBrowser
com.microsoft.playwright.PlaywrightException: Source root directory
must be specified via PLAYWRIGHT_JAVA_SRC environment variable when
source collection is enabled at
com.microsoft.playwright.impl.TracingImpl.startImpl(TracingImpl.java:94)
at
com.microsoft.playwright.impl.TracingImpl.lambda$start$0(TracingImpl.java:68)
at
com.microsoft.playwright.impl.LoggingSupport.lambda$withLogging$0(LoggingSupport.java:36)
at
com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
at
com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:85)
at
com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:35)
at
com.microsoft.playwright.impl.TracingImpl.start(TracingImpl.java:68)
at
com.united_arab_bank.base.BasePage.doLaunchBrowser(BasePage.java:91)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568) at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at
org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at
org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at
org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at
org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:700)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
at
org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at
org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at
org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at
org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at
org.testng.TestRunner.privateRun(TestRunner.java:794) at
org.testng.TestRunner.run(TestRunner.java:596) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:377) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:332) at
org.testng.SuiteRunner.run(SuiteRunner.java:276) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1212) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1134) at
org.testng.TestNG.runSuites(TestNG.java:1063) at
org.testng.TestNG.run(TestNG.java:1031) at
org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

解决方案

方案1:关闭源码收集

如果不需要在Trace中包含Java源码,直接修改代码,将.setSources(true)改为.setSources(false)即可,无需设置环境变量:

final Browser.NewContextOptions browserContextOptions = new Browser.NewContextOptions().setIgnoreHTTPSErrors(true).setViewportSize(width,height);
context = browser.newContext(browserContextOptions);

// Start tracing before creating / navigating a page.
context.tracing().start(new Tracing.StartOptions()
        .setScreenshots(true)
        .setSnapshots(true)
        .setSources(false)); // 关闭源码收集
page = context.newPage();

方案2:配置PLAYWRIGHT_JAVA_SRC环境变量

若需要保留源码收集功能,必须指定项目的源码根目录到该环境变量,以下是Linux系统的配置方式:

临时生效(仅当前终端)

打开终端,执行命令替换为你的实际源码路径(如Maven项目的src/main/java):

export PLAYWRIGHT_JAVA_SRC="/path/to/your/project/src/main/java"

之后在同一终端启动Eclipse或运行测试命令即可生效。

永久生效(所有终端会话)

  1. 编辑用户环境配置文件(Bash用~/.bashrc,Zsh用~/.zshrc):
nano ~/.bashrc
  1. 在文件末尾添加:
export PLAYWRIGHT_JAVA_SRC="/path/to/your/project/src/main/java"
  1. 保存退出(按Ctrl+O回车,再Ctrl+X),执行以下命令让配置立即生效:
source ~/.bashrc

Eclipse内直接配置

不想修改系统环境变量的话,可在Eclipse运行配置中设置:

  1. 右键测试类/套件 → Run As > Run Configurations
  2. 选择对应的TestNG配置,切换到Environment标签页
  3. 点击New,输入变量名PLAYWRIGHT_JAVA_SRC,值为你的源码根目录路径,点击OK
  4. 保存配置后重新运行测试即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 02:01:16