5 Common Playwright Exceptions and How to Handle Them

In the world of technology, it can be challenging to stay updated. Especially building a web application today is challenging since asynchronous events are common and make it challenging for automation to function predictably and consistently. This is why so many developers and testers today are struggling with automation testing

Most of the current frameworks and drivers are trying to improve the situation. But considering the demands of modern web apps, it’s a challenging issue. Because of this, the Microsoft team recognized the need for a quick and reliable solution like Playwright to keep up with and handle the execution requirements of modern web development.

Playwright 

Microsoft Playwright is an open-source, cross-browser automation framework for web apps and end-to-end testing. It began as a javascript-based library but has now grown to support Python, Java, and C#—three of the most widely used programming languages. Even though Playwright is a new framework, it offers several quick testing options that most other frameworks do not.

Playwright offers a collection of APIs to developers and testers so they may automate the testing of their web applications across the current three most popular browser engines, Chromium, Firefox, and WebKit. Using the Playwright API testers can create new browser pages, navigate to URLs, and then interact with elements on a page by writing JavaScript code

The framework is compatible with Windows, Linux, and macOS and is made to be quick and reliable. Additionally, it simply needs NodeJS as a precondition and is entirely open-source. The framework includes all of the tools users require to create and run UI tests.

The success of the Playwright can be attributed to numerous factors, including exceptional handling capability. No matter how carefully programmers build a program, there will inevitably be some exceptions in the code. A robust application is determined by how successfully these exceptions are handled. Fortunately, Playwright’s exception handling is simple to understand and apply.

In this article, we will talk about 5 Common Playwright Exceptions and how to deal with them, but first, let’s briefly go over what exceptions are, how to handle them, and why it is crucial to do so.

Exception

Exceptions are unexpected or unanticipated events that happen while a program is being run or at run time, that disrupt the usual flow. Exceptions can be caught and handled by the program. An object is created when an exception occurs within a method referred to as an exception object. It includes details about the exception, including its name, description, and the program’s status at the time the exception occurred.

In other words, an exception occurs whenever an unexpected condition inappropriately disturbs the usual flow of instructions or halts the execution of a program.  Java exceptions are specific events that let the user know that something wrong occurred in the application and that it needs to either fix the problem or close it.

Exception Handling

Responding to undesirable or unexpected events that occur while an application is running is known as exception handling. Without this procedure, exceptions would cause a program’s regular functioning to be disrupted since it deals with these events to prevent the application from crashing. 

One crucial ability a developer of test automation must possess is the ability to handle exceptions (unexpected events) effectively. It handles programming-side exceptions without displaying them to the application’s user. For instance, the application will display a system-generated error message to the user if an exception occurs and is not handled by the programmer. Making errors user-friendly is the goal of exception handling.

However, if the backend is handled by the programmers, then the message will be displayed in plain language rather than the one that the system generates. So that the users may understand the reason for the exception and supply the necessary information to run the program normally the following time.

Exceptions are frequently misinterpreted with errors. But there is a significant distinction between errors and exceptions. Errors signify a situation that cannot be handled in runtime. On the other hand, addressing exceptions is a situation that can and should be addressed by an application while running.

Significant reasons why an exception occurs

Exceptions might happen both during the compilation of the automation code and during the actual execution of the automation code lines. A program’s runtime exceptional events might occur for many different causes. 

  • When attempting to open a deleted or non-existing file.
  • Digit entered an array outside of the allowed range.
  • Incorrect usage of logic or calculation in the code.
  • Introducing invalid user input into the code.
  • Device breakdown.
  • The inability of automation libraries to carry out or finish a desired operation (e.g. locating a web element).
  • Interruption of the network
  • Physical restrictions (out of disc memory), or 
  • Because of other hardware/software problems.

Why handle exceptions

Exceptions must be handled since they interfere with how tests are typically run. Preventing breakage and carrying out the test execution is one of the crucial intentions of managing exceptions. If exceptions are not handled correctly, the framework may encounter unexpected problems at unexpected moments. Because of this, it is challenging for the developers to assess test execution data or troubleshoot and resolve problems.

A statement that ends a sentence is an exception. This means that anytime a code throws an exception, the execution is immediately stopped and the code is suddenly terminated. But, if the exception is handled, the remaining code will be performed in the program’s regular order after the compiler skips just the one line of code where the exception occurs.

In the worst case scenario, if the user’s data is lost and the application breaks while they are performing any crucial tasks. Handling exceptions is crucial to ensuring the user interface is reliable and preventing unexpected application crashes and data loss. A sudden system crash may have several causes, such as faulty or unexpected data input.

Exception handling is crucial because it helps in keeping the program’s usual, desired flow even in the face of unexpected events. Requests may not succeed or programs may crash if exceptions are not handled. Users may find this to be quite annoying, and if it occurs frequently, they may stop using the services.

Classification of Playwright exceptions

A web automation framework called Playwright enables cross-browser testing of apps. The Playwright testing team must manage numerous exceptions during automation.

Exceptions are flaws or interruptions in the program that could lead to abnormal behavior. Exceptions can happen during application execution or compilation. For the program to continue to run naturally or normally, exception management is essential.

Playwright exceptions can be broadly categorized into two types: Checked and Unchecked Exceptions.

Checked exceptions

Checked exceptions are addressed during the actual coding process. Before the code is compiled, these exceptions must be examined and corrected; otherwise, the compiler will raise the exception. As a result, these exceptions are checked during compilation.

Unchecked Exceptions

Unchecked exceptions happen at runtime and can significantly affect how an application flows. As they are thrown during execution, these exceptions need careful study and debugging.

5 Common exceptions in Playwright

Below are some of the most typical Playwright exceptions and the numerous approaches to handling them.

NoSuchElementException

When the locators are unable to locate or access elements on the website or application, this exception occurs. Preferably, the exception happens because the find element’s element locators were chosen incorrectly. Use the wait command to avoid this issue.

TimeoutException

This exception indicates that the execution was unsuccessful because the order was not carried out or finished within the allotted time. As was previously said, waits are utilized to prevent NoSuchElementException. But, if the page components do not load within the allotted wait time, a TimeoutException will be raised. One must determine the average page load time and modify the wait time accordingly to avoid this exception.

ElementNotVisibleException

This kind of exception occurs when a pre-existing DOM element contains a feature set that is hidden or is not visible and is consequently unable to be selected. There are elements present in this environment, but they are hidden and inaccessible. Thus, it is impossible to interact with. The precise cause of this issue must be determined to be handled; it may be related to nested web elements or overlapping web elements.

RuntimeExceptions

The category of unchecked exceptions includes runtime exceptions. It happens when a programmer makes an error in the written code; everything looks good to the compiler, but when the code is executed, it fails because it attempted to access an element of an array that did not exist or because a logical mistake resulted in a method being called with a null value.

NumberFormatException

NumberFormatException may occur if the formatting of any variable or integer is inconsistent. This exception is triggered when a method was unable to convert a string variable with characters into a numeric format.

Handling Playwright Exception  

Below are a few standard ways to handle the above-mentioned exceptions.

Try-catch

The most popular exception-handling technique is the try/catch block technique. The try-and-catch keywords are combined in this approach to catch exceptions. Try denotes the beginning of the block, where an exception code should be placed. Catch is positioned after the try block to manage or resolve the exception. One cannot use the try block alone; it must be followed by either catch or finally. This code is known as “protected code” and cannot be used outside of the Try/Catch block.

Multiple catch blocks

There are numerous forms of exceptions, and a single piece of code may produce multiple exceptions. Every type of Exception is handled independently using a different block of code using several catch blocks. There is no restriction on the number of catch blocks that can be used, and more than two catch blocks may be used.

Throw / Throws

The single exception is thrown inside the block of code using the throw keyword. Throwing custom exceptions and exceptions based on specific criteria within a code block can both benefit from the use of the throw keyword. A programmer uses the Throw keyword to throw an exception to the runtime so that it can be handled explicitly when they want to create an exception in their code.

To declare an exception that might be thrown by the function during code execution, the throws keyword is used in the method signature. The throws keyword in the method signature must be used when a programmer throws an exception without handling it so that the caller program can comprehend the exceptions that are being thrown by the method.

Multiple Exceptions

There is a possibility that a code could have two different exceptions. That’s where multiple exception handling comes into play. One attempt block can be followed by numerous catch blocks in this situation to deal with various exceptions.

Finally

A block of code is written after a try block using the Finally keyword. All of the program’s crucial lines of code that must be executed regardless of whether an exception occurs are contained in a Finally block. Programmers can combine the catch block and the Finally block depending on the situation. It is an optional block that may or may not be written. Usually, it is employed to release resources.

PlaywrightException code generation on the LambdaTest Cloud Grid

Every automation test script must include an exception-handling mechanism. Exceptions must be handled because they prevent programs from running normally and might make deploying production code unpleasant. By managing the Exceptions creatively, one can create efficient and reliable scripts.

The proper handling of exceptions ensures that the automation code runs without interruption. Yet, if done incorrectly, exception handling can result in a program failure, which eventually affects the user experience. Real-time tracking, analysis, and management of exceptions can help you go forward with more confidence.

Custom reporting, which provides information about the exception and its cause, can also be beneficial while addressing exceptions. However, because it necessitates a thorough comprehension of the programming language and test automation tool being used, creating custom reporting libraries is challenging and takes more time. Using a test automation framework like LambdaTest is one of the simplest ways to make it simple to create such libraries.

LambdaTest is a robust, quick, and secure platform with many unmatched features, like automatic test case generation, test reporting mechanisms after each run, the ability to communicate the results without relying on any other tools, parallel testing to speed up test execution speed, and many more.

LambdaTest is a cloud-based platform for testing cross-browser compatibility that enables Playwright automation testing at scale across a cloud of more than 3000 real devices, browsers, and operating systems for both web and mobile applications online, enabling the delivery of higher-quality applications at a faster rate.

Conclusion

In conclusion, it can be said that throughout the execution of any test automation project, exceptions are inevitable. Therefore the above-mentioned common Playwright exceptions should be handled carefully and efficiently to speed up documentation creation and strengthen test coverage.

Share your love
Muhammad Bilal

Muhammad Bilal

Hi, I'm Muhammad Bilal, founder of Tech Spying, that provides useful tech related tips, tricks, hacks, how to and much more stuff. I am a part time content writer and feed this blog with helpful technology skills.