Turn your manual testers into automation experts! Request a DemoStart testRigor Free

How to Handle TimeoutException in Playwright?

Timeouts are common in the real world. You would have seen timeouts in sports matches.

Similarly, timeouts are a way to break loops in programming. You will usually see this being done by frameworks through exceptions or errors.

One such popular framework is Playwright. It is commonly used for end-to-end testing of web applications, ensuring that they work correctly across different browsers and environments. It’s also used for automating repetitive web tasks, scraping web data, and creating bots for various web interactions. Its ability to handle modern web app features and single-page applications makes it a popular choice among developers and testers.

Let’s take a look at how Playwright takes care of timeouts.

TimeoutException in Playwright

In Playwright, while the concept of a timeout is consistent across different language bindings, the specific exception name can differ based on the programming language used. You might see TimeoutException or TimeoutError.

In all cases, this error or exception is thrown when a Playwright operation is not completed within the specified timeout period.

Reasons for timeouts in Playwright

There could be a number of reasons why your Playwright script timed out. Here are some of them.

Page navigation timeouts

A page may take too long to load or navigate to a new URL, exceeding the specified timeout duration. Similarly, when navigating within iframes or frames, the navigation takes longer than the set timeout.

Element interaction timeouts

The script may be waiting for an element to appear, become clickable, or satisfy other interaction conditions. If this doesn’t happen within a stipulated time, then a timeout may occur.

Wait functions timeouts

If the waitForFunction (in JavaScript) or equivalent methods in other bindings are used to wait for a certain condition to be true on the page, and the condition is not met within the set time, then a timeout exception will occur.

Network activity timeouts

Waiting for network requests to complete, such as with waitForRequest or waitForResponse methods, and the network activity not concluding within the timeout period can again lead to a timeout exception.

Assertions timeouts

In automated testing, when an assertion is waiting for a condition, like text in an element or element visibility, and the condition is not met within the allowed time. You will see a timeout exception.

File download timeouts

When waiting for a file download to complete takes longer than the expected time, you may see a file download timeout.

Dialog or pop-up timeouts

If a script is waiting for a dialog or pop-up to appear or be handled, and it doesn’t occur in the allotted time.

Resolution for timeouts in Playwright

Here are a few ways to manage the timeout issue in Playwright.

Increase timeout duration

If the default timeout isn’t sufficient due to slow network conditions or heavy page content, you can increase the timeout duration for specific actions or globally alter it for the entire session. This will require you to assess your application and make the necessary changes in your test script.

Optimize page load and interaction

Ensure that the web page is optimized for faster loading. On the test script side, interact with elements only after they are ready. Use functions like waitForSelector, waitForLoadState, etc., to wait for elements or the page to be in the desired state.

Retry strategies

Implement retry logic for operations that might occasionally exceed the timeout period. This can be especially useful in environments with varying network conditions.

Handle network issues

If timeouts are related to network problems, consider using service workers or setting up a mock server for testing purposes to reduce dependency on actual network conditions.

Review and optimize selectors

Ensure that your selectors are efficient and do not cause delays. Sometimes, complex or inefficient selectors can slow down the process of finding elements.

Parallel execution

If the timeout is due to a large number of tests or operations, consider running tests in parallel to speed up the overall execution time.

Coding to catch these exceptions

The best way to ensure that your test script is able to perform is to introduce try-catch, multiple ‘catch’ or ‘finally‘ blocks. This way, you can prepare for unwanted exceptions.

Here’s a .NET example of handling this exception.
sing Microsoft.Playwright;
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();

var page = await browser.NewPageAsync();

  await page.ClickAsync("text=Example", new() { Timeout = 100 });

catch (TimeoutException)

Handling timeout exceptions efficiently with testRigor

Playwright offers a good selection of capabilities, yet it falls short when it comes to keeping up with automation testing in the Agile environment. With the inclusion of modern technologies like artificial intelligence and machine learning in test automation, you can find tools that make testing possible for everyone, not just specialists.

One such tool that makes this happen is testRigor. This tool uses generative AI to make sure that test creation is as easy as writing plain English statements. It does not rely on unstable locators like XPath or CSS selectors; it utilizes AI to identify elements. Therefore, you only need to provide the element name text as it is visible on the UI.

For example:
click on button "Save" below "Personal Details"

You don’t need to set any implicit wait for page loads with testRigor. Its intelligent features manage that automatically for you. It captures whether the page is fully loaded and only then executes the next test step. This approach helps avoid most timeout errors related to page loads easily without letting the test execution stop or fail.

With testRigor, users can execute test scripts concurrently across different browsers or platforms, minimizing timeout errors.

testRigor offers many other features. You can check them out over here.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...