-
Notifications
You must be signed in to change notification settings - Fork 155
Migrate consent integration tests to playwright from webdriver.io - LIBRARIES-2546 #1279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
26fb722
Added test case file: consent-tools-vanilla-opt-in.test.ts
neelkanth-kaushik e48f302
Replaced One Test Case from WebDriver.IO to Playwright: consent-tools…
neelkanth-kaushik 86ffd28
Replaced One Test Case from WebDriver.IO to Playwright: consent-tools…
neelkanth-kaushik 64136f7
Removed wdio.conf.local.ts
neelkanth-kaushik 5a31945
Updated the code after Seth's feedback on the PR
neelkanth-kaushik 127f683
Added Opt out test case
neelkanth-kaushik 3528d93
Updated code after Seth's review
neelkanth-kaushik f2b70d4
Updated code after Seth's review
neelkanth-kaushik c82ee1d
Update the code of mockCDNSettingsAPI() to use CDNSettings
neelkanth-kaushik 9a5cfa9
Update the code of mockCDNSettingsAPI() to use CDNSettings
neelkanth-kaushik eb27030
Replaced any[] with SegmentEvent[]
neelkanth-kaushik 355a289
Removed any[] and replaced it with proper interfaces
neelkanth-kaushik 68fd11a
WIP: One Trust Test Case
neelkanth-kaushik 6cac5f9
WIP: One Trust Test Case
neelkanth-kaushik 0f52a33
WIP: One Trust Test Case
neelkanth-kaushik cc45627
Skipped One Trust Case
neelkanth-kaushik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/consent/consent-tools-integration-tests/playwright.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import type { PlaywrightTestConfig } from '@playwright/test' | ||
| import { devices } from '@playwright/test' | ||
| import path from 'path' | ||
|
|
||
| /** | ||
| * See https://playwright.dev/docs/test-configuration. | ||
| */ | ||
| const config: PlaywrightTestConfig = { | ||
| webServer: { | ||
| command: 'yarn serve', | ||
| url: 'http://127.0.0.1:5432', | ||
| reuseExistingServer: !process.env.CI, | ||
| }, | ||
| testDir: './src/tests', | ||
| globalSetup: path.resolve(__dirname, 'playwright.global-setup.ts'), | ||
| /* Maximum time one test can run for. */ | ||
| timeout: 30 * 1000, | ||
| expect: { | ||
| /** | ||
| * Maximum time expect() should wait for the condition to be met. | ||
| * For example in `await expect(locator).toHaveText();` | ||
| */ | ||
| timeout: 5000, | ||
| }, | ||
| /* Run tests in files in parallel */ | ||
| fullyParallel: true, | ||
| /* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
| forbidOnly: !!process.env.CI, | ||
| /* Retry on CI only */ | ||
| retries: process.env.CI ? 2 : 0, | ||
| /* Opt out of parallel tests on CI. */ | ||
| workers: process.env.CI ? 1 : undefined, | ||
| /* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
| reporter: [['html', { open: 'never' }]], | ||
| /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
| use: { | ||
| /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
| baseURL: `http://127.0.0.1:5432`, | ||
| trace: 'on', | ||
| launchOptions: { | ||
| args: ['--enable-precise-memory-info', '--js-flags=--expose-gc'], | ||
| }, | ||
| }, | ||
|
|
||
| /* Configure projects for major browsers */ | ||
| projects: [ | ||
| { | ||
| name: 'chromium', | ||
| use: { | ||
| ...devices['Desktop Chrome'], | ||
| }, | ||
| }, | ||
| ], | ||
| } | ||
|
|
||
| export default config |
8 changes: 8 additions & 0 deletions
8
packages/consent/consent-tools-integration-tests/playwright.global-setup.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { FullConfig } from '@playwright/test' | ||
| import { execSync } from 'child_process' | ||
|
|
||
| export default function globalSetup(_cfg: FullConfig) { | ||
| console.log('Executing global setup...') | ||
| execSync('yarn build', { stdio: 'inherit' }) | ||
| console.log('Finished global setup.') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/consent/consent-tools-integration-tests/src/page-bundles/onetrust/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { AnalyticsBrowser } from '@segment/analytics-next' | ||
| import { initMockConsentManager } from '../helpers/mock-cmp' | ||
| import { withMockCMP } from '../helpers/mock-cmp-wrapper' | ||
|
|
||
| initMockConsentManager({ | ||
| consentModel: 'opt-in', | ||
| }) | ||
| //import { withOneTrust } from '@segment/analytics-consent-wrapper-onetrust' | ||
|
|
||
| // declare global { | ||
| // interface Window { | ||
| // withOneTrust: typeof import('@segment/analytics-consent-wrapper-onetrust').withOneTrust | ||
| // } | ||
| // } | ||
| // window.withOneTrust = withOneTrust | ||
|
|
||
| const analytics = new AnalyticsBrowser() | ||
|
|
||
| withMockCMP(analytics).load( | ||
| { | ||
| writeKey: 'foo', | ||
| }, | ||
| { initialPageview: true } | ||
| ) | ||
| ;(window as any).analytics = analytics |
139 changes: 69 additions & 70 deletions
139
packages/consent/consent-tools-integration-tests/src/page-objects/base-page.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,124 +1,123 @@ | ||
| import { CDNSettingsBuilder } from '@internal/test-helpers' | ||
| import type { SegmentEvent } from '@segment/analytics-next' | ||
| import assert from 'assert' | ||
| import type { Matches } from 'webdriverio' | ||
|
|
||
| const waitUntilReady = () => | ||
| browser.waitUntil( | ||
| () => browser.execute(() => document.readyState === 'complete'), | ||
| { | ||
| timeout: 10000, | ||
| } | ||
| ) | ||
| import { Page, Route, Request } from '@playwright/test' | ||
| import { SegmentEvent } from '@segment/analytics-next' | ||
| import { CoreExtraContext } from '@segment/analytics-core' | ||
|
|
||
| export abstract class BasePage { | ||
| constructor(protected page: string) {} | ||
| protected page: Page | ||
| protected pageFile: string | ||
|
|
||
| segmentTrackingApiReqs: Matches[] = [] | ||
| fetchIntegrationReqs: Matches[] = [] | ||
| segmentTrackingApiReqs: SegmentEvent[] = [] | ||
| fetchIntegrationReqs: CoreExtraContext[] = [] | ||
|
|
||
| constructor(page: Page, pageFile: string) { | ||
| this.page = page | ||
| this.pageFile = pageFile | ||
| } | ||
|
|
||
| async load(): Promise<void> { | ||
| const baseURL = browser.options.baseUrl | ||
| assert(baseURL) | ||
| await this.mockAPIs() | ||
| await browser.url(baseURL + '/public/' + this.page) | ||
| await waitUntilReady() | ||
| await this.page.goto(`/${this.pageFile}`) | ||
| await this.page.waitForLoadState('load') | ||
| } | ||
|
|
||
| async clearStorage() { | ||
| await browser.deleteAllCookies() | ||
| await browser.execute(() => window.localStorage.clear()) | ||
| await this.page.context().clearCookies() | ||
| await this.page.evaluate(() => { | ||
| window.localStorage.clear() | ||
| window.sessionStorage.clear() | ||
| }) | ||
| } | ||
|
|
||
| async cleanup() { | ||
| this.segmentTrackingApiReqs = [] | ||
| this.fetchIntegrationReqs = [] | ||
| await this.clearStorage() | ||
| } | ||
|
|
||
| getAllTrackingEvents(): SegmentEvent[] { | ||
| const reqBodies = this.segmentTrackingApiReqs.map((el) => | ||
| JSON.parse(el.postData!) | ||
| ) | ||
| return reqBodies | ||
| return this.segmentTrackingApiReqs | ||
| } | ||
|
|
||
| getConsentChangedEvents(): SegmentEvent[] { | ||
| const reqBodies = this.getAllTrackingEvents() | ||
| const consentEvents = reqBodies.filter( | ||
| return this.getAllTrackingEvents().filter( | ||
| (el) => el.event === 'Segment Consent Preference Updated' | ||
| ) | ||
| return consentEvents | ||
| } | ||
|
|
||
| async cleanup() { | ||
| this.segmentTrackingApiReqs = [] | ||
| this.fetchIntegrationReqs = [] | ||
| await this.clearStorage() | ||
| get fetchIntegrationReqsData(): CoreExtraContext[] { | ||
| return this.fetchIntegrationReqs | ||
| } | ||
|
|
||
| async mockAPIs() { | ||
| protected async mockAPIs() { | ||
| await this.mockSegmentTrackingAPI() | ||
| await this.mockCDNSettingsAPI() | ||
| await this.mockNextIntegrationsAPI() | ||
| } | ||
|
|
||
| private async mockSegmentTrackingAPI(): Promise<void> { | ||
| const mock = await browser.mock('https://api.segment.io/v1/t', { | ||
| method: 'post', | ||
| }) | ||
| mock.respond((mock) => { | ||
| this.segmentTrackingApiReqs.push(mock) | ||
| // response with status 200 | ||
| return Promise.resolve({ | ||
| statusCode: 200, | ||
| body: JSON.stringify({ success: true }), | ||
| }) | ||
| }) | ||
| private async mockSegmentTrackingAPI() { | ||
| await this.page.route( | ||
| 'https://api.segment.io/v1/t', | ||
| async (route: Route, request: Request) => { | ||
| const postData = await request.postData() | ||
| const parsed = JSON.parse(postData || '{}') | ||
| this.segmentTrackingApiReqs.push(parsed) // store the parsed event object directly | ||
|
|
||
| await route.fulfill({ | ||
| status: 200, | ||
| contentType: 'application/json', | ||
| body: JSON.stringify({ success: true }), | ||
| }) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| private async mockNextIntegrationsAPI(): Promise<void> { | ||
| const mock = await browser.mock('**/next-integrations/**') | ||
| mock.respond((mock) => { | ||
| this.fetchIntegrationReqs.push(mock) | ||
| return Promise.resolve({ | ||
| statusCode: 200, | ||
| body: 'console.log("mocking action and classic destinations")', | ||
| }) | ||
| }) | ||
| private async mockNextIntegrationsAPI() { | ||
| await this.page.route( | ||
| '**/next-integrations/**', | ||
| async (route: Route, request: Request) => { | ||
| this.fetchIntegrationReqs.push({ url: request.url() }) | ||
| await route.fulfill({ | ||
| status: 200, | ||
| body: 'console.log("mocking action and classic destinations")', | ||
| contentType: 'application/javascript', | ||
silesky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| /** * Mock the CDN Settings endpoint so that this can run offline | ||
| */ | ||
| private async mockCDNSettingsAPI(): Promise<void> { | ||
| const settings = new CDNSettingsBuilder({ | ||
| writeKey: 'something', | ||
| const cdnSettings = new CDNSettingsBuilder({ | ||
| writeKey: 'foo', | ||
| }) | ||
| .addActionDestinationSettings( | ||
| { | ||
| url: 'https://cdn.segment.com/next-integrations/actions/fullstory-plugins/foo.js', | ||
| creationName: 'FullStory', | ||
| url: 'https://cdn.segment.com/next-integrations/actions/fullstory-plugins/foo.js', | ||
| consentSettings: { | ||
| categories: ['FooCategory2'], | ||
| }, | ||
| }, | ||
| { | ||
| url: 'https://cdn.segment.com/next-integrations/actions/amplitude-plugins/foo.js', | ||
| creationName: 'Actions Amplitude', | ||
| url: 'https://cdn.segment.com/next-integrations/actions/amplitude-plugins/foo.js', | ||
| consentSettings: { | ||
| categories: ['FooCategory1'], | ||
| }, | ||
| } | ||
| ) | ||
| .build() | ||
|
|
||
| const mock = await browser.mock('**/settings') | ||
| mock.respond(settings, { | ||
| statusCode: 200, | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| await this.page.route('**/settings', async (route: Route) => { | ||
| await route.fulfill({ | ||
| status: 200, | ||
| contentType: 'application/json', | ||
| body: JSON.stringify(cdnSettings), | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Hard reload the page | ||
| */ | ||
| reload() { | ||
| return browser.execute(() => window.location.reload()) | ||
| async reload() { | ||
| await this.page.reload() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is still failing after pushing yarn.lock. I will look into it separately once all the test cases have been migrated.