Software Testing

Promise in Playwright Automation

Promise in Playwright Automation

A Promise in JavaScript represents the result of an operation that may complete in the future.
In Playwright Automation, many browser operations are asynchronous. For example, opening a webpage, clicking a button, entering text, or waiting for an element may take some time.

A Promise allows JavaScript to handle these asynchronous operations without blocking the entire program.

1. What are the Three States of a Promise?

A JavaScript Promise has three possible states:

Promise State Description
Pending The asynchronous operation is still in progress.
Fulfilled The asynchronous operation completed successfully.
Rejected The asynchronous operation failed.

2. Example of a Promise in Playwright

Consider the following Playwright code:

const { chromium } = require('playwright');

(async () => {

    const browser = await chromium.launch({
        headless: false
    });

    const page = await browser.newPage();

    await page.goto('https://www.google.com');

    console.log('Google website opened');

    await browser.close();

})();

Explanation:

Statement Purpose
chromium.launch() Launches the browser and returns a Promise.
browser.newPage() Creates a new page and returns a Promise.
page.goto() Navigates to the specified URL and returns a Promise.
browser.close() Closes the browser and returns a Promise.

The await keyword tells JavaScript to wait for the Promise to settle before continuing with the next statement inside the asynchronous function.

3. What are async and await?

Playwright uses Promises extensively. We commonly use the JavaScript keywords async and await to handle asynchronous operations in a readable and sequential manner.

Example without await

page.goto('https://www.google.com');

console.log('Website opened');

In this example, JavaScript does not wait for the navigation operation to finish before executing the console statement.

Example with await

await page.goto('https://www.google.com');

console.log('Website opened');

Here, JavaScript waits for the navigation Promise to settle before executing the next statement.

4. Handling Promises using then() and catch()

Promises can also be handled using the .then() and .catch() methods.

page.goto('https://www.google.com')
    .then(() => {
        console.log('Navigation successful');
    })
    .catch((error) => {
        console.log('Navigation failed:', error);
    });
  • .then() handles successful Promise completion.
  • .catch() handles Promise rejection or failure.

5. Difference Between Promise and Async/Await

Promise Async/Await
Represents the eventual result of an asynchronous operation. Provides a readable way to work with Promises.
Can be handled using .then() and .catch(). Uses await and commonly try...catch.
Has pending, fulfilled, and rejected states. Does not change the underlying Promise states.
Used by many Playwright APIs. Commonly used in Playwright automation scripts.

6. Real-Time Promise Example in Playwright Automation

Consider a login test using Playwright:

const { test, expect } = require('@playwright/test');

test('Login Test', async ({ page }) => {

    // Navigate to the application
    await page.goto('https://example.com');

    // Enter username
    await page.locator('#username').fill('admin');

    // Enter password
    await page.locator('#password').fill('admin123');

    // Click Login button
    await page.locator('#login').click();

    // Verify successful login
    await expect(page.locator('#dashboard')).toBeVisible();

});

Explanation:

  1. async declares the test callback as an asynchronous function.
  2. await page.goto() waits for the navigation Promise to settle.
  3. await page.locator().fill() waits for the text-entry operation to complete.
  4. await page.locator().click() waits for the click operation to complete.
  5. await expect() waits for the assertion to pass or time out.

7. Important Points for Playwright Testers

  • Playwright APIs frequently return Promises.
  • Use async to declare an asynchronous function.
  • Use await to wait for a Promise to settle.
  • Use try...catch to handle errors when required.
  • Missing await can lead to unexpected execution order and test failures.

A Promise represents an asynchronous operation whose result will be available in the future. In Playwright Automation, browser operations such as navigation, clicking, filling text fields, and assertions frequently return Promises.

The JavaScript keywords async and await make it easier to manage these asynchronous operations and write readable automation scripts.