From cafad83c55a530cbe06fb955beae483fb0b36d52 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 28 Mar 2024 06:00:54 +0100 Subject: [PATCH 1/6] Deletes or rename `debug.log` file before tests --- src/support/hooks.ts | 31 +++++++++++++++++++--- utils/commands.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/support/hooks.ts b/src/support/hooks.ts index 854b2f8..f225565 100644 --- a/src/support/hooks.ts +++ b/src/support/hooks.ts @@ -22,16 +22,23 @@ import { PageUtils } from "../../utils/page-utils"; import { batchUpdateVRTestUrl } from "../../utils/helpers"; import { deleteFolder } from "../../utils/helpers"; import backstop from 'backstopjs'; -import { SCENARIO_URLS } from "../../config/wp.config"; +import {SCENARIO_URLS, WP_SSH_ROOT_DIR,} from "../../config/wp.config"; import { After, AfterAll, Before, BeforeAll, Status, setDefaultTimeout } from "@cucumber/cucumber"; -// import wp, {cp, deleteTransient, generateUsers, resetWP, rm, unzip} from "../../utils/commands"; +import {rename, exists, rm} from "../../utils/commands"; // import {configurations, getWPDir} from "../../utils/configurations"; /** * The Playwright Chromium browser instance used for testing. */ let browser: ChromiumBrowser; + +/** + * Stores the name of the previous test scenario. + * It is initially undefined until it is assigned a value in the `After` hook. + * @type {string} + */ +let previousScenarioName: string; /** * Sets the default timeout for Playwright tests. * If PWDEBUG environment variable is set, timeout is infinite (-1). @@ -42,6 +49,13 @@ setDefaultTimeout(process.env.PWDEBUG ? -1 : 60 * 10000); * Before all tests, launches the Chromium browser. */ BeforeAll(async function (this: ICustomWorld) { + const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; + const debugLogExists = await exists(debugLogPath); + + if (debugLogExists) { + await rm(debugLogPath); + } + await deleteFolder('./backstop_data/bitmaps_test'); browser = await chromium.launch({ headless: false }); @@ -77,12 +91,21 @@ BeforeAll(async function (this: ICustomWorld) { urls: SCENARIO_URLS }); } + }); /** * Before each test scenario without the @setup tag, performs setup tasks. */ Before({tags: 'not @setup'}, async function (this: ICustomWorld) { + const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; + const debugLogExists = await exists(debugLogPath); + + if (debugLogExists && previousScenarioName) { + const newDebugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug-${previousScenarioName}.log`; + await rename(debugLogPath, newDebugLogPath); + } + /** * To uncomment during implementation of cli */ @@ -139,7 +162,7 @@ Before({tags: 'not @setup'}, async function (this: ICustomWorld) { this.page = await this.context.newPage(); this.sections = new Sections(this.page, pluginSelectors); this.utils = new PageUtils(this.page, this.sections); - + /** * To uncomment during implementation of cli */ @@ -167,6 +190,8 @@ Before({tags: '@setup'}, async function(this: ICustomWorld) { * After each test scenario, performs cleanup tasks and captures screenshots and videos in case of failure. */ After(async function (this: ICustomWorld, { pickle, result }) { + previousScenarioName = pickle.name + if (result?.status == Status.FAILED) { await this.utils.createScreenShot(this, pickle); } diff --git a/utils/commands.ts b/utils/commands.ts index 559cb23..e8efb41 100644 --- a/utils/commands.ts +++ b/utils/commands.ts @@ -110,6 +110,69 @@ export async function cp(origin: string, destination: string): Promise { }); } +/** + * Renames a file on the host. + * + * @function + * @name rename + * @async + * @param {string} oldName - The current name of the file. + * @param {string} newName - The new name for the file. + * @returns {Promise} - A Promise that resolves when the rename operation is completed. + */ +export async function rename(oldName: string, newName: string): Promise { + if(configurations.type === ServerType.docker) { + await exec(`docker exec -T ${configurations.docker.container} mv ${oldName} ${newName}`, { + cwd: configurations.rootDir, + async: false + }); + + return; + } + + if(configurations.type === ServerType.external) { + console.log(`ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "mv ${oldName} ${newName}"`); + await exec(`ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "mv ${oldName} ${newName}"`); + return; + } + + exec(`mv ${oldName} ${newName}`, { + cwd: configurations.rootDir, + async: false + }); +} + +/** + * Checks if a file exists on the server. + * + * @function + * @name exists + * @async + * @param {string} filePath - The path of the file to check. + * @returns {Promise} - A Promise that resolves with true if the file exists, false otherwise. + */ +export async function exists(filePath: string): Promise { + let command: string; + + if(configurations.type === ServerType.docker) { + command = `docker exec -T ${configurations.docker.container} test -f ${filePath}`; + } else if(configurations.type === ServerType.external) { + command = `ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "test -f ${filePath}"`; + } else { + command = `test -f ${filePath}`; + } + + try { + await exec(command, { + cwd: configurations.rootDir, + async: false + }); + return true; + } catch (error) { + return false; + } +} + /** * Unzips a compressed file to the specified destination on the server. * From 9add1abef0908f54ff652845e03a2c71e9578ce2 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 28 Mar 2024 10:53:06 +0100 Subject: [PATCH 2/6] Deletes all .log files before tests --- src/support/hooks.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/support/hooks.ts b/src/support/hooks.ts index f225565..3f9e20f 100644 --- a/src/support/hooks.ts +++ b/src/support/hooks.ts @@ -49,12 +49,8 @@ setDefaultTimeout(process.env.PWDEBUG ? -1 : 60 * 10000); * Before all tests, launches the Chromium browser. */ BeforeAll(async function (this: ICustomWorld) { - const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; - const debugLogExists = await exists(debugLogPath); - - if (debugLogExists) { - await rm(debugLogPath); - } + const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/*.log`; + await rm(debugLogPath); await deleteFolder('./backstop_data/bitmaps_test'); browser = await chromium.launch({ headless: false }); From 8741f7d7b554b0987c571aeadf2e0413175cacf4 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 29 Mar 2024 05:13:50 +0100 Subject: [PATCH 3/6] Replace check if debug.log exists by ssh --- src/support/steps/general.ts | 14 +++++++++----- utils/commands.ts | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/support/steps/general.ts b/src/support/steps/general.ts index 89fd6e4..3106a04 100644 --- a/src/support/steps/general.ts +++ b/src/support/steps/general.ts @@ -18,6 +18,8 @@ import { WP_BASE_URL } from '../../../config/wp.config'; import { createReference, compareReference } from "../../../utils/helpers"; import type { Section } from "../../../utils/types"; import { Page } from '@playwright/test'; +import { exists } from "../../../utils/commands"; +import { WP_SSH_ROOT_DIR } from "../../../config/wp.config"; /** * Executes the step to log in. @@ -230,11 +232,13 @@ Then('I should see {string}', async function (this: ICustomWorld, text) { * Executes the step to check for errors in debug.log. */ Then('I must not see any error in debug.log', async function (this: ICustomWorld){ - // Goto WP Rocket dashboard - await this.utils.gotoWpr(); - await this.page.waitForLoadState('load', { timeout: 30000 }); - // Assert that there is no related error in debug.log - await expect(this.page.locator('#wpr_debug_log_notice')).toBeHidden(); + // Define the path to debug.log + const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; + + // Check if debug.log exists + const debugLogExists = await exists(debugLogPath); + + await expect(debugLogExists).toBe(false); }); /** diff --git a/utils/commands.ts b/utils/commands.ts index e8efb41..b56daf5 100644 --- a/utils/commands.ts +++ b/utils/commands.ts @@ -155,19 +155,19 @@ export async function exists(filePath: string): Promise { let command: string; if(configurations.type === ServerType.docker) { - command = `docker exec -T ${configurations.docker.container} test -f ${filePath}`; + command = `docker exec -T ${configurations.docker.container} test -f ${filePath}; echo $?`; } else if(configurations.type === ServerType.external) { - command = `ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "test -f ${filePath}"`; + command = `ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} 'test -f ${filePath}; echo $?'`; } else { - command = `test -f ${filePath}`; + command = `test -f ${filePath}; echo $?`; } try { - await exec(command, { + const result = await exec(command, { cwd: configurations.rootDir, async: false }); - return true; + return result.stdout.trim() === '0'; } catch (error) { return false; } @@ -224,6 +224,19 @@ export async function activatePlugin(name: string): Promise { await wp(`plugin activate ${name}`) } +/** + * Executes a SQL query on the WordPress database using WP-CLI. + * + * @function + * @name query + * @async + * @param {string} query - The SQL query to be executed. + * @returns {Promise} - A Promise that resolves when the query is executed. + */ +export async function query(query: string): Promise { + await wp(`db query "${query}"`) +} + /** * Deactivates a WordPress plugin using the WP-CLI command. * From d821fff4e3e467f9d169c8f7b353e9bfce675942 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 2 Apr 2024 14:50:19 +0100 Subject: [PATCH 4/6] Revert changes for checking wpr related errors in debug.log --- src/support/steps/general.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/support/steps/general.ts b/src/support/steps/general.ts index 3106a04..89fd6e4 100644 --- a/src/support/steps/general.ts +++ b/src/support/steps/general.ts @@ -18,8 +18,6 @@ import { WP_BASE_URL } from '../../../config/wp.config'; import { createReference, compareReference } from "../../../utils/helpers"; import type { Section } from "../../../utils/types"; import { Page } from '@playwright/test'; -import { exists } from "../../../utils/commands"; -import { WP_SSH_ROOT_DIR } from "../../../config/wp.config"; /** * Executes the step to log in. @@ -232,13 +230,11 @@ Then('I should see {string}', async function (this: ICustomWorld, text) { * Executes the step to check for errors in debug.log. */ Then('I must not see any error in debug.log', async function (this: ICustomWorld){ - // Define the path to debug.log - const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; - - // Check if debug.log exists - const debugLogExists = await exists(debugLogPath); - - await expect(debugLogExists).toBe(false); + // Goto WP Rocket dashboard + await this.utils.gotoWpr(); + await this.page.waitForLoadState('load', { timeout: 30000 }); + // Assert that there is no related error in debug.log + await expect(this.page.locator('#wpr_debug_log_notice')).toBeHidden(); }); /** From 4140e38f2ddd6837a4da45de001b12954c68161c Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 4 Apr 2024 21:52:15 +0100 Subject: [PATCH 5/6] Removed console log --- utils/commands.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/commands.ts b/utils/commands.ts index b56daf5..d84a533 100644 --- a/utils/commands.ts +++ b/utils/commands.ts @@ -131,7 +131,6 @@ export async function rename(oldName: string, newName: string): Promise { } if(configurations.type === ServerType.external) { - console.log(`ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "mv ${oldName} ${newName}"`); await exec(`ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "mv ${oldName} ${newName}"`); return; } From 6c949f1fd00a705670968bd6e61b77f1a8bbf745 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 4 Apr 2024 21:52:52 +0100 Subject: [PATCH 6/6] Rename debug.log in after hook --- src/support/hooks.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/support/hooks.ts b/src/support/hooks.ts index 3f9e20f..588765c 100644 --- a/src/support/hooks.ts +++ b/src/support/hooks.ts @@ -94,14 +94,6 @@ BeforeAll(async function (this: ICustomWorld) { * Before each test scenario without the @setup tag, performs setup tasks. */ Before({tags: 'not @setup'}, async function (this: ICustomWorld) { - const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; - const debugLogExists = await exists(debugLogPath); - - if (debugLogExists && previousScenarioName) { - const newDebugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug-${previousScenarioName}.log`; - await rename(debugLogPath, newDebugLogPath); - } - /** * To uncomment during implementation of cli */ @@ -192,6 +184,17 @@ After(async function (this: ICustomWorld, { pickle, result }) { await this.utils.createScreenShot(this, pickle); } + const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`; + const debugLogExists = await exists(debugLogPath); + + if (debugLogExists && previousScenarioName) { + // Close up white spaces. + previousScenarioName = previousScenarioName.toLowerCase(); + previousScenarioName = previousScenarioName.replaceAll(' ', '-'); + const newDebugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug-${previousScenarioName}.log`; + await rename(debugLogPath, newDebugLogPath); + } + await this.page?.close() await this.context?.close()