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 #70 Add scenario for pages without LCP/ATF #76

76 changes: 45 additions & 31 deletions src/features/lcp-beacon-script.feature
Khadreal marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions src/support/steps/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ 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 {
deactivatePlugin, installRemotePlugin,
} from "../../../utils/commands";
/**
* Executes the step to log in.
*/
Expand Down Expand Up @@ -75,7 +77,6 @@ Given('I save settings {string} {string}', async function (this: ICustomWorld, s
await this.page.waitForLoadState('load', { timeout: 30000 });
});

/**
/**
* Executes the step to activate the WP plugin.
*/
Expand Down Expand Up @@ -326,4 +327,18 @@ Then('page navigated to the new page {string}', async function (this: ICustomWor
const url = `${WP_BASE_URL}/${path}`;
const regex = new RegExp(url);
await expect(this.page).toHaveURL(regex);
});

/**
* Executes the step to deactivate a specified WP plugin via CLI.
*/
Given('plugin {word} is deactivated', async function (plugin) {
await deactivatePlugin(plugin)
});

/**
* Executes the step to install a WP plugin from a remote url via CLI.
*/
Given('I install plugin {string}', async function (pluginUrl) {
await installRemotePlugin(pluginUrl)
});
84 changes: 81 additions & 3 deletions src/support/steps/lcp-beacon-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
import { ICustomWorld } from "../../common/custom-world";
import { expect } from "@playwright/test";
import { Given, Then } from "@cucumber/cucumber";
import { LcpDataTable, LcpData, Row } from "../../../utils/types";
import { Given, Then, When } from "@cucumber/cucumber";
import {LcpDataTable, LcpData, Row, NoLcpData} from "../../../utils/types";
import axios from 'axios';
import { dbQuery, getWPTablePrefix } from "../../../utils/commands";
import { extractFromStdout } from "../../../utils/helpers";
Expand All @@ -22,6 +22,7 @@ let data: LcpDataTable[],
failMsg: string = "";

const [actual, expected]: [LcpData, LcpData] = [{}, {}];
const [noATF, expectedATF]: [NoLcpData, NoLcpData] = [{}, {}];

/**
* Executes step to visit page based on the form factor(desktop/mobile) and get the LCP/ATF data from DB.
Expand Down Expand Up @@ -147,4 +148,81 @@ Then('lcp and atf should be as expected in {string}', async function (this: ICus
}

expect(truthy, failMsg).toBeTruthy();
});
});

/**
*
*/
When('I go to {string} in {string}', async function (this: ICustomWorld, page, formFactor: string) {
let viewPortWidth: number = 1600,
viewPortHeight: number = 700;

// Set page to be visited in mobile.
if ( formFactor === 'mobile' ) {
viewPortWidth = 393;
viewPortHeight = 830;
}


await this.page.waitForLoadState('load', { timeout: 100000 });

await this.page.setViewportSize({
width: viewPortWidth,
height: viewPortHeight
});

const tablePrefix: string = await getWPTablePrefix();

await this.utils.visitPage(page);

// Wait the beacon to add an attribute `beacon-complete` to true before fetching from DB.
await this.page.waitForFunction(() => {
const beacon = document.querySelector('[data-name="wpr-lcp-beacon"]');
return beacon && beacon.getAttribute('beacon-completed') === 'true';
});

// Get the LCP/ATF from the DB
const sql = `SELECT lcp, viewport FROM ${tablePrefix}wpr_above_the_fold WHERE url LIKE "%${page}%"`;
const result = await dbQuery(sql);
const resultFromStdout = await extractFromStdout(result);

noATF[0] = {
type: formFactor,
lcp: resultFromStdout[0].lcp,
viewport: resultFromStdout[0].viewport
}

expectedATF[0] = {
type: formFactor,
lcp: 'not found',
viewport: '[]'
}
})

/**
* Executes step to visit page based on the form factor(desktop/mobile) and get the LCP/ATF data from DB.
*/
Then('I must not see any LCP or ATF', async function (this: ICustomWorld) {
for (const key in noATF) {
if (Object.hasOwnProperty.call(noATF, key)) {
const [actualType, expectedType, actualLcp, expectedLcp, actualViewport, expectedViewport] = [noATF[key].type, expectedATF[key].type, noATF[key].lcp, expectedATF[key].lcp, noATF[key].viewport, expectedATF[key].viewport];

// Check if expected lcp is present in actual lcp.
if (!actualLcp.includes(expectedLcp)) {
truthy = false;
failMsg += `Expected LCP - ${expectedLcp} for ${expectedType} is not present in actual - ${actualLcp} for ${actualType}\n\n\n`;
}

if (!actualViewport.includes(expectedViewport)) {
truthy = false;
failMsg += `Expected Viewport - ${expectedViewport} for ${expectedType} is not present in actual - ${actualViewport} for ${actualType}\n\n\n`;
}
}
}

if ( failMsg !== '' ) {
console.log(failMsg);
}

expect(truthy, failMsg).toBeTruthy();
})
9 changes: 8 additions & 1 deletion src/support/steps/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
*/
import {expect} from "@playwright/test";
import {AfterAll, BeforeAll} from "@cucumber/cucumber";
import wp, {activatePlugin, cp, generateUsers, resetWP, rm, setTransient} from "../../../utils/commands";
import wp, {
activatePlugin,
cp,
generateUsers,
resetWP,
rm,
setTransient
} from "../../../utils/commands";
import {configurations, getWPDir} from "../../../utils/configurations";
import {match} from "ts-pattern";

Expand Down
14 changes: 14 additions & 0 deletions utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ export async function activatePlugin(name: string): Promise<void> {
await wp(`plugin activate ${name}`)
}

/**
* Install a WordPress plugin from a remote zip file using the WP-CLI command.
*
* @function
* @name installRemotePlugin
* @async
* @param {string} url - The remote zip url of the plugin to be installed.
* @returns {Promise<void>} - A Promise that resolves when the installation is completed.
*/
export async function installRemotePlugin(url: string): Promise<void> {
await wp(`plugin install ${url}`)
}


/**
* Executes a SQL query on the WordPress database using WP-CLI.
*
Expand Down
8 changes: 8 additions & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ export interface LcpData {

export interface Row {
[key: string]: string
}

export interface NoLcpData {
[key: string]: {
type: string,
lcp: string,
viewport: string
}
}
Loading