DEV Community

Cover image for Passing Test Data to onTestEnd Reporter Method in Playwright
samsuthen akbarali
samsuthen akbarali

Posted on

Passing Test Data to onTestEnd Reporter Method in Playwright

Introduction

When working with Playwright for end-to-end testing, it's essential to gather meaningful data during test execution. In certain scenarios, passing test-specific data, such as configurations or test case details, to the onTestEnd reporter method can be beneficial for comprehensive reporting and analysis. This article provides guidance on how to achieve this in Playwright.

The Scenario

Consider a scenario where you have a test suite iterating over a set of records, and you want to pass individual record data to the onTestEnd method for reporting purposes. Here's a simplified example:

Image description

In the onTestEnd method, you need to access the record object for reporting:

Image description

Custom Annotations in Playwright

To pass data from each test to the onTestEnd method, Playwright provides the concept of custom annotations. Annotations are key/value pairs accessible via test.info().annotations. While Playwright annotations typically accept key-value pairs, they don't directly support objects. To overcome this limitation, you can stringify the object before storing it in the annotations.

Here's how you can implement this:

Test Implementation

Image description

In this example, JSON.stringify is used to convert the uiRecord object to a string before storing it in the annotations.

Reporter Implementation

Image description

In the onTestEnd method, the description property of the annotation is parsed back into an object using JSON.parse. Now, you have access to the original uiRecord object for reporting.

Conclusion
By utilizing custom annotations in Playwright, you can efficiently pass test-specific data, even objects, to the onTestEnd reporter method. This approach enhances the reporting capabilities and allows you to gather valuable insights during your test execution.

Top comments (0)