mirror of
https://github.com/wasp-lang/open-saas.git
synced 2025-06-04 03:59:35 +02:00
avoid cookie setting race condition (#176)
* increase max time to await cookies * Update landingPageTests.spec.ts * Update landingPageTests.spec.ts * Update e2e-tests.yml * Update landingPageTests.spec.ts * Update landingPageTests.spec.ts * Update landingPageTests.spec.ts * extract cookie check to function * apply marty's suggestions
This commit is contained in:
parent
0a100d532c
commit
027d7c2018
@ -1,4 +1,4 @@
|
|||||||
import { test, expect } from '@playwright/test';
|
import { test, expect, Cookie } from '@playwright/test';
|
||||||
|
|
||||||
const DOCS_URL = 'https://docs.opensaas.sh';
|
const DOCS_URL = 'https://docs.opensaas.sh';
|
||||||
|
|
||||||
@ -54,19 +54,24 @@ test.describe('cookie consent tests', () => {
|
|||||||
let cookies = await context.cookies();
|
let cookies = await context.cookies();
|
||||||
const consentCookie = cookies.find((c) => c.name === 'cc_cookie');
|
const consentCookie = cookies.find((c) => c.name === 'cc_cookie');
|
||||||
const cookieObject = JSON.parse(decodeURIComponent(consentCookie.value));
|
const cookieObject = JSON.parse(decodeURIComponent(consentCookie.value));
|
||||||
|
// Check that the Cookie Consent cookie is set. This should happen immediately, and then the GA cookies will get set after it, dynamically.
|
||||||
expect(cookieObject.categories.includes('analytics')).toBeTruthy();
|
expect(cookieObject.categories.includes('analytics')).toBeTruthy();
|
||||||
|
|
||||||
// Wait for Google Analytics cookies to be set after accepting
|
const areGaCookiesSet = (cookies: Cookie[]) => {
|
||||||
const MAX_TIME = 15000;
|
const gaCookiesArr = cookies.filter((c) => c.name.startsWith('_ga'));
|
||||||
|
return gaCookiesArr.length === 2; // GA cookies are _ga and _ga_<GA_ANALYTICS_ID>
|
||||||
|
};
|
||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
while (cookies.length === 1) {
|
const MAX_TIME_MS = 10000;
|
||||||
if (Date.now() - startTime > MAX_TIME) {
|
let timeElapsed = 0;
|
||||||
throw new Error('Timeout: Google Analytics cookies not set.');
|
|
||||||
}
|
while (!areGaCookiesSet(cookies) && timeElapsed < MAX_TIME_MS) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000)); // wait for 1 second before checking again
|
||||||
cookies = await context.cookies();
|
cookies = await context.cookies();
|
||||||
|
timeElapsed = Date.now() - startTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
const gaCookiesArr = cookies.filter((c) => c.name.startsWith('_ga'));
|
expect(timeElapsed).toBeLessThan(MAX_TIME_MS);
|
||||||
expect(gaCookiesArr.length === 2).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user