Skip to content

Commit

Permalink
Extract repeated pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 29, 2024
1 parent 49138f9 commit 2e7b0cb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
8 changes: 3 additions & 5 deletions playwright/session-recording/opting-out.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ test.describe('Session Recording - opting out', () => {

await page.locator('[data-cy-input]').type('hello posthog!')
await page.waitForTimeout(250) // short delay since there's no snapshot to wait for
const capturedEvents = await page.capturedEvents()
expect(capturedEvents).toEqual([])
await page.expectCapturedEventsToBe([])
})

test('does not capture recordings when config disables session recording', async ({ page, context }) => {
Expand All @@ -51,8 +50,7 @@ test.describe('Session Recording - opting out', () => {

await page.locator('[data-cy-input]').type('hello posthog!')
await page.waitForTimeout(250) // short delay since there's no snapshot to wait for
const capturedEvents = await page.capturedEvents()
expect(capturedEvents.map((x) => x.event)).toEqual(['$pageview'])
await page.expectCapturedEventsToBe(['$pageview'])
})

test('can start recording after starting opted out', async ({ page, context }) => {
Expand All @@ -66,7 +64,7 @@ test.describe('Session Recording - opting out', () => {
})
})

expect((await page.capturedEvents()).map((x) => x.event)).toEqual(['$opt_in', '$pageview'])
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])

await page.resetCapturedEvents()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ test.describe('session recording in array.full.js', () => {
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$pageview', '$snapshot', 'test_registered_property'])
const capturedEvents = await page.capturedEvents()
expect(capturedEvents.map((x) => x.event)).toEqual(['$pageview', '$snapshot', 'test_registered_property'])

// don't care about network payloads here
const snapshotData = capturedEvents[1]['properties']['$snapshot_data'].filter((s: any) => s.type !== 6)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'
import { assertThatRecordingStarted, pollUntilEventCaptured } from '../utils/event-capture-utils'

Expand All @@ -24,9 +24,7 @@ const startOptions = {
test.describe('Session recording - multiple ingestion controls', () => {
test.beforeEach(async ({ page, context }) => {
await start(startOptions, page, context)

const capturedEvents = await page.evaluate(() => (window as WindowWithPostHog).capturedEvents || [])
expect(capturedEvents.map((x) => x.event)).toEqual([])
await page.expectCapturedEventsToBe([])
await page.resetCapturedEvents()
})

Expand All @@ -40,7 +38,7 @@ test.describe('Session recording - multiple ingestion controls', () => {
})
})

expect((await page.capturedEvents()).map((x) => x.event)).toEqual(['$opt_in', '$pageview'])
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])

await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
Expand All @@ -50,7 +48,7 @@ test.describe('Session recording - multiple ingestion controls', () => {
// there's nothing to wait for... so, just wait a bit
await page.waitForTimeout(250)
// no new events
expect((await page.capturedEvents()).map((x) => x.event)).toEqual(['$opt_in', '$pageview'])
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])
await page.resetCapturedEvents()

await page.evaluate(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'
import { assertThatRecordingStarted, pollUntilEventCaptured } from '../utils/event-capture-utils'

Expand All @@ -22,9 +22,7 @@ const startOptions = {
test.describe('Session recording - linked flags', () => {
test.beforeEach(async ({ page, context }) => {
await start(startOptions, page, context)

const capturedEvents = await page.evaluate(() => (window as WindowWithPostHog).capturedEvents || [])
expect(capturedEvents.map((x) => x.event)).toEqual([])
await page.expectCapturedEventsToBe([])
await page.resetCapturedEvents()
})

Expand All @@ -37,7 +35,7 @@ test.describe('Session recording - linked flags', () => {
ph?.startSessionRecording()
})
})
expect((await page.capturedEvents()).map((x) => x.event)).toEqual(['$opt_in', '$pageview'])
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])

await page.resetCapturedEvents()

Expand Down
13 changes: 6 additions & 7 deletions playwright/session-recording/session-recording-sampling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ test.describe('Session recording - sampling', () => {
await start(startOptions, page, context)
})

const capturedEvents = await page.evaluate(() => (window as WindowWithPostHog).capturedEvents || [])
expect(capturedEvents.map((x) => x.event)).toEqual(['$pageview'])
await page.expectCapturedEventsToBe(['$pageview'])
await page.resetCapturedEvents()
})

Expand All @@ -41,8 +40,7 @@ test.describe('Session recording - sampling', () => {
// because it doesn't make sense to wait for a snapshot event that won't happen
await page.waitForTimeout(250)

const capturedEvents = await page.capturedEvents()
expect(capturedEvents).toEqual([])
await page.expectCapturedEventsToBe([])
})

test('can override sampling when starting session recording', async ({ page, context }) => {
Expand All @@ -51,9 +49,10 @@ test.describe('Session recording - sampling', () => {
ph?.startSessionRecording({ sampling: true })
ph?.capture('test_registered_property')
})
const capturedEvents = await page.capturedEvents()
expect(capturedEvents.map((x) => x.event)).toEqual(['test_registered_property'])
expect(capturedEvents[0]['properties']['$session_recording_start_reason']).toEqual('sampling_overridden')
await page.expectCapturedEventsToBe(['test_registered_property'])
expect((await page.capturedEvents())[0]['properties']['$session_recording_start_reason']).toEqual(
'sampling_overridden'
)

// sampling override survives a page refresh
await page.resetCapturedEvents()
Expand Down
20 changes: 10 additions & 10 deletions playwright/session-recording/session-recording.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ const startOptions = {

test.describe('Session recording - array.js', () => {
test.beforeEach(async ({ page, context }) => {
await start(startOptions, page, context)
await page.waitForResponse('**/recorder.js*')
const capturedEvents = await page.evaluate(() => (window as WindowWithPostHog).capturedEvents || [])
expect(capturedEvents.map((x) => x.event)).toEqual(['$pageview'])
await page.waitingForNetworkCausedBy(['**/recorder.js*'], async () => {
await start(startOptions, page, context)
})
await page.expectCapturedEventsToBe(['$pageview'])
await page.resetCapturedEvents()
})

Expand Down Expand Up @@ -135,9 +135,9 @@ test.describe('Session recording - array.js', () => {
})

test('continues capturing to the same session when the page reloads', async ({ page }) => {
const responsePromise = page.waitForResponse('**/ses/*')
await page.locator('[data-cy-input]').fill('hello posthog!')
await responsePromise
await page.waitingForNetworkCausedBy(['**/ses/*'], async () => {
await page.locator('[data-cy-input]').fill('hello posthog!')
})

const firstSessionId = await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
Expand Down Expand Up @@ -171,8 +171,8 @@ test.describe('Session recording - array.js', () => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('some_custom_event')
})
await page.expectCapturedEventsToBe(['$snapshot', 'some_custom_event'])
const capturedAfterReload = await page.capturedEvents()
expect(capturedAfterReload.map((x) => x.event)).toEqual(['$snapshot', 'some_custom_event'])
expect(capturedAfterReload[1]['properties']['$session_id']).toEqual(firstSessionId)
expect(capturedAfterReload[1]['properties']['$session_recording_start_reason']).toEqual('recording_initialized')
expect(capturedAfterReload[1]['properties']['$recording_status']).toEqual('active')
Expand Down Expand Up @@ -216,8 +216,8 @@ test.describe('Session recording - array.js', () => {
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$snapshot', 'test_registered_property'])
const capturedEvents = await page.capturedEvents()
expect(capturedEvents.map((x) => x.event)).toEqual(['$snapshot', 'test_registered_property'])

const firstSessionId = capturedEvents[0]['properties']['$session_id']
expect(typeof firstSessionId).toEqual('string')
Expand Down Expand Up @@ -249,8 +249,8 @@ test.describe('Session recording - array.js', () => {
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$snapshot', 'test_registered_property'])
const capturedEventsAfter24Hours = await page.capturedEvents()
expect(capturedEventsAfter24Hours.map((x) => x.event)).toEqual(['$snapshot', 'test_registered_property'])

expect(capturedEventsAfter24Hours[0]['properties']['$session_id']).not.toEqual(firstSessionId)
expect(capturedEventsAfter24Hours[0]['properties']['$snapshot_data'][0].type).toEqual(4) // meta
Expand Down
8 changes: 7 additions & 1 deletion playwright/utils/posthog-playwright-test-base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test as base, Page } from '@playwright/test'
import { test as base, Page, expect } from '@playwright/test'
import { PostHog } from '../../src/posthog-core'
import { CaptureResult } from '../../src/types'

Expand Down Expand Up @@ -31,6 +31,8 @@ declare module '@playwright/test' {
capturedEvents(): Promise<CaptureResult[]>

waitingForNetworkCausedBy: (urlPatterns: (string | RegExp)[], action: () => Promise<void>) => Promise<void>

expectCapturedEventsToBe(expectedEvents: string[]): Promise<void>
}
}

Expand Down Expand Up @@ -60,6 +62,10 @@ export const test = base.extend<{ mockStaticAssets: void; page: Page }>({
// eslint-disable-next-line compat/compat
await Promise.allSettled(responsePromises)
}
page.expectCapturedEventsToBe = async function (expectedEvents: string[]) {
const capturedEvents = await this.capturedEvents()
expect(capturedEvents.map((x) => x.event)).toEqual(expectedEvents)
}

// Pass the extended page to the test
await use(page)
Expand Down

0 comments on commit 2e7b0cb

Please sign in to comment.