0

I'm creating a framework with Playwright, Java and testNG. I'm initialising driver class where the browser is initialised and have wrapper methods where the playwright functions are defined. In test class where the test is triggered.

I'm getting playwright connection closed error without launching the browser

Driver initialisation class

public class DriverFactory {
public static ThreadLocal<Page> currentPage = new ThreadLocal<>();
public Page page = null;
private PropertyReader propertyReader = new PropertyReader();
Environment environment;

    public DriverFactory() {
        environment = propertyReader.getEnvironment();
    }

public Page getCurrentPage() {
    page = currentPage.get();
    if(page!=null) {
        return page;
    } else {
        return null;
    }
}
public void initilizeBrowser(){
    currentPage.set(getPage(environment.getPlatform()));
}

public Page getPage(String browserName) {
    try (Playwright playwright = Playwright.create()) {
        Browser browser = null;
        if (browserName.equalsIgnoreCase("Chrome")) {
            browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
            page = browser.newPage();
        } else if (browserName.equalsIgnoreCase("Firefox")) {
            browser = playwright.firefox().launch();
            page = browser.newPage();
        } else if (browserName.equalsIgnoreCase("edge")) {
            browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge"));
            page = browser.newPage();
        } else if (browserName.equalsIgnoreCase("safari")) {
            browser = playwright.webkit().launch();
            page = browser.newPage();
        }
    }
    System.out.println("Page ID "+page);
    return page;
}
}

Wrapper Methods

public class WrapperMethods {

private DriverFactory driverFactory;
public WrapperMethods() {
    driverFactory = new DriverFactory();
}
private Page page;

public void openApplication(String url) throws InterruptedException {
    page = driverFactory.getCurrentPage();
    System.out.println(page.toString());
    page.navigate(url);
    Thread.sleep(500000);
}

}

Test class

public class DesktopTest extends BaseTest {
    private WrapperMethods wrapper = new WrapperMethods();
    @Test
    public void navigateToWebPage() throws InterruptedException {
        wrapper.openApplication("https://www.youtube.com");
    }
}

Error faced

**

Page ID com.microsoft.playwright.impl.PageImpl@22b53226
com.microsoft.playwright.impl.PageImpl@22b53226

**

com.microsoft.playwright.PlaywrightException: Playwright connection closed

    at com.microsoft.playwright.impl.PipeTransport.send(PipeTransport.java:50)
    at com.microsoft.playwright.impl.Connection.internalSendMessage(Connection.java:164)
    at com.microsoft.playwright.impl.Connection.sendMessageAsync(Connection.java:130)
    at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:126)
    at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:102)
    at com.microsoft.playwright.impl.FrameImpl.navigateImpl(FrameImpl.java:463)
    at com.microsoft.playwright.impl.PageImpl.lambda$navigate$45(PageImpl.java:836)
    at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
    at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:87)
    at com.microsoft.playwright.impl.PageImpl.navigate(PageImpl.java:836)
    at com.microsoft.playwright.impl.PageImpl.navigate(PageImpl.java:42)
    at com.microsoft.playwright.Page.navigate(Page.java:5282)
    at org.redwind.autoTest.orca.utils.WrapperMethods.openApplication(WrapperMethods.java:18)
    at org.redwind.autoTest.orca.testContainer.DesktopTest.navigateToWebPage(DesktopTest.java:12)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:848)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1114)
    at org.testng.TestNG.run(TestNG.java:1082)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)
Deepak_Mahalingam
  • 454
  • 1
  • 9
  • 21

1 Answers1

0

I don't see a call to the initilizeBrowser() method that calls the getPage() method that is initialising the page. Since, the page is not initialised the connection seems to be closing before launching the browser

  • Hello, could you please share the code how it should look like. – Endery Aug 25 '23 at 06:50
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 06:50
  • initilizeBrowser() is called in basePage and issue was not there, now issue is fixed thanks – Deepak_Mahalingam Aug 28 '23 at 19:36
  • Can you share what was the fix ? – akshat narang Aug 28 '23 at 20:03