Skip to content

Commit e48f302

Browse files
Replaced One Test Case from WebDriver.IO to Playwright: consent-tools-vanilla-opt-in.test.ts and Removed all WebDriver.IO Test Cases
1 parent 26fb722 commit e48f302

File tree

8 files changed

+72
-276
lines changed

8 files changed

+72
-276
lines changed

packages/consent/consent-tools-integration-tests/playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { PlaywrightTestConfig } from '@playwright/test'
22

33
const PORT = 4567
44
const config: PlaywrightTestConfig = {
5-
testDir: './src/playwright/tests',
5+
testDir: './src/tests',
66
timeout: 30000,
77
retries: 2,
88
webServer: {
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
import { Page } from '@playwright/test'
12
import { BasePage } from './base-page'
23

3-
class OneTrustPage extends BasePage {
4-
constructor() {
5-
super('onetrust.html')
4+
export class OneTrustPage extends BasePage {
5+
constructor(page: Page) {
6+
super(page, 'onetrust.html')
67
}
78

8-
get isOneTrustLoaded(): Promise<void> {
9-
// @ts-ignore
10-
return window.isOneTrustLoaded
9+
// Check if OneTrust is loaded by evaluating a global variable on the page
10+
async isOneTrustLoaded() {
11+
// To Do
1112
}
1213

13-
clickAcceptButtonAndClosePopup() {
14-
return $('#onetrust-accept-btn-handler').click()
14+
//Click the OneTrust accept button
15+
async clickAcceptButtonAndClosePopup() {
16+
// To Do
1517
}
1618
}
17-
18-
export default new OneTrustPage()

packages/consent/consent-tools-integration-tests/src/playwright/playwright-logger.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/consent/consent-tools-integration-tests/src/playwright/tests/consent-tools-vanilla-opt-in.test.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,94 @@
11
/**
2-
* Tests targeting @segment/analytics-consent-tools
2+
* Playwright Tests targeting @segment/analytics-consent-tools
3+
*
4+
* This test verifies that:
5+
* - Tracking events do not go through before explicit consent.
6+
* - After giving consent, appropriate events are sent with the correct consent context.
37
*/
48

59
import { ConsentToolsVanillaOptIn } from '../page-objects/consent-tools-vanilla'
6-
import { expect } from 'expect'
10+
import { test, expect } from '@playwright/test'
711

8-
const page = new ConsentToolsVanillaOptIn()
12+
let pageObject: ConsentToolsVanillaOptIn
913

10-
afterEach(async () => {
11-
await page.cleanup()
14+
test.beforeEach(async ({ page }) => {
15+
// Initialize the test page object and load the test HTML fixture
16+
pageObject = new ConsentToolsVanillaOptIn(page)
17+
await pageObject.load()
1218
})
1319

14-
it('should send a track call after waiting for explicit consent', async () => {
15-
await page.load()
20+
test.afterEach(async () => {
21+
// Cleanup local/session storage and reset intercepted requests after each test
22+
await pageObject.cleanup()
23+
})
1624

17-
void browser.execute(() => {
18-
return window.analytics.track('buffered')
25+
test('should send a track call after waiting for explicit consent', async ({
26+
page,
27+
}) => {
28+
// Attempt to track an event before consent is given — this should be buffered/blocked
29+
await page.evaluate(() => {
30+
void window.analytics.track('buffered')
1931
})
2032

21-
try {
22-
// we don't expect any tracking events to go through yet
23-
await browser.waitUntil(() => page.getAllTrackingEvents().length, {
24-
timeout: 7000,
25-
})
26-
throw new Error('Expected no track calls to be made')
27-
} catch (err) {
28-
// expected to fail
29-
}
33+
// Optional delay to let the network pipeline settle (useful in debugging)
34+
await page.waitForTimeout(2000)
3035

31-
await page.clickGiveConsent()
36+
// Assert that no events have been tracked yet
37+
await expect.poll(() => pageObject.getAllTrackingEvents().length).toBe(0)
3238

33-
await browser.waitUntil(() => page.getConsentChangedEvents().length, {
34-
timeout: 20000,
35-
timeoutMsg: 'Expected a consent change call to be made',
36-
})
39+
// Ensure analytics is loaded before interacting
40+
await page.waitForFunction(() => typeof window.analytics !== 'undefined')
3741

38-
const consentChangeEvents = page.getConsentChangedEvents()
39-
expect(consentChangeEvents.length).toBe(1)
40-
expect(consentChangeEvents[0].event).toEqual(
41-
'Segment Consent Preference Updated'
42-
)
42+
// Simulate user giving consent
43+
await pageObject.clickGiveConsent()
4344

44-
await browser.waitUntil(() => page.fetchIntegrationReqs.length, {
45-
timeout: 20000,
46-
timeoutMsg: 'Expected integrations/destinations to be fetched',
47-
})
45+
// Wait until the consent change event is tracked
46+
await expect
47+
.poll(() => pageObject.getConsentChangedEvents().length, {
48+
timeout: 10_000,
49+
})
50+
.toBeGreaterThan(0)
4851

49-
await browser.execute(() => {
50-
return window.analytics.track('hello')
52+
// Validate that the expected consent change event was emitted
53+
const events = pageObject.getConsentChangedEvents()
54+
expect(events.length).toBe(1)
55+
expect(events[0].event).toBe('Segment Consent Preference Updated')
56+
57+
// Now track a new event "hello" after consent has been given
58+
await page.evaluate(() => {
59+
void window.analytics.track('hello')
5160
})
5261

53-
const getHelloTrackEvents = () =>
54-
page.getAllTrackingEvents().find((el) => el.event === 'hello')
62+
// Helper to retrieve the "hello" tracking event
63+
const getHelloEvent = () =>
64+
pageObject.getAllTrackingEvents().find((e) => e.event === 'hello')
5565

56-
await browser.waitUntil(() => getHelloTrackEvents(), {
57-
timeout: 20000,
58-
timeoutMsg: 'Expected a hello track call to be made',
59-
})
66+
// Wait until "hello" event is tracked
67+
await expect.poll(getHelloEvent).not.toBe(undefined)
6068

61-
expect(getHelloTrackEvents()!.context?.consent).toEqual({
69+
// Validate that the "hello" event includes the correct consent context
70+
expect((await getHelloEvent())?.context?.consent).toEqual({
6271
categoryPreferences: {
6372
FooCategory1: true,
6473
FooCategory2: true,
6574
},
6675
})
6776

68-
const getBufferTrackEvents = () =>
69-
page.getAllTrackingEvents().find((el) => el.event === 'buffered')
77+
// Now re-check the previously buffered event "buffered"
78+
const getBufferedEvent = () =>
79+
pageObject.getAllTrackingEvents().find((e) => e.event === 'buffered')
7080

71-
await browser.waitUntil(() => getBufferTrackEvents(), {
72-
timeout: 20000,
73-
timeoutMsg: 'Expected a "BUFFER" track call to be made',
74-
})
81+
// Wait until the "buffered" event is finally flushed and sent
82+
await expect.poll(getBufferedEvent).not.toBe(undefined)
7583

76-
expect(getBufferTrackEvents()!.context?.consent).toEqual({
84+
// Assert that the flushed "buffered" event also includes the consent context
85+
expect((await getBufferedEvent())?.context?.consent).toEqual({
7786
categoryPreferences: {
7887
FooCategory1: true,
7988
FooCategory2: true,
8089
},
8190
})
91+
92+
// Optional: print all tracking events for debug purposes
93+
//console.log('All tracking events so far:', pageObject.getAllTrackingEvents())
8294
})

packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla-opt-out.test.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect } from '@playwright/test'
22

3-
test('Homepage has welcome message', async ({ page }) => {
3+
test('Example Test Case to check playwright is installed', async ({ page }) => {
44
await page.goto('https://www.example.com/')
55
await expect(page.locator('text=Example Domain')).toBeVisible()
66
})

0 commit comments

Comments
 (0)