Skip to content

Commit

Permalink
Klaro: fix toggle selectors (#339)
Browse files Browse the repository at this point in the history
* Klaro: use "disable all" button when available.

* Exclude 'required-only'

* Support multiline eval snippets and update klaro to test all possible APIs

* Improve Klaro self test snippet

* Tidy spacing

* Handle some more design variants
  • Loading branch information
sammacbeth authored Jan 11, 2024
1 parent 96f2926 commit ea8ddaf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/cmps/klaro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Klaro extends AutoConsentCMPBase {
}

if (!this.settingsOpen) {
click('.klaro .cn-learn-more');
click('.klaro .cn-learn-more,.klaro .cm-button-manage');
await waitForElement('.klaro > .cookie-modal', 2000);
this.settingsOpen = true;
}
Expand All @@ -45,8 +45,8 @@ export default class Klaro extends AutoConsentCMPBase {
return true;
}

click('.cm-purpose:not(.cm-toggle-all) > input:not(.half-checked)', true);
return click('.cm-btn-accept');
click('.cm-purpose:not(.cm-toggle-all) > input:not(.half-checked,.required,.only-required),.cm-purpose:not(.cm-toggle-all) > div > input:not(.half-checked,.required,.only-required)', true);
return click('.cm-btn-accept,.cm-button');
}

async optIn() {
Expand Down
19 changes: 17 additions & 2 deletions lib/eval-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@ export const snippets = {
EVAL_COOKIEBOT_3: () => window.Cookiebot.withdraw() || true,
EVAL_COOKIEBOT_4: () => window.Cookiebot.hide() || true,
EVAL_COOKIEBOT_5: () => window.Cookiebot.declined === true,
EVAL_KLARO_1: () => klaro.getManager().config.services.every(c => c.required || !klaro.getManager().consents[c.name]),
EVAL_KLARO_1: () => {
const config = globalThis.klaroConfig || (globalThis.klaro?.getManager && globalThis.klaro.getManager().config)
if (!config) {
// with no klaro globals, we can't test on this page
return true
}
const optionalServices = (config.services || config.apps).filter(s => !s.required).map(s => s.name)
if (klaro && klaro.getManager) {
const manager = klaro.getManager()
return optionalServices.every(name => !manager.consents[name])
} else if (klaroConfig && klaroConfig.storageMethod === 'cookie') {
const cookieName = klaroConfig.cookieName || klaroConfig.storageName;
const consents = JSON.parse(decodeURIComponent(document.cookie.split(';').find(c => c.trim().startsWith(cookieName)).split('=')[1]))
return Object.keys(consents).filter(k => optionalServices.includes(k)).every(k => consents[k] === false)
}
},
EVAL_ONETRUST_1: () => window.OnetrustActiveGroups.split(',').filter(s => s.length > 0).length <= 1,
EVAL_TRUSTARC_TOP: () => window && window.truste && window.truste.eu.bindMap.prefCookie === '0',

Expand Down Expand Up @@ -112,5 +127,5 @@ export const snippets = {

export function getFunctionBody(snippetFunc: () => any) {
const snippetStr = snippetFunc.toString();
return snippetStr.substring(snippetStr.indexOf("=>") + 2);
return `(${snippetStr})()`
}
1 change: 1 addition & 0 deletions tests/klaro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ generateCMPTests('Klaro', [
'https://www.zeitraum-moebel.de/',
'https://repisalud.isciii.es/',
'https://www.innogames.com/',
'https://www.lebenslauf.de/',
// 'https://dspace.library.stonybrook.edu', // polyfills Promise so playwright doesn't work
]);

0 comments on commit ea8ddaf

Please sign in to comment.