diff --git a/README.md b/README.md index 47ed22c..510e820 100644 --- a/README.md +++ b/README.md @@ -6,34 +6,36 @@ Contribute by raising pull requests. All commands must be documented and, if pos # 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) +- [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 @@ -43,7 +45,7 @@ General commands - `visitAndWaitForSelector` - `setLanguage` -- `hideCookieBanner` +- `hideCookieBanner`, `acceptCookieBanner` - `disableGainsights` Authentication related commands diff --git a/src/lib/commands/general.ts b/src/lib/commands/general.ts index 3fb6a09..217f44e 100644 --- a/src/lib/commands/general.ts +++ b/src/lib/commands/general.ts @@ -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; + /** + * Accept the cookie banner with the provided configuration for current, functional and marketing cookies. + */ + acceptCookieBanner( + required: boolean, + functional: boolean, + marketing: boolean + ): Chainable; + /** * Sets the language in user preferences. * @@ -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", ( diff --git a/test/cypress/app/apps/public/public-options/options.json b/test/cypress/app/apps/public/public-options/options.json new file mode 100644 index 0000000..446eb84 --- /dev/null +++ b/test/cypress/app/apps/public/public-options/options.json @@ -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" +} \ No newline at end of file diff --git a/test/cypress/app/apps/public/public-options@app-cockpit/options.json b/test/cypress/app/apps/public/public-options@app-cockpit/options.json new file mode 100644 index 0000000..36446bc --- /dev/null +++ b/test/cypress/app/apps/public/public-options@app-cockpit/options.json @@ -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." + } + } \ No newline at end of file diff --git a/test/cypress/e2e/general.cy.ts b/test/cypress/e2e/general.cy.ts index 64331a2..823c978 100644 --- a/test/cypress/e2e/general.cy.ts +++ b/test/cypress/e2e/general.cy.ts @@ -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", + }); + }); + }); }); });