NashTech Insights

Playwright Test Result Reporting and Visualisation Using Grafana

Ankit Lohia
Ankit Lohia
Table of Contents

Introduction

To ensure the calibre of web apps, effective and thorough testing is crucial in today’s fast-paced world of software development. Playwright, a new browser automation tool, has gained popularity because to its capacity to automate web browsers and do end-to-end testing. However, managing and visualising test data may be challenging. In this case, Grafana, a powerful open-source data visualisation and monitoring tool, can be employed. In this blog post, we’ll learn at Playwright test result Reporting using Grafana, providing teams with insightful data on their testing efforts.

Installing and Configuring Grafana

Grafana installation and configuration come first. This includes downloading and installing the Grafana server, creating dashboards, and configuring data sources. Grafana can access a variety of data sources, including databases, APIs, and time series databases like InfluxDB or Prometheus. Based on your architecture, you may choose the ideal data source for storing and retrieving Playwright test results.

Integration of Playwright and Grafana: We need to capture and export test results in a way that is suitable for Grafana’s data sources in order to integrate Playwright and Grafana. Playwright provides a number of methods for generating test results, including JSON, JUnit XML, and HTML. We can leverage Playwright’s built-in functionality or create our own custom scripts to export test results in a format that Grafana can use. Grafana must first be installed and configured. From the official website, you may download the Grafana server and install it by following the directions. You may use the Grafana web interface by going to http://localhost:3000 once Grafana is up and running.

Integrating Playwright with Grafana

We must record and export test results in a format that is compatible with Grafana’s data sources so that Playwright can be integrated with it. Playwright offers a number of tools for producing test results. Here is an illustration of exporting test results in JSON format:


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

test(‘Example Test’, async ({ page }) => {
// Test code goes here

// Capture test results
const testResult = {
test: ‘Example Test’,
status: ‘pass’, // or ‘fail’
duration: 1500, // in milliseconds
};

// Export test results to JSON file
const fs = require(‘fs’);
fs.writeFileSync(‘test-results.json’, JSON.stringify(testResult));
});

Creating Grafana Dashboards

Grafana’s dashboards, which let us visualise and analyse data, are its beating heart. Let’s build a sample dashboard to show the percentage of our Playwright tests that were successful using a pie chart:
Access Grafana by logging in, then go to the dashboard section.
To set up a new panel, click “New Dashboard” and select “Add Query”.
Create a query to retrieve the test results from the JSON file after choosing the right data source.


Example Query:

SELECT
count(*) as total,
sum(if(status = ‘pass’, 1, 0)) as passed,
sum(if(status = ‘fail’, 1, 0)) as failed
FROM
“test-results”


Select the Visualization tab and select Pie Chart.
Configure the chart settings and labels to your liking.
Save the panel and add it to your dashboard.

Adding Alerts and Notifications

Grafana allows you to set alerts based on specific thresholds and conditions. Let’s configure an alert to notify us when the error rate exceeds a certain percentage.
Go to the dashboard area to see the test failure rate.
Click the bell icon to open the alarm settings.
Set the condition to Failed > 5%.
Configure notification channels (email, Slack, etc.) and recipients.
Save the warning and test it by intentionally failing the Playwright test.

Collaborating and Sharing Reports

Grafana provides collaboration features that allow team members to share test reports and work together. You can create user roles and permissions to control access to specific dashboards or data sources. Grafana’s intuitive interface makes it easy for team members to explore test results, share insights, and make data-driven decisions. Leveraging her Grafana for reporting and visualizing Playwright test results helps teams achieve valuable results

References

https://grafana.com/docs/

https://playwright.dev/docs/intro

https://blog.nashtechglobal.com/playwright-an-introduction/

Internal Links

For any references visit at https://blog.nashtechglobal.com/

Ankit Lohia

Ankit Lohia

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: