'Microsoft.Playwright.PlaywrightException:Connection closed (connection disposed) - getting this error using PoM Playwright c#, Visual studio 2022

public async Task Test2() { //IPage page=null;    LoginPage
    lpage = new LoginPage();
    IPage page = await IPage.loginToApllication("john", "admin", "<<applicationLink>>", false);
    await cMgmt.fillCalendarDetails();
}

public class LoginPage {

    public async Task < Ipage > loginToApllication(username, pwd, URL, bHeadLess); {
        using
        var playwright = await playwright.createAsync();
        BrowserTypeLaunchOptions launchoptions = new BrowserTypeLaunchOptions();
        launchoptions.HeadLess = bHeadLess;

        await using
        var browser = await.playwright.Chromium.launchAsync(launchoptions);
        var page = await browser.NewPageAsync();

        await page.GotoAsync(URL);
        await page.TypeAsync("#loginid", username);
        await page.TypeAsync("#pwd", pwd);
        await page.clickAsync("#submit);

        return page;  
    }
}


Solution 1:[1]

I encountered this problem

I think this will solve the problem

public static class CommonActivities
    {
        static Microsoft.Playwright.IBrowser _browser = null;

        static CommonActivities()
        {
            var playWright = Task.Run(() => Playwright.CreateAsync()).Result;
            if (_browser == null)
            {
                _browser = Task.Run(() => playWright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
                {
                    SlowMo = 800,
                    Headless = false,
                })).Result;
            }
        }

        public static async Task GoToSettingsMain(IPage page)
        {
            

            await page.RunAndWaitForNavigationAsync(async () =>
            {
                await page.Locator("#settings i").ClickAsync();
            });
        }


        public static async Task GoToOThmanMain(IPage page)
        {
            

            await page.RunAndWaitForNavigationAsync(async () =>
            {
                await page.Locator("#header >> text=Othman").ClickAsync();
            });
        }

        public static async Task<IPage> LoginToAny()
        {
            //  we might need to re-create the context for each test. 
            var context = await _browser.NewContextAsync();
            var page = context.NewPageAsync().Result;

            

            await page.Locator("#username").ClickAsync();

            await page.Locator("#username").FillAsync("Your Email");


            await page.Locator("[placeholder=\"Password\"]").ClickAsync();

            await page.Locator("[placeholder=\"Password\"]").FillAsync("Your Pass");

            await page.RunAndWaitForNavigationAsync(async () =>
            {
                await page.Locator("[placeholder=\"Password\"]").PressAsync("Enter");
            });


            return page;
        }

     
    }
}

[TestClass] public class Time {

    // AddTime

    [TestMethod]
    public async Task AddTime_AttendanceLocation()
    {
        try
        {
            #region First Command 
            var page = await CommonActivities.LoginToAny();

            await CommonActivities.GoToSettingsMain(page);

            #endregion EndCommand
            
            
           Yoru Code
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Dembele coder