Skip to content

Commit

Permalink
Merge b4c50c3 into a126db6
Browse files Browse the repository at this point in the history
  • Loading branch information
sookburt committed Feb 19, 2024
2 parents a126db6 + b4c50c3 commit fa0e3a8
Show file tree
Hide file tree
Showing 13 changed files with 508 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-clouds-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@guardian/consent-management-platform": patch
---

Proof of concept for consumer-self-test
7 changes: 4 additions & 3 deletions monitoring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
},
"dependencies": {
"@aws-sdk/client-cloudwatch": "^3.490.0",
"@guardian/consent-management-platform": "0.0.0-beta-20240212113333",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"inquirer": "^8.2.6",
"playwright-aws-lambda": "^0.10.0",
"playwright-core": "^1.40.1",
"playwright-core": "1.41.2",
"tslib": "^2.6.2",
"uuid": "^9.0.1"
},
"devDependencies": {
"@aws-sdk/client-lambda": "^3.490.0",
"@babel/parser": "^7.23.6",
"@babel/parser": "^7.23.6",
"@guardian/eslint-config-typescript": "^9.0.1",
"@guardian/prettier": "^7.0.0",
"@guardian/tsconfig": "^0.2.0",
Expand All @@ -47,4 +48,4 @@
"overrides": {
"clone-deep": "4.0.1"
}
}
}
2 changes: 1 addition & 1 deletion monitoring/src/check-page/aus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const checkPages = async (config: Config, url: string, nextUrl: string) => {
await loadPage(page, url);
await checkTopAdHasLoaded(page);
await checkCMPIsOnPage(page);
await clickAcceptAllCookies(config, page, 'Continue');
await clickAcceptAllCookies(page, 'Continue');
await checkCMPIsNotVisible(page);
await reloadPage(page);
await checkTopAdHasLoaded(page);
Expand Down
46 changes: 6 additions & 40 deletions monitoring/src/check-page/common-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ import {
CloudWatchClient,
PutMetricDataCommand
} from '@aws-sdk/client-cloudwatch';
import { clickAcceptAllCookies, getCMPVersionRunning, log_error, log_info } from '@guardian/consent-management-platform';
import { launchChromium } from 'playwright-aws-lambda';
import type { Browser, BrowserContext, Page, Request } from 'playwright-core';
import type { Config } from '../types';
import { ELEMENT_ID } from '../types';
// import { text } from 'stream/consumers';

/**
* This function console logs an info message.
*
* @param {string} message
*/
export const log_info = (message: string): void => {
console.log(`(cmp monitoring) info: ${message}`);
};

/**
* This function console logs an error message.
*
* @param {string} message
*/
export const log_error = (message: string): void => {
console.error(`(cmp monitoring): error: ${message}`);
};

/**
* This function will clear the cookies for a chromium client
Expand Down Expand Up @@ -73,24 +59,6 @@ export const makeNewPage = async (context: BrowserContext): Promise<Page> => {
return page;
};

/**
* This function waits for the page to load
* clicks the accept all button
*
* @param {Config} config
* @param {Page} page
* @param {string} textToPrintToConsole
*/
export const clickAcceptAllCookies = async (config: Config, page: Page, textToPrintToConsole: string) => {

log_info(`Clicking on "${textToPrintToConsole}" on CMP`);

const acceptAllButton = page.frameLocator(ELEMENT_ID.CMP_CONTAINER).locator(ELEMENT_ID.TCFV2_FIRST_LAYER_ACCEPT_ALL);
await acceptAllButton.click();

log_info(`Clicked on "${textToPrintToConsole}"`);
};

/**
* This function waits for the page to load
* clicks the manage cookies button to open the privacy settings panel
Expand Down Expand Up @@ -238,13 +206,9 @@ export const checkTopAdDidNotLoad = async (page: Page) => {
};

export const recordVersionOfCMP = async (page: Page) => {
log_info('* Getting the version of Sourcepoint CMP');
log_info('* Getting the versions:');

const functionToGetVersion = function () {
return window._sp_.version;
};

log_info(await page.evaluate(functionToGetVersion));
await getCMPVersionRunning(page);
};

/**
Expand Down Expand Up @@ -433,3 +397,5 @@ export const checkCMPLoadingTimeAndVersion = async (page: Page, config: Config)
}
};

export {clickAcceptAllCookies, log_info, log_error}

6 changes: 3 additions & 3 deletions monitoring/src/check-page/tcfv2.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { clickAcceptAllCookies } from '@guardian/consent-management-platform';
import type { Browser, BrowserContext, Page } from 'playwright-core';
import type { Config } from '../types';
import {
Expand All @@ -9,7 +10,6 @@ import {
checkTopAdHasLoaded,
clearCookies,
clearLocalStorage,
clickAcceptAllCookies,
clickRejectAllSecondLayer,
clickSaveAndCloseSecondLayer,
loadPage,
Expand Down Expand Up @@ -45,7 +45,7 @@ const checkSubsequentPage = async (
]);
await reloadPage(page);
await checkTopAdDidNotLoad(page);
await clickAcceptAllCookies(config, page, "Yes I'm Happy");
await clickAcceptAllCookies(page, "Yes I'm Happy");
await Promise.all([
checkCMPIsNotVisible(page),
checkTopAdHasLoaded(page),
Expand Down Expand Up @@ -126,7 +126,7 @@ export const firstLayerCheck = async function (

await checkTopAdDidNotLoad(page);

await clickAcceptAllCookies(config, page, "Yes I'm Happy");
await clickAcceptAllCookies(page, "Yes I'm Happy");

await checkCMPIsNotVisible(page);

Expand Down
13 changes: 9 additions & 4 deletions monitoring/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.56.0.tgz#ef20350fec605a7f7035a01764731b2de0f3782b"
integrity sha512-gMsVel9D7f2HLkBma9VbtzZRehRogVRfbr++f06nL2vnCGCNlzOD+/MUov/F4p8myyAHspEhVobgjpX64q5m6A==

"@guardian/consent-management-platform@0.0.0-beta-20240212113333":
version "0.0.0-beta-20240212113333"
resolved "https://registry.yarnpkg.com/@guardian/consent-management-platform/-/consent-management-platform-0.0.0-beta-20240212113333.tgz#ffcd9cdc7146a9f03498a6204e0a1107eefcf7fa"
integrity sha512-clMExgPjDaH2CMChrlMvvTQDrAmk3B3zItDy/SNZICbGrCQ+j/4WQuvv286+pd2L/q/oXXYhXfQpoh1cn9yoRQ==

"@guardian/eslint-config-typescript@^9.0.1":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@guardian/eslint-config-typescript/-/eslint-config-typescript-9.0.1.tgz#42d63f6b4d0c0414d5a7a6a4da3b288fa53df709"
Expand Down Expand Up @@ -4724,10 +4729,10 @@ playwright-aws-lambda@^0.10.0:
dependencies:
lambdafs "^2.1.1"

playwright-core@^1.40.1:
version "1.40.1"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.40.1.tgz#442d15e86866a87d90d07af528e0afabe4c75c05"
integrity sha512-+hkOycxPiV534c4HhpfX6yrlawqVUzITRKwHAmYfmsVreltEl6fAZJ3DPfLMOODw0H3s1Itd6MDCWmP1fl/QvQ==
playwright-core@1.41.2:
version "1.41.2"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.41.2.tgz#db22372c708926c697acc261f0ef8406606802d9"
integrity sha512-VaTvwCA4Y8kxEe+kfm2+uUUw5Lubf38RxF7FpBxLPmGe5sdNkSg5e3ChEigaGrX7qdqT3pt2m/98LiyvU2x6CA==

prelude-ls@^1.2.1:
version "1.2.1"
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"homepage": "https://github.com/guardian/consent-management-platform.git",
"repository": "https://github.com/guardian/consent-management-platform.git",
"license": "Apache-2.0",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
Expand Down Expand Up @@ -76,6 +76,8 @@
"markdown-toc": "^1",
"np": "^9.2.0",
"npm-run-all": "^4",
"playwright-core": "1.41.2",
"playwright-aws-lambda": "^0.10.0",
"prettier": "^3.2.4",
"prettier-plugin-svelte": "^3.1.2",
"rollup": "^2.70.1",
Expand All @@ -88,9 +90,11 @@
"svelte": "~3.59.1",
"tslib": "^2.6.2",
"typescript": "~5.3.3",
"wait-for-expect": "^3"
"wait-for-expect": "^3",
"ts-node-dev": "^2.0.0"
},
"peerDependencies": {
"playwright-core": "1.41.2",
"@guardian/libs": "^15.0.0 || ^16.0.0"
}
}
117 changes: 117 additions & 0 deletions src/consumer-self-test-commons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import type { Browser, BrowserContext, Page } from 'playwright-core';
import { launchChromium } from 'playwright-aws-lambda';

const ELEMENT_ID = {
TCFV2_FIRST_LAYER_ACCEPT_ALL:
'div.message-component.message-row > button.sp_choice_type_11',
CMP_CONTAINER: '[id*="sp_message_iframe"]',
};

/**
* This function console logs an info message.
*
* @param {string} message
*/
export const log_info = (message: string): void => {
console.log(`(cmp monitoring) info: ${message}`);
};

/**
* This function console logs an error message.
*
* @param {string} message
*/
export const log_error = (message: string): void => {
console.error(`(cmp monitoring): error: ${message}`);
};

/**
* This function waits for the page to load
* clicks the accept all button
*
* @param {Config} config
* @param {Page} page
* @param {string} textToPrintToConsole
*/
export const clickAcceptAllCookies = async (page: Page, textToPrintToConsole: string) => {

log_info(`Clicking on "${textToPrintToConsole}" on CMP`);

const acceptAllButton = page.frameLocator(ELEMENT_ID.CMP_CONTAINER).locator(ELEMENT_ID.TCFV2_FIRST_LAYER_ACCEPT_ALL);
await acceptAllButton.click();

log_info(`Clicked on "${textToPrintToConsole}"`);
};

export const getCMPVersionRunning = async (page: Page) => {
log_info(`Sourcepoint version: ${await page.evaluate('window._sp_.version')}`);
log_info(`CMP version: ${await page.evaluate('window.guCmpHotFix.cmp.version')}`);
};

/**
* This function checks for the CMP banner on the page
*
* @param {Page} page
* @return {*} {Promise<void>}
*/
export const checkCMPIsOnPage = async (page: Page): Promise<void> => {
log_info(`Waiting for CMP: Start`);

const cmpl = page.locator(ELEMENT_ID.CMP_CONTAINER);
await cmpl.waitFor();
await getCMPVersionRunning(page);
if (!(await cmpl.isVisible())) {
throw Error('CMP is not present on page');
}

log_info(`Waiting for CMP: Complete`);
};

/**
* This function loads a url onto a chromium page
*
* @param {Page} page
* @param {string} url
* @return {*} {Promise<void>}
*/
export const loadPage = async (page: Page, url: string): Promise<void> => {
log_info(`Loading page: Start`);
log_info(`Loading page ${url}`);

const response = await page.goto(url, {
waitUntil: 'domcontentloaded',
timeout: 30000,
});

// If the response status code is not a 2xx success code
if (response != null) {
if (response.status() < 200 || response.status() > 299) {
log_error(`Loading URL: Error: Status ${response.status()}`);
throw 'Failed to load page!';
}
}

log_info(`Loading page: Complete`);
};

/**
* This function creates a new chromium browser
*
* @param {boolean} debugMode
* @return {*} {Promise<Browser>}
*/
export const makeNewBrowser = async (debugMode: boolean): Promise<Browser> => {
const browser = await launchChromium({headless:!debugMode});
return browser;
};

/**
* This function creates a new page
*
* @param {BrowserContext} context
* @return {*} {Promise<Page>}
*/
export const makeNewPage = async (context: BrowserContext): Promise<Page> => {
const page = await context.newPage();
return page;
};

0 comments on commit fa0e3a8

Please sign in to comment.