Testing Popup Windows with TypeScript and Playwright

Testing Popup Windows with TypeScript and Playwright

Popup windows are a common feature in modern web applications, and testing them requires a nuanced approach. In this blog post, I’ll use TypeScript and Playwright to test popup windows effectively. This combination provides browser automation capabilities and the benefits of TypeScript’s strong typing.

Writing the Test for a Popup Window

1. Handling Popup Windows

Playwright has a sophisticated way to handle popup windows. Let’s assume our application opens a popup when a certain button is clicked. We need to prepare for the popup event before triggering it:

import { test, expectfrom '@playwright/test'
test('should handle the popup window'async ({ page }) => 

    let popup
    // Prepare for the popup event 
    page.once('popup'asyncnewPage => { popup = newPage; }); 
    // Trigger the popup 
    await page.click('selector-for-button-that-triggers-popup'); 
    // Wait for the popup to appear 
    await expect(popup).toBeTruthy(); 
    // Now you can interact with the popup window 
    // For example, checking its URL 
    await expect(popup).toHaveURL('https://expected-popup-url.com'); 
});

2. Interacting with Elements in the Popup

After catching the popup, you can interact with it like any other page. Suppose you want to fill out a form in the popup:

await popup.fill('input[name="email"]''test@example.com'); 
await popup.click('button[type="submit"]');

3. Asserting Popup Behavior

Assertions play a crucial role in verifying the behaviour of the popup. For instance, if submitting the form in the popup should close it, you can assert that:

await expect(popup).toBeClosed();

Or if it redirects to another page, you can assert the URL change:

await expect(popup).toHaveURL('https://example-after-submit.com');

Tips for Popup Testing

  1. Selectors: Use clear and specific selectors to interact with elements in the popup.
  2. Timing: Be mindful of the timing when dealing with popups. Use await to ensure that the popup has loaded before attempting to interact with it.
  3. Debugging: Utilize Playwright’s built-in debugging tools to step through your tests and understand the popup’s behaviour.
  4. Edge Cases: Consider testing edge cases, like what happens if the user closes the popup manually.

Conclusion

Testing popup windows with TypeScript and Playwright is a powerful approach to ensure the correctness of this interactive element in web applications.

By handling events, interacting with elements, and asserting expected outcomes, you can build a comprehensive test suite that enhances the reliability of your application’s user interface.

Remember, the key to successful testing is understanding the behaviour of the components under test and simulating real-world user interactions.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.