Skip to content

Commit

Permalink
:chore: Add steps, functions to test lazyload images -- #56
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Mar 28, 2024
1 parent 94d56b4 commit a003dba
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/features/lazy-load.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@llimg
Feature: Check if content are lazyloaded while scrolling
Background:
Given I am logged in
#And plugin is installed 'new_release'
#And plugin is activated
When I go to 'wp-admin/options-general.php?page=wprocket#dashboard'
And I save settings 'media' 'lazyloadCssBgImg'
And clear wpr cache

Scenario: Open Lazy load css background images page
When I log out
And I go to 'lazyload_css_background_images' Check initial image loaded
Then I must see other lazyloaded images
8 changes: 8 additions & 0 deletions src/support/steps/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ When('I go to {string}', async function (this: ICustomWorld, page) {
await this.page.waitForLoadState('load', { timeout: 100000 });
});

/**
* Clear wpr cache
*/
Given('clear wpr cache', async function (this: ICustomWorld) {
await this.utils.clearWPRCache();
});


/**
* Executes the step to click on a specific button.
*/
Expand Down
36 changes: 35 additions & 1 deletion src/support/steps/ll-css-bg-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Then } from '@cucumber/cucumber';
import { When, Then } from '@cucumber/cucumber';
import { ICustomWorld } from "../../common/custom-world";
import { expect } from "@playwright/test";
import { LL_BACKGROUND_IMAGES } from '../../../config/wp.config';

Then('I must see the correct style in the head', async function (this: ICustomWorld) {
const html = await this.page.evaluate(async () => {
Expand Down Expand Up @@ -58,4 +59,37 @@ Then('I must see the correct style in the head', async function (this: ICustomWo
console.log(failMessage);

expect(isMatch, failMessage).toBeTruthy();
});

When('I go to {string} Check initial image loaded', async function (this: ICustomWorld, page) {
const images = [];

this.page.on('request', request => {
if (request.resourceType() === 'image') {
images[0] = request.url();
expect(images).not.toContain(LL_BACKGROUND_IMAGES.templateOne.onLoad)
}
});

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

Then('I must see other lazyloaded images', async function (this: ICustomWorld) {
const scrollPage: Promise<void> = new Promise((resolve) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;

if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 700);
});

await scrollPage;
});
10 changes: 10 additions & 0 deletions utils/page-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ export class PageUtils {
await this.sections.massToggle();
}

public clearWPRCache = async(): Promise<void> => {
await this.gotoWpr();
await this.page.waitForLoadState('load', { timeout: 30000 });

const clearCacheURL = await this.page.locator('.wpr-button.wpr-button--icon.wpr-icon-trash').first().getAttribute('href');

await this.page.goto(clearCacheURL);
await this.page.waitForLoadState('load', { timeout: 30000 });
}

/**
* Performs the enable all options action on WP Rocket.
*
Expand Down

0 comments on commit a003dba

Please sign in to comment.