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;
}
}

- 3,188
- 3
- 20
- 37

- 41
- 3
-
You're disposing of Playwright, so the `IPage` won't be usable when the method returns. – Martin Costello Apr 11 '22 at 13:06
-
ok, then what is the way out? i am unable to return the page. I tried declaring playwright in static static class, public class etc... but no luck. how can i implement Page Object Model (POM) using this? – techjogi Apr 11 '22 at 15:36
-
Here's some example code of my own you can look through. As you can see, the `IPage` ownership is separate to the POM classes: https://github.com/martincostello/dotnet-minimal-api-integration-testing/blob/main/tests/TodoApp.Tests/TodoPage.cs – Martin Costello Apr 12 '22 at 08:04
2 Answers
I've also had this problem with Playwright when implementing a Driver class.
You need to remove the "using" keywork from the beginning of the playwright variable because this will dispose the IPlaywright object and it will not be called.
I've used this for instantiating Playwright in my test
public class Driver : IDisposable
{
private readonly Task<IPage> _page;
private IBrowser? _browser;
public Driver()
{
_page = InitializePlaywright();
}
public IPage Page => _page.Result;
private async Task<IPage> InitializePlaywright()
{
IPlaywright palywright = await Playwright.CreateAsync();
_browser = await palywright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
SlowMo = 800,
Headless = false,
});
return await _browser.NewPageAsync();
}
public void Dispose()
{
_browser?.CloseAsync();
}
}
And to call it in an actual test class along with the POM you can use the following example:
public class CheckIfHomePageWillOpen : Driver
{
public readonly Driver _driver;
public readonly Home _home;
public CheckIfHomePageWillOpen()
{
_driver = new Driver();
_home = new Home(_driver.Page);
}
[Test]
public async Task TestMainPageWillOpen()
{
await _driver.Page.GotoAsync("https://ro.wikipedia.org/wiki/Pagina_principal%C4%83");
await _home.CheckIfMainPageLogoIsDisplayed();
_driver.Dispose();
}
}
Here you can instantiate the driver class and the POM (in my case the POM is the Home page) in the constructor and use them in the actual test. Also you can call the driver.Page in order to access the public filed of the Driver class that contains the InitializePlaywright() async function.

- 49,934
- 160
- 51
- 83

- 1
- 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());
}
}