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

fix(command): Fix hideCookieBanner command #206

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
60 changes: 31 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ Contribute by raising pull requests. All commands must be documented and, if pos

# Content
<!-- set markdown.extension.toc.levels 2..6 - level 1 is ignored in auto generated toc -->
- [Overview of commands](#overview-of-commands)
- [Screenshot automation](#screenshot-automation)
- [Installation and setup](#installation-and-setup)
- [Peer dependencies](#peer-dependencies)
- [Load plugin](#load-plugin)
- [Import commands](#import-commands)
- [Environment variables](#environment-variables)
- [Additional frameworks](#additional-frameworks)
- [Concepts](#concepts)
- [Authentication and credentials](#authentication-and-credentials)
- [Authentication via getAuth and useAuth commands](#authentication-via-getauth-and-useauth-commands)
- [Authentication via test case annotations](#authentication-via-test-case-annotations)
- [Authentication via environment variables](#authentication-via-environment-variables)
- [Passing authentication to cy.request](#passing-authentication-to-cyrequest)
- [Chaining of commands](#chaining-of-commands)
- [c8y/client and Web SDK types](#c8yclient-and-web-sdk-types)
- [Recording of requests and responses](#recording-of-requests-and-responses)
- [Component testing](#component-testing)
- [Development](#development)
- [Debugging](#debugging)
- [Console log debugging](#console-log-debugging)
- [Debugging in Visual Studio Code](#debugging-in-visual-studio-code)
- [Testing](#testing)
- [Test access of DOM elements](#test-access-of-dom-elements)
- [Test requests](#test-requests)
- [Test interceptions](#test-interceptions)
- [Useful links](#useful-links)
- [Disclaimer](#disclaimer)
- [Cypress commands for Cumulocity](#cypress-commands-for-cumulocity)
- [Content](#content)
- [Overview of commands](#overview-of-commands)
- [Screenshot automation](#screenshot-automation)
- [Installation and setup](#installation-and-setup)
- [Peer dependencies](#peer-dependencies)
- [Load plugin](#load-plugin)
- [Import commands](#import-commands)
- [Environment variables](#environment-variables)
- [Additional frameworks](#additional-frameworks)
- [Concepts](#concepts)
- [Authentication and credentials](#authentication-and-credentials)
- [Authentication via getAuth and useAuth commands](#authentication-via-getauth-and-useauth-commands)
- [Authentication via test case annotations](#authentication-via-test-case-annotations)
- [Authentication via environment variables](#authentication-via-environment-variables)
- [Passing authentication to cy.request](#passing-authentication-to-cyrequest)
- [Chaining of commands](#chaining-of-commands)
- [c8y/client and Web SDK types](#c8yclient-and-web-sdk-types)
- [Recording of requests and responses](#recording-of-requests-and-responses)
- [Component testing](#component-testing)
- [Development](#development)
- [Debugging](#debugging)
- [Console log debugging](#console-log-debugging)
- [Debugging in Visual Studio Code](#debugging-in-visual-studio-code)
- [Testing](#testing)
- [Test access of DOM elements](#test-access-of-dom-elements)
- [Test requests](#test-requests)
- [Test interceptions](#test-interceptions)
- [Useful links](#useful-links)
- [Disclaimer](#disclaimer)

## Overview of commands

Expand All @@ -43,7 +45,7 @@ General commands

- `visitAndWaitForSelector`
- `setLanguage`
- `hideCookieBanner`
- `hideCookieBanner`, `acceptCookieBanner`
- `disableGainsights`

Authentication related commands
Expand Down
62 changes: 52 additions & 10 deletions src/lib/commands/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ declare global {
namespace Cypress {
interface Chainable {
/**
* Disables the Cumulocity cookie banner. Run before visit.
* Disables the Cumulocity cookie banner.
*/
hideCookieBanner(): Chainable<void>;

/**
* Accept the cookie banner with the provided configuration for current, functional and marketing cookies.
*/
acceptCookieBanner(
required: boolean,
functional: boolean,
marketing: boolean
): Chainable<void>;

/**
* Sets the language in user preferences.
*
Expand Down Expand Up @@ -57,20 +66,53 @@ declare global {
}

Cypress.Commands.add("hideCookieBanner", () => {
const COOKIE_NAME = "acceptCookieNotice";
const COOKIE_VALUE = '{"required":true,"functional":true}';

Cypress.on("window:before:load", (window) => {
window.localStorage.setItem(COOKIE_NAME, COOKIE_VALUE);
});
window.localStorage.setItem(COOKIE_NAME, COOKIE_VALUE);

Cypress.log({
name: "hideCookieBanner",
message: "",
});
cy.acceptCookieBanner(true, true, true);
});

Cypress.Commands.add(
"acceptCookieBanner",
(required = true, functional = true, marketing = true) => {
const COOKIE_NAME = "acceptCookieNotice";

const consoleProps = {
required,
functional,
marketing,
};
Cypress.log({
name: "acceptCookieBanner",
message: "",
consoleProps: () => consoleProps,
});

cy.intercept(
{
pathname: /\/apps\/public\/public-options(@app-[^/]+)?\/options.json/,
},
(request) => {
request.on("before:response", (response) => {
if (response.statusCode !== 200) {
return;
}
const policyVersion = response.body.cookieBanner?.policyVersion;
const denyCookies: { [key: string]: string | boolean } = {
required: !!required,
functional: !!functional,
marketing: !!marketing,
};
if (policyVersion != null) {
denyCookies.policyVersion = policyVersion;
}
localStorage.setItem(COOKIE_NAME, JSON.stringify(denyCookies));
});
}
).as("publicOptions");
}
);

Cypress.Commands.add(
"visitAndWaitForSelector",
(
Expand Down
14 changes: 14 additions & 0 deletions test/cypress/app/apps/public/public-options/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"cookieBanner": {
"cookieBannerTitle": "About cookies on this website",
"cookieBannerText": "Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept all cookies and go directly to the site or click Configure preferences for further details and to manage your options. The consent can be revoked at any time.",
"policyUrl": "https://www.softwareag.com/en_corporate/privacy.html",
"cookieBannerDisabled": false,
"policyVersion": "2"
},
"cookiePreferences": {
"required": true,
"functional": "These cookies are used to support you during your first steps in the product, to deliver content tailored to your needs, and to collect usage statistics."
},
"lastUpdated": "2024-10-24T12:32:15.558Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"cookieBanner": {
"cookieBannerTitle": "About cookies on this website",
"cookieBannerText": "Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept all cookies and go directly to the site or click Configure preferences for further details and to manage your options. The consent can be revoked at any time.",
"policyUrl": "https://cumulocity.com/docs/legal-notices/privacy-notice/",
"policyVersion": "2024-12-03"
},
"cookiePreferences": {
"required": true,
"functional": "These cookies are used to support you during your first steps in the product, to deliver content tailored to your needs, and to collect usage statistics."
}
}
65 changes: 56 additions & 9 deletions test/cypress/e2e/general.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,68 @@ describe("general", () => {
});
});

context("hideCookieBanner", () => {
it("hideCookieBanner should update localStorage cookie value", () => {
context("CookieBanner", () => {
beforeEach(() => {
cy.window().then((win) => {
const cookie = win.localStorage.getItem("acceptCookieNotice");
expect(cookie).to.be.null;
});
});

cy.hideCookieBanner();
it("hideCookieBanner should configure acceptCookieNotice", () => {
cy.hideCookieBanner()
.as("interception")
.then(() => $.get(url(`/apps/public/public-options/options.json?_=a`)))
.then(() => {
cy.window().then((win) => {
const cookie = win.localStorage.getItem("acceptCookieNotice");
expect(cookie).to.be.not.null;
expect(JSON.parse(cookie!)).to.deep.eq({
required: true,
functional: true,
marketing: true,
policyVersion: "2",
});
});
});
});

cy.window().then((win) => {
const cookie = JSON.parse(
win.localStorage.getItem("acceptCookieNotice")!
);
expect(cookie).to.deep.eq({ required: true, functional: true });
});
it("acceptCookieBanner should configure acceptCookieNotice", () => {
cy.acceptCookieBanner(false, false, false)
.as("interception")
.then(() => $.get(url(`/apps/public/public-options/options.json?_=b`)))
.then(() => {
cy.window().then((win) => {
const cookie = win.localStorage.getItem("acceptCookieNotice");
expect(cookie).to.be.not.null;
expect(JSON.parse(cookie!)).to.deep.eq({
required: false,
functional: false,
marketing: false,
policyVersion: "2",
});
});
});
});

it("acceptCookieBanner should intercept app options", () => {
cy.acceptCookieBanner(false, false, true)
.as("interception")
.then(() =>
$.get(url(`/apps/public/public-options@app-cockpit/options.json?_=c`))
)
.then(() => {
cy.window().then((win) => {
const cookie = win.localStorage.getItem("acceptCookieNotice");
expect(cookie).to.be.not.null;
expect(JSON.parse(cookie!)).to.deep.eq({
required: false,
functional: false,
marketing: true,
policyVersion: "2024-12-03",
});
});
});
});
});

Expand Down
Loading