Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #59: Deletes or rename debug.log file before tests #60

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/support/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -42,6 +49,9 @@ 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/*.log`;
await rm(debugLogPath);

await deleteFolder('./backstop_data/bitmaps_test');
browser = await chromium.launch({ headless: false });

Expand Down Expand Up @@ -77,6 +87,7 @@ BeforeAll(async function (this: ICustomWorld) {
urls: SCENARIO_URLS
});
}

});

/**
Expand Down Expand Up @@ -139,7 +150,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
*/
Expand Down Expand Up @@ -167,10 +178,23 @@ 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);
}

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()

Expand Down
75 changes: 75 additions & 0 deletions utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,68 @@ export async function cp(origin: string, destination: string): Promise<void> {
});
}

/**
* 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<void>} - A Promise that resolves when the rename operation is completed.
*/
export async function rename(oldName: string, newName: string): Promise<void> {
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) {
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<boolean>} - A Promise that resolves with true if the file exists, false otherwise.
*/
export async function exists(filePath: string): Promise<boolean> {
let command: string;

if(configurations.type === ServerType.docker) {
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}; echo $?'`;
} else {
command = `test -f ${filePath}; echo $?`;
}

try {
const result = await exec(command, {
cwd: configurations.rootDir,
async: false
});
return result.stdout.trim() === '0';
} catch (error) {
return false;
}
}

/**
* Unzips a compressed file to the specified destination on the server.
*
Expand Down Expand Up @@ -161,6 +223,19 @@ export async function activatePlugin(name: string): Promise<void> {
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<void>} - A Promise that resolves when the query is executed.
*/
export async function query(query: string): Promise<void> {
await wp(`db query "${query}"`)
}

/**
* Deactivates a WordPress plugin using the WP-CLI command.
*
Expand Down
Loading