microsoft / playwright-java

Java version of the Playwright testing and automation library
https://playwright.dev/java/
Apache License 2.0
1.07k stars 195 forks source link

[Bug]: Browser object not available when a page is launched using launchPersistentContextOptions #1576

Closed srichhar closed 1 month ago

srichhar commented 1 month ago

Version

1.43.0

Steps to reproduce

If we run the below code in a main function:

try (Playwright playwright = Playwright.create()) {
    LaunchPersistentContextOptions launchPersistentContextOptions =
        new BrowserType.LaunchPersistentContextOptions();
    BrowserType chromium = playwright.chromium();
    Browser browser = chromium.launch();
    BrowserContext context = browser.browserType()
        .launchPersistentContext(Paths.get("userprofiles"), launchPersistentContextOptions);
    com.microsoft.playwright.Page pageMethod1 = context.newPage();
    pageMethod1.navigate("https://google.com");
    log.debug("Is browser object available with launchPersistentContextOptions? : {}", pageMethod1.context().browser()!=null);
}

try (Playwright playwright = Playwright.create()) {
   BrowserType chromium = playwright.chromium();
    Browser browser = chromium.launch();
    BrowserContext context = browser.newContext();
    com.microsoft.playwright.Page pageMethod2 = context.newPage();
    pageMethod2.navigate("https://google.com");
    log.debug("Is browser object available with newContext? : {}", pageMethod2.context().browser()!=null);
}

I get output as:

Is browser object available with launchPersistentContextOptions? : false
Is browser object available with newContext? : true

Expected behavior

Is browser object available with launchPersistentContextOptions? : true
Is browser object available with newContext? : true

Actual behavior

I want to get the executable path and user data directory from the page object. According to API documentation, I should have got it using pageMethod1.context().browser().browserType().executablePath() when the page is launched using launchPersistentContextOptions. However, pageMethod1.context().browser() is always null.

Any help here would be appreciated.

Additional context

No response

Environment

mxschmitt commented 1 month ago

As per the documentation browser() is null when launching a persistent context.

mxschmitt commented 1 month ago

Closing as per above.