Skip to content

Commit

Permalink
no need to start with very large test files
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 28, 2024
1 parent 6236863 commit be6e7c2
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 280 deletions.
49 changes: 49 additions & 0 deletions playwright/session-recording-array-full.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, test, WindowWithPostHog } from './utils/posthog-playwright-test-base'
import { start } from './utils/setup'

const startOptions = {
options: {
session_recording: {},
},
decideResponseOverrides: {
sessionRecording: {
endpoint: '/ses/',
},
capturePerformance: true,
autocapture_opt_out: true,
},
url: './playground/cypress-full/index.html',
}

test.describe('session recording in array.full.js', () => {
test('captures session events', async ({ page, context }) => {
await start(startOptions, page, context)

const responsePromise = page.waitForResponse('**/ses/*')
await page.locator('[data-cy-input]').fill('hello posthog!')
await responsePromise

await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('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)

// a meta and then a full snapshot
expect(snapshotData[0].type).toEqual(4) // meta
expect(snapshotData[1].type).toEqual(2) // full_snapshot
expect(snapshotData[2].type).toEqual(5) // custom event with remote config
expect(snapshotData[3].type).toEqual(5) // custom event with options
expect(snapshotData[4].type).toEqual(5) // custom event with posthog config
// Making a set from the rest should all be 3 - incremental snapshots
const incrementalSnapshots = snapshotData.slice(5)
expect(Array.from(new Set(incrementalSnapshots.map((s: any) => s.type)))).toStrictEqual([3])

expect(capturedEvents[2]['properties']['$session_recording_start_reason']).toEqual('recording_initialized')
})
})
5 changes: 5 additions & 0 deletions playwright/session-recording-network-recorder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from './utils/posthog-playwright-test-base'

test.describe('Session recording - array.js', () => {
test.fixme('network capture', () => {})
})
78 changes: 78 additions & 0 deletions playwright/session-recording-sampling.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { expect, test, WindowWithPostHog } from './utils/posthog-playwright-test-base'
import { start } from './utils/setup'

const startOptions = {
options: {
session_recording: {},
},
decideResponseOverrides: {
sessionRecording: {
endpoint: '/ses/',
},
capturePerformance: true,
autocapture_opt_out: true,
},
url: './playground/cypress/index.html',
}

test.describe('Session recording - sampling', () => {
const sampleZeroStartOptions = {
...startOptions,
decideResponseOverrides: {
...startOptions.decideResponseOverrides,
sessionRecording: {
...startOptions.decideResponseOverrides.sessionRecording,
sampleRate: '0',
},
},
}
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.resetCapturedEvents()
})

test('does not capture events when sampling is set to 0', async ({ page }) => {
await page.locator('[data-cy-input]').fill('hello posthog!')
// 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([])
})

test('can override sampling when starting session recording', async ({ page, context }) => {
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
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')

// sampling override survives a page refresh
await page.resetCapturedEvents()
await page.reload()

await start(
{
...sampleZeroStartOptions,
type: 'reload',
},
page,
context
)
await page.waitForResponse('**/recorder.js*')
const responsePromise = page.waitForResponse('**/ses/*')
await page.locator('[data-cy-input]').fill('hello posthog!')
await responsePromise

const afterReloadCapturedEvents = await page.capturedEvents()
const lastCaptured = afterReloadCapturedEvents[afterReloadCapturedEvents.length - 1]
expect(lastCaptured['event']).toEqual('$snapshot')
})
})
Loading

0 comments on commit be6e7c2

Please sign in to comment.