Skip to content

Commit 68fd11a

Browse files
WIP: One Trust Test Case
1 parent 355a289 commit 68fd11a

File tree

9 files changed

+99
-90
lines changed

9 files changed

+99
-90
lines changed

packages/consent/consent-tools-integration-tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ yarn . test:int
2222
### Debugging Tips:
2323
- Webdriver.io has the handy `browser.debug()` command.
2424

25-
- You can serve the static pages by themselves (without webdriver.io) with the following:
25+
- You can serve the static pages by themselves (without Playwright) with the following:
2626
```
2727
yarn webpack -w &
2828
npx live-server .

packages/consent/consent-tools-integration-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"scripts": {
55
".": "yarn run -T turbo run --filter=@internal/consent-tools-integration-tests...",
66
"test:int": "playwright test",
7+
"test:int-debug": "playwright test --debug",
78
"build": "webpack",
89
"watch": "yarn build --watch",
910
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<h1>Hello World - Serving Analytics</h1>
5+
<h2>Please Check Network tab</h2>
6+
<p>This page can used as playground or run by Playwright</p>
7+
<!-- <script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript" charset="UTF-8"
8+
data-domain-script="80ca7b5c-e72f-4bd0-972a-b74d052a0820-test"></script>
9+
<script src="https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@latest/dist/umd/analytics-onetrust.global.js"></script> -->
10+
</body>
11+
</html>

packages/consent/consent-tools-integration-tests/public/consent-tools-vanilla-opt-in.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<body>
55
<h1>Hello World - Serving Analytics</h1>
66
<h2>Please Check Network tab</h2>
7-
<p>This page can used as playground or run by webdriver.io</p>
7+
<p>This page can used as playground or run by Playwright</p>
88
<script src="dist/consent-tools-vanilla-opt-in.bundle.js"></script>
99
</body>
1010

packages/consent/consent-tools-integration-tests/public/consent-tools-vanilla-opt-out.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<body>
55
<h1>Hello World - Serving Analytics (Consent Tools Vanilla Opt Out)</h1>
66
<h2>Please Check Network tab</h2>
7-
<p>This page can used as playground or run by webdriver.io</p>
7+
<p>This page can used as playground or run by Playwright</p>
88
<script src="dist/consent-tools-vanilla-opt-out.bundle.js"></script>
99
</body>
1010

packages/consent/consent-tools-integration-tests/public/onetrust.html

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AnalyticsBrowser } from '@segment/analytics-next'
2+
import { withMockCMP } from '../helpers/mock-cmp-wrapper'
3+
4+
const analytics = new AnalyticsBrowser()
5+
6+
withMockCMP(analytics).load(
7+
{
8+
writeKey: 'foo',
9+
},
10+
{ initialPageview: true }
11+
)
12+
;(window as any).analytics = analytics
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1+
// page-objects/onetrust.ts
12
import { Page } from '@playwright/test'
23
import { BasePage } from './base-page'
34

4-
export class OneTrustPage extends BasePage {
5+
class OneTrustPage extends BasePage {
56
constructor(page: Page) {
6-
super(page, 'onetrust.html')
7+
super(page, 'consent-tools-onetrust.html')
78
}
89

9-
// Check if OneTrust is loaded by evaluating a global variable on the page
10-
async isOneTrustLoaded() {
11-
// To Do
10+
// Check for global variable `window.isOneTrustLoaded`
11+
async isOneTrustLoaded(): Promise<boolean> {
12+
return await this.page.evaluate(() => {
13+
return Boolean((window as any).isOneTrustLoaded)
14+
})
1215
}
1316

14-
//Click the OneTrust accept button
15-
async clickAcceptButtonAndClosePopup() {
16-
// To Do
17+
async clickGiveConsent() {
18+
//await this.page.click('#onetrust-accept-btn-handler')
19+
const acceptButton = this.page.locator('#onetrust-accept-btn-handler')
20+
await acceptButton.waitFor({ state: 'visible' }) // Ensure the button is visible
21+
await acceptButton.click()
22+
}
23+
24+
async clickDenyConsent() {
25+
const rejectButton = await this.page.locator('#onetrust-reject-all-handler')
26+
await rejectButton.waitFor({ state: 'visible' }) // Ensure the button is visible
27+
await rejectButton.click()
28+
}
29+
}
30+
31+
export class OneTrustConsentPage extends OneTrustPage {
32+
constructor(page: Page) {
33+
super(page)
1734
}
1835
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { test, expect } from '@playwright/test'
2+
import { OneTrustConsentPage } from '../page-objects/onetrust'
3+
4+
// Setup for your page object
5+
let pageObject: OneTrustConsentPage
6+
7+
test.beforeEach(async ({ page: p }) => {
8+
// Initialize the page object
9+
pageObject = new OneTrustConsentPage(p)
10+
11+
// Load the page
12+
await pageObject.load()
13+
})
14+
15+
test.afterEach(async () => {
16+
await pageObject.cleanup()
17+
})
18+
19+
test('should send a consent changed event when user clicks accept on popup', async ({
20+
page,
21+
}) => {
22+
// Pause for a brief moment if needed
23+
await page.waitForTimeout(1000)
24+
25+
// Check that no consent changed event is initially sent
26+
const consentChangedEvents = pageObject.getConsentChangedEvents()
27+
expect(consentChangedEvents.length).toBe(0)
28+
29+
await page.waitForSelector('#onetrust-accept-btn-handler', { timeout: 5000 })
30+
31+
// Make a consent selection in the OneTrust popup
32+
await pageObject.clickGiveConsent()
33+
34+
await page.waitForTimeout(2000)
35+
// Optional: print all tracking events for debug purposes
36+
console.log('All tracking events so far:', pageObject.getAllTrackingEvents())
37+
// // Wait for the consent changed event to be sent using poll and a timeout
38+
// await expect
39+
// .poll(() => pageObject.getConsentChangedEvents().length, {
40+
// timeout: 10_000, // Timeout after 10 seconds
41+
// })
42+
// .toBeGreaterThan(0) // Ensure the consent event count increases
43+
44+
// // Ensure that exactly 1 consent changed event was sent
45+
// const updatedConsentChangedEvents = pageObject.getConsentChangedEvents()
46+
// expect(updatedConsentChangedEvents.length).toBe(1)
47+
})

0 commit comments

Comments
 (0)