Slack Notifications for Playwright Test Results

✅ Slack Notifications for Playwright Test Results

🎯 Goal:

Send a message to a Slack channel when your Playwright tests pass or fail.


πŸ›  Prerequisites

Node.js project using Playwright


A Slack webhook URL


Optionally, a CI tool (like GitHub Actions, Jenkins, or GitLab CI)


πŸ”§ Step 1: Get Your Slack Webhook URL

Go to your Slack workspace.


Create a new Incoming Webhook.


Choose the channel to post in.


Copy the generated Webhook URL (e.g., https://hooks.slack.com/services/...).


πŸ“¦ Step 2: Install a Request Package (Optional)

If you don't want to use Node's native https, you can install a package like axios:


bash

Copy

Edit

npm install axios

πŸ“„ Step 3: Create a Slack Notification Script

Create a file called notify-slack.js:


js

Copy

Edit

const axios = require('axios');


const webhookUrl = 'https://hooks.slack.com/services/your/webhook/url';

const status = process.argv[2]; // pass 'passed' or 'failed'


const message = {

  text: `πŸ§ͺ Playwright Tests *${status.toUpperCase()}*!`,

  icon_emoji: status === 'passed' ? ':white_check_mark:' : ':x:',

  username: 'PlaywrightBot'

};


axios.post(webhookUrl, message)

  .then(() => console.log('Slack notification sent.'))

  .catch(err => console.error('Error sending Slack message:', err));

πŸ§ͺ Step 4: Modify Your Test Script

Update your package.json test command to use the Slack script based on the result:


json

Copy

Edit

"scripts": {

  "test": "npx playwright test",

  "test:notify": "npm test && node notify-slack.js passed || node notify-slack.js failed"

}

This command:


Runs your tests


Sends a "passed" message if tests succeed


Sends a "failed" message if any test fails


πŸ€– Step 5: (Optional) Run in CI/CD

In GitHub Actions, for example:


yaml

Copy

Edit

- name: Run Playwright Tests

  run: npm run test:notify

✅ Result in Slack

You’ll see messages like:


Copy

Edit

πŸ§ͺ Playwright Tests PASSED!

or


Copy

Edit

πŸ§ͺ Playwright Tests FAILED!

With appropriate emojis and labels to alert your team quickly. 

Learn Playwright Training Course in Hyderabad

Read More

Using Playwright in a Kubernetes Environment

Using Playwright with AWS Lambda

Running Playwright Tests on BrowserStack

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know