From fd55441c8c163ac413a1e426ab55a7dd067a0056 Mon Sep 17 00:00:00 2001
From: kobenguyent <kobenguyent@gmail.com>
Date: Thu, 18 Apr 2024 14:38:49 +0200
Subject: [PATCH] fix(playwright): set the record video resolution

---
 lib/helper/Playwright.js       | 14 +++++++++++++-
 test/helper/Playwright_test.js | 12 +++++++-----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js
index b2aeb9d15..374627fab 100644
--- a/lib/helper/Playwright.js
+++ b/lib/helper/Playwright.js
@@ -417,7 +417,14 @@ class Playwright extends Helper {
     }
 
     if (this.options.video) {
-      this.options.recordVideo = { size: parseWindowSize(this.options.windowSize) };
+      // set the video resolution with window size
+      let size = parseWindowSize(this.options.windowSize);
+
+      // if the video resolution is passed, set the record resoultion with that resolution
+      if (this.options.recordVideo && this.options.recordVideo.size) {
+        size = parseWindowSize(this.options.recordVideo.size);
+      }
+      this.options.recordVideo = { size };
     }
     if (this.options.recordVideo && !this.options.recordVideo.dir) {
       this.options.recordVideo.dir = `${global.output_dir}/videos/`;
@@ -3656,6 +3663,11 @@ async function targetCreatedHandler(page) {
 
 function parseWindowSize(windowSize) {
   if (!windowSize) return { width: 800, height: 600 };
+
+  if (windowSize.width && windowSize.height) {
+    return { width: parseInt(windowSize.width, 10), height: parseInt(windowSize.height, 10) };
+  }
+
   const dimensions = windowSize.split('x');
   if (dimensions.length < 2 || windowSize === 'maximize') {
     console.log('Invalid window size, setting window to default values');
diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js
index 05665d807..2dfcb799b 100644
--- a/test/helper/Playwright_test.js
+++ b/test/helper/Playwright_test.js
@@ -1321,8 +1321,6 @@ describe('Playwright - Performance Metrics', () => {
       show: false,
       restart: true,
       browser: 'chromium',
-      trace: true,
-      video: true,
     });
     I._init();
     return I._beforeSuite();
@@ -1332,7 +1330,6 @@ describe('Playwright - Performance Metrics', () => {
     webApiTests.init({
       I, siteUrl,
     });
-    deleteDir(path.join(global.output_dir, 'video'));
     return I._before().then(() => {
       page = I.page;
       browser = I.browser;
@@ -1346,7 +1343,6 @@ describe('Playwright - Performance Metrics', () => {
   it('grabs performance metrics', async () => {
     await I.amOnPage('https://codecept.io');
     const metrics = await I.grabMetrics();
-    console.log(metrics);
     expect(metrics.length).to.greaterThan(0);
     expect(metrics[0].name).to.equal('Timestamp');
   });
@@ -1361,12 +1357,18 @@ describe('Playwright - Video & Trace & HAR', () => {
 
     I = new Playwright({
       url: siteUrl,
-      windowSize: '500x700',
+      windowSize: '300x500',
       show: false,
       restart: true,
       browser: 'chromium',
       trace: true,
       video: true,
+      recordVideo: {
+        size: {
+          width: 400,
+          height: 600,
+        },
+      },
       recordHar: {},
     });
     I._init();