Playwright with Java - 'TargetClosedError:目标页面、上下文或浏览器已关闭错误

问题描述 投票:0回答:1

我是剧作家新手,有人可以帮助我如何解决此错误吗?

这是基础测试中的浏览器启动方法

public void initBrowser() {
        playwright = Playwright.create();
        String headless = config.headLess;

        boolean isHeadless = Boolean.parseBoolean(headless);
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(isHeadless));
        System.out.println(browser);
        context = browser.newContext();
        System.out.println(context);
        page = context.newPage();
    }

这是基础测试中的closeBrowser方法

public void closeBrowser() {
    context.close();
    browser.close();
    playwright.close();
}

这是钩子类

公共类 Hooks { BasePage basePage = new BasePage();

@Before
public void setup() {
    basePage.initBrowser();
}

@After
public void tearDown() {
    basePage.closeBrowser();
}

}

运行代码时我看到此错误。浏览器打开,但立即关闭,无需运行进一步的步骤。框架是 BDD(cucumber),带有 playwright 和 Java。仅当在 mac 机器上使用 chromium 运行代码时才会出现此问题(我们在 Windows 上运行此代码,一切看起来都很好)。此代码在 Firefox 和 Webkit(mac/windows 均)上也运行良好。

com.microsoft.playwright.impl.BrowserImpl@28276e50
com.microsoft.playwright.impl.BrowserContextImpl@36b0fcd5
      com.microsoft.playwright.PlaywrightException: Error {
  message='Target page, context or browser has been closed
  name='TargetClosedError
  stack='TargetClosedError:Target page, context or browser has been closed
Error
    at captureRawStack (/private/var/folders/y1/hjtdt91x4r55hpcdsg5v_2vh0000gp/T/playwright-java-15087204804003979156/package/lib/utils/stackTrace.js:50:17)
    at LongStandingScope._race (/private/var/folders/y1/hjtdt91x4r55hpcdsg5v_2vh0000gp/T/playwright-java-15087204804003979156/package/lib/utils/manualPromise.js:91:52)
    at LongStandingScope.race (/private/var/folders/y1/hjtdt91x4r55hpcdsg5v_2vh0000gp/T/playwright-java-15087204804003979156/package/lib/utils/manualPromise.js:84:17)
    at BrowserContextDispatcher._handleCommand (/private/var/folders/y1/hjtdt91x4r55hpcdsg5v_2vh0000gp/T/playwright-java-15087204804003979156/package/lib/server/dispatchers/dispatcher.js:96:36)
    at DispatcherConnection.dispatch (/private/var/folders/y1/hjtdt91x4r55hpcdsg5v_2vh0000gp/T/playwright-java-15087204804003979156/package/lib/server/dispatchers/dispatcher.js:362:39)
}
    at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:54)
    at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:120)
    at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:129)
    at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:106)
    at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:101)
    at com.microsoft.playwright.impl.BrowserContextImpl.newPageImpl(BrowserContextImpl.java:419)
    at com.microsoft.playwright.impl.BrowserContextImpl.lambda$newPage$13(BrowserContextImpl.java:412)
    at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
    at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:89)
    at com.microsoft.playwright.impl.BrowserContextImpl.newPage(BrowserContextImpl.java:412)
    at com.microsoft.playwright.impl.BrowserContextImpl.newPage(BrowserContextImpl.java:44)
    at pages.BasePage.initBrowser(BasePage.java:24)
    at hooks.Hooks.setup(Hooks.java:12)
Caused by: com.microsoft.playwright.impl.TargetClosedError: Error {
  message='Target page, context or browser has been closed
  name='TargetClosedError
  stack='TargetClosedError:Target page, context or browser has been closed
Error
java hook playwright bdd playwright-java
1个回答
0
投票

添加最新的剧作家依赖性解决了这个问题

<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright</artifactId>
    <version>1.49.0</version>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.