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
Comments
Post a Comment