Skip to content

Commit 4b95b86

Browse files
authored
Create 2025-02-07-automating-qa-with-playwright-and-laravel-workflow.md
1 parent 95ada4f commit 4b95b86

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
---
2+
slug: automating-qa-with-playwright-and-laravel-workflow
3+
title: "Automating QA with Playwright and Laravel Workflow"
4+
authors:
5+
name: Richard
6+
title: Core Team
7+
url: https://github.com/rmcdaniel
8+
image_url: https://github.com/rmcdaniel.png
9+
tags: [playwright, laravel-workflow, automation, qa, testing]
10+
---
11+
12+
Automating QA with Playwright and Laravel Workflow
13+
14+
![captionless image](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*b6eXVs5J3aRNzYAiqnS9Vw.png)
15+
16+
Have you ever spent hours tracking down a frontend bug that only happens in production? When working with web applications, debugging frontend issues can be challenging. Console errors and unexpected UI behaviors often require careful inspection and reproducible test cases. Wouldn’t it be great if you could automate this process, capture errors, and even record a video of the session for later analysis?
17+
18+
With **Playwright** and **Laravel Workflow**, you can achieve just that! In this post, I’ll walk you through an automated workflow that:
19+
20+
* Loads a webpage and captures console errors.
21+
* Records a video of the session.
22+
* Converts the video to an MP4 format for easy sharing.
23+
* Runs seamlessly in a **GitHub Codespace**.
24+
25+
The Stack
26+
=========
27+
28+
* **Playwright**: A powerful browser automation tool for testing web applications.
29+
* **Laravel Workflow**: A durable workflow engine for handling long-running, distributed processes.
30+
* **FFmpeg**: Used to convert Playwright’s WebM recordings to MP4 format.
31+
32+
![captionless image](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*2AcR_sLHGToBWQx-SCSPHA.png)
33+
34+
# 1. Capturing Errors and Video with Playwright
35+
36+
The Playwright script automates a browser session, navigates to a given URL, and logs any console errors. It also records a video of the entire session.
37+
38+
```javascript
39+
import { chromium } from 'playwright';
40+
import path from 'path';
41+
import fs from 'fs';
42+
43+
(async () => {
44+
const url = process.argv[2];
45+
const videoDir = path.resolve('./videos');
46+
47+
if (!fs.existsSync(videoDir)) {
48+
fs.mkdirSync(videoDir, { recursive: true });
49+
}
50+
51+
const browser = await chromium.launch({ args: ['--no-sandbox'] });
52+
const context = await browser.newContext({
53+
recordVideo: { dir: videoDir }
54+
});
55+
56+
const page = await context.newPage();
57+
58+
let errors = [];
59+
60+
page.on('console', msg => {
61+
if (msg.type() === 'error') {
62+
errors.push(msg.text());
63+
}
64+
});
65+
66+
try {
67+
await page.goto(url, { waitUntil: 'networkidle', timeout: 10000 });
68+
} catch (error) {
69+
errors.push(`Page load error: ${error.message}`);
70+
}
71+
const video = await page.video().path();
72+
73+
await browser.close();
74+
75+
console.log(JSON.stringify({ errors, video }));
76+
})();
77+
```
78+
79+
# 2. Running the Workflow
80+
81+
A Laravel console command (`php artisan app:playwright`) starts the workflow which:
82+
83+
* Runs the Playwright script and collects errors.
84+
* Converts the video from `.webm` to `.mp4` using FFmpeg.
85+
* Returns the errors and the final video file path.
86+
87+
```php
88+
namespace App\Console\Commands;
89+
90+
use App\Workflows\Playwright\CheckConsoleErrorsWorkflow;
91+
use Illuminate\Console\Command;
92+
use Workflow\WorkflowStub;
93+
94+
class Playwright extends Command
95+
{
96+
protected $signature = 'app:playwright';
97+
protected $description = 'Runs a playwright workflow';
98+
public function handle()
99+
{
100+
$workflow = WorkflowStub::make(CheckConsoleErrorsWorkflow::class);
101+
$workflow->start('https://example.com');
102+
while ($workflow->running());
103+
$this->info($workflow->output()['mp4']);
104+
}
105+
}
106+
```
107+
108+
# 3. The Workflow
109+
110+
```php
111+
namespace App\Workflows\Playwright;
112+
113+
use Workflow\ActivityStub;
114+
use Workflow\Workflow;
115+
116+
class CheckConsoleErrorsWorkflow extends Workflow
117+
{
118+
public function execute(string $url)
119+
{
120+
$result = yield ActivityStub::make(CheckConsoleErrorsActivity::class, $url);
121+
$mp4 = yield ActivityStub::make(ConvertVideoActivity::class, $result['video']);
122+
return [ 'errors' => $result['errors'],
123+
'mp4' => $mp4,
124+
];
125+
}
126+
}
127+
```
128+
129+
# 4. Running Playwright
130+
131+
```php
132+
namespace App\Workflows\Playwright;
133+
134+
use Illuminate\Support\Facades\Process;
135+
use Workflow\Activity;
136+
137+
class CheckConsoleErrorsActivity extends Activity
138+
{
139+
public function execute(string $url)
140+
{
141+
$result = Process::run([
142+
'node', base_path('playwright-script.js'), $url
143+
])->throw();
144+
return json_decode($result->output(), true);
145+
}
146+
}
147+
```
148+
149+
# 5. Video Conversion with FFmpeg
150+
151+
The Playwright recording is stored in WebM format, but we need an MP4 for wider compatibility. Laravel Workflow runs this process asynchronously.
152+
153+
```php
154+
namespace App\Workflows\Playwright;
155+
156+
use Illuminate\Support\Facades\Process;
157+
use Workflow\Activity;
158+
159+
class ConvertVideoActivity extends Activity
160+
{
161+
public function execute(string $webm)
162+
{
163+
$mp4 = str_replace('.webm', '.mp4', $webm);
164+
Process::run([
165+
'ffmpeg', '-i', $webm, '-c:v', 'libx264', '-preset', 'fast', '-crf', '23', '-c:a', 'aac', '-b:a', '128k', $mp4
166+
])->throw();
167+
unlink($webm);
168+
return $mp4;
169+
}
170+
}
171+
```
172+
173+
## Try It Out in a GitHub Codespace 🚀
174+
175+
You don’t need to set up anything on your local machine. Everything is already configured in the **Laravel Workflow Sample App**.
176+
177+
# Steps to Run the Playwright Workflow
178+
179+
* Open the **Laravel Workflow Sample App** on GitHub: [laravel-workflow/sample-app](https://github.com/laravel-workflow/sample-app)
180+
* Click **“Create codespace on main”** to start a pre-configured development environment.
181+
182+
![captionless image](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*063hPvkrvDQP6gU-VYb0Ug.png)
183+
184+
* Once the Codespace is ready, run the following commands in the terminal:
185+
186+
```bash
187+
php artisan migrate
188+
php artisan queue:work
189+
```
190+
191+
* Then open a second terminal and run this command:
192+
193+
```bash
194+
php artisan app:playwright
195+
```
196+
197+
That’s it! The workflow will execute, capture console errors, record a video, and convert it to MP4. You can find the video in the videos folder. Take a look at the sample app’s README.md for more information on other workflows and how to view the Waterline UI.
198+
199+
# Conclusion
200+
201+
By integrating Playwright with Laravel Workflow, we’ve automated frontend error detection and debugging. This setup allows teams to quickly identify and resolve issues, all while leveraging Laravel’s queue system to run tasks asynchronously.
202+
203+
## 🔗 **Next Steps:**
204+
205+
* Check out the [Laravel Workflow repo](https://github.com/laravel-workflow/laravel-workflow) on GitHub.
206+
* Explore more workflows in the [sample app](https://github.com/laravel-workflow/sample-app).
207+
* Join the community and share your workflows!
208+
209+
Happy automating! 🚀

0 commit comments

Comments
 (0)