I have a Page Object Model that is handling browser specification but when I tried to assign a new context from browser and assign to page is not working as expected because is an async method this way is not possible to create the object without initialize with a method after the object creation.
Is possible to assing async value in the constructor?
What is the best pattern?
export class AppPage {
readonly browser: Browser;
readonly page: Page;
readonly context: BrowserContext;
public currentPage: Page;
constructor(browser: Browser) {
this.browser = browser;
// Is not possible to assign in the constructor because these are async methods
// this.context = await this.browser.newContext();
// this.page = await context.newPage();
}
async initialize() {
const context = await this.browser.newContext();
const page = await context.newPage();
// Is requiring to call an extra method in order ot initialized
this.currentPage = page;
}
async goto() {
await this.currentPage.goto('https://playwright.dev');
}
}
This is the current implementation is required to initialize the page assingation.
test('test using context with page object model', async ({ browser }) => {
const app = new AppPage(browser);
await app.initialize();
await app.goto();
});