From 6cb699910b7766139c69a3cd68ec80dc200ac07b Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Tue, 27 Aug 2024 11:15:54 +0200 Subject: [PATCH] test: Fix flaky csp test (#13376) --- .../suites/feedback/captureFeedbackCsp/init.js | 7 ------- .../suites/feedback/captureFeedbackCsp/subject.js | 4 ++++ .../suites/feedback/captureFeedbackCsp/template.html | 4 +--- .../suites/feedback/captureFeedbackCsp/test.ts | 4 ++-- 4 files changed, 7 insertions(+), 12 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/subject.js diff --git a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/init.js b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/init.js index 067dbec23fd4..27e5495f66a8 100644 --- a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/init.js +++ b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/init.js @@ -10,10 +10,3 @@ Sentry.init({ feedbackIntegration({ tags: { from: 'integration init' }, styleNonce: 'foo1234', scriptNonce: 'foo1234' }), ], }); - -document.addEventListener('securitypolicyviolation', () => { - const container = document.querySelector('#csp-violation'); - if (container) { - container.innerText = 'CSP Violation'; - } -}); diff --git a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/subject.js b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/subject.js new file mode 100644 index 000000000000..66adfd0f87d4 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/subject.js @@ -0,0 +1,4 @@ +window.__CSPVIOLATION__ = false; +document.addEventListener('securitypolicyviolation', () => { + window.__CSPVIOLATION__ = true; +}); diff --git a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/template.html b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/template.html index 919f372ef468..8039192f5787 100644 --- a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/template.html +++ b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/template.html @@ -7,7 +7,5 @@ content="style-src 'nonce-foo1234'; script-src sentry-test.io 'nonce-foo1234';" /> - -
- + diff --git a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/test.ts b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/test.ts index 95a8a2eacee8..bca9b498fed0 100644 --- a/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/test.ts +++ b/dev-packages/browser-integration-tests/suites/feedback/captureFeedbackCsp/test.ts @@ -79,6 +79,6 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => { }, platform: 'javascript', }); - const cspContainer = await page.locator('#csp-violation'); - expect(cspContainer).not.toContainText('CSP Violation'); + const cspViolation = await page.evaluate('window.__CSPVIOLATION__'); + expect(cspViolation).toBe(false); });