Executing asynchronous methods in a Playwright implementation: A guide on constructor assignment

My current implementation uses a Page Object Model to handle browser specification. However, I encountered an issue where assigning a new context from the browser and then assigning it to the page does not work as expected due to the asynchronous nature of the method. This makes it impossible to create the object without initializing it with a method after its creation.

I am wondering if it is possible to assign an async value in the constructor or if there is a better pattern to follow?

export class AppPage {
  readonly browser: Browser;
  readonly page: Page;
  readonly context: BrowserContext;

  public currentPage: Page;


  constructor(browser: Browser) {
    this.browser = browser;
    // It is not feasible to assign in the constructor as 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();
    // An extra method call is required for initialization
    this.currentPage = page;
  }

  async goto() {
    await this.currentPage.goto('https://playwright.dev');
  }
}

The current implementation requires initializing the page assignment.

test('test using context with page object model', async ({ browser }) => {
  const app = new AppPage(browser);
  await app.initialize();

  await app.goto();
});

Check out the live sample on Stackblitz.

Answer №1

To effectively handle this situation, one method is to have the constructor store a promise for the initialization process and then ensure that every other method awaits this promise before executing. Consequently, instead of having context and currentPage as properties of the instance, they become the resolved values of the aforementioned promise. Although they can be retrieved from the promise multiple times, it must always be done asynchronously.

export class AppPage {
  readonly browser: Browser;

  constructor(browser: Browser) {
    this.browser = browser;
    this.ready = this.initialize();
  }

  async initialize() {
    const context = await this.browser.newContext();
    const page = await context.newPage();
    return {context, page};
  }

  async goto() {
    const {page} = await this.ready;
    await page.goto('https://playwright.dev');
  }

  async close() {
    const {context} = await this.ready;
    await context.close();
  }
}

The test script no longer needs to include the call to initialize:

test('test using context with page object model', async ({ browser }) => {
  const app = new AppPage(browser);
  await app.goto();
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing HTML from triggering JavaScript when loaded inside a <div> with a script?

I'm working on creating a collapsible menu that I can easily customize on any page without the use of iframes. As someone new to web design, I have knowledge of CSS and HTML but I am currently learning JavaScript with limited experience in jQuery or A ...

Methods for showing Internet Explorer not supported in Angular without needing to import polyfills

Due to the deprecation of IE in Angular 12, I need to inform users to switch to a supported browser by displaying a static warning on my page. To achieve this, I have implemented a simple snippet in the index.html file that appends a CSS class to the body ...

Error encountered while running npm build: Typescript issue within plotly.js/index.d.ts

Trying to implement this code snippet: import createPlotlyComponent from 'react-plotly.js/factory'; const Plot = createPlotlyComponent(window.Plotly); in my React project implemented in TypeScript. Encountered a TypeScript error during npm run ...

What could be the reason for my component not getting the asynchronous data?

Currently, I am delving into the intricacies of React and have been following a tutorial that covers creating components, passing props, setting state, and querying an API using useEffect(). Feeling confident in my understanding up to this point, I decided ...

The nodejs server failed to respond to my request, displaying "undefined" instead

I have encountered some challenges while hosting my website on Firebase and Heroku Here are the issues I am facing: Initially, I am encountering CORS errors when trying to post data from a Firebase hosted URL to a server hosted on Heroku Even after re ...

`To filter out JSON data that does not exist in Javascript, follow these steps:``

Utilizing JavaScript Fetch to retrieve JSON data, I am aiming to present the information in a well-arranged HTML layout. I encountered challenges when attempting to process certain content. The majority of the data objects I am parsing include images that ...

Fetching data from the server using Angular and parsing it as JSON

Can anyone provide some insight on the best way to use jsonObjects in ng repeat? Here is my code: This is the response I get from PHP: die(json_encode(array('sts'=>'success', 'title'=>'*****', 'msg' ...

Adjustable Material UI Switch that reflects the value, yet remains changeable

I am facing a challenge with integrating a Material UI switch (using Typescript) to showcase a status value (active/inactive) on my edit page, and making it toggleable to change the status. I have been able to either display the value through the switch OR ...

Is it possible to modify this code to accept multiple IDs at once?

I'm attempting to create a form in JavaScript where, upon entering the necessary details and clicking submit, the user's email client opens with the information pre-filled for easy sending. However, I am facing challenges as my code involves mult ...

How can I create a form in Django where selecting a drop-down option triggers an automatic submission?

I'm attempting to create a form that can pre-fill fields based on user selection from a drop-down menu. To kick things off, I want the form to automatically submit when an option is chosen from the drop-down list. Here is the code snippet I have been ...

"Encountered an unexpected token when using an object parameter in a React function

I am facing an issue with a function I have implemented in React Native. const HandleConfirmApplication = async ( opts: { isInvite: boolean, confirmationCheckValues: () => any, confirmPopUp: () => any, loadingApply: () => any, ...

Shining a component (or persona) however essentially duplicate a distinct term

Is it possible to highlight an element or word, but still copy a different word when hitting ctrl+c? For example, imagine I have an emoji represented by: Original text: :heart: Output in HTML: <span background={...logic here}></span> I am ...

Is it possible to delete a <div> tag based on screen size using jQuery or JavaScript?

Hello, I was curious if it's possible to dynamically remove a specific div tag using jQuery or JavaScript when the screen reaches a certain size, for example 500px. ...

Tips on utilizing variables instead of static values when calling objects in JavaScript

How can I make something like this work? I tried putting the variable in [], but it didn't work. Can someone please help me out with this? Thank you. const obj = { car : "a" , bus: "b" }; const x = "car" ; ...

ReactJS sidebar navigation functionality

I have a fixed sidebar with icons for navigating to different pages. When an icon is clicked, a secondary menu slides out. However, the issue is that when another icon is selected, the menu slides back in instead of switching or staying out. Additionally, ...

JavaScript: Retrieving the names of children within a <div> element

Within my structure setup, there is a tower div with inner elements like this: <div class="tower"> <div class="E0">abc</div> <div class="GU">123</di </div> The challenge I am facing is that I need to access the in ...

Using Jquery to handle input data in a form

Using jQuery, I have set up an AJAX function to fetch data from a database in real-time as the user fills out a form with 5 input fields. Currently, my code looks like this: $("#searchtype, #searchtext, #searchtag, #daterangefrom, #daterangeto").on("load ...

"Error encountered: Unable to resolve dependency tree" message appears when attempting to run npm install

Encountering dependency errors while trying to execute the npm install command for my Angular application. As a newcomer to TypeScript and Angular, I'm unsure of the next steps to take. Any suggestions? Attempted solutions include clearing the npm ca ...

Transforming Color with JQuery on the Fly

Check out this Jquery script that gradually changes the color of an object from (0, 0, 0) to (255, 255, 0). $(document).ready(function(){ var r = 0; var g = 0; changeColor(r, g); }); function changeColor(r, g){ var newColor = "(" + r + ", ...

In Angular 16, allow only the row that corresponds to the clicked EDIT button to remain enabled, while disabling

Exploring Angular and seeking guidance on a specific task. I currently have a table structured like this: https://i.stack.imgur.com/0u5GX.png This code is used to populate the table: <tbody> <tr *ngFor="let cus of customers;" [ngClass ...