Skip to content

Commit

Permalink
Revert "Extract repeated pattern"
Browse files Browse the repository at this point in the history
This reverts commit 40def25.
  • Loading branch information
pauldambra committed Dec 29, 2024
1 parent 40def25 commit 28d4f66
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 83 deletions.
16 changes: 8 additions & 8 deletions playwright/session-recording/opting-out.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '../utils/posthog-playwright-test-base'
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'
import { BrowserContext, Page } from '@playwright/test'
import { PostHogConfig } from '../../src/types'
Expand Down Expand Up @@ -57,10 +57,10 @@ test.describe('Session Recording - opting out', () => {
await startWith({ opt_out_capturing_by_default: true }, page, context)

await page.waitingForNetworkCausedBy(['**/recorder.js*'], async () => {
await page.evaluate(async () => {
const ph = await page.posthog()
ph.opt_in_capturing()
ph.startSessionRecording()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.opt_in_capturing()
ph?.startSessionRecording()
})
})

Expand All @@ -78,9 +78,9 @@ test.describe('Session Recording - opting out', () => {

await page.waitingForNetworkCausedBy(['**/recorder.js*'], async () => {
await page.resetCapturedEvents()
await page.evaluate(async () => {
const ph = await page.posthog()
ph.startSessionRecording()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.startSessionRecording()
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '../utils/posthog-playwright-test-base'
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'

const startOptions = {
Expand All @@ -23,9 +23,9 @@ test.describe('session recording in array.full.js', () => {
await page.locator('[data-cy-input]').fill('hello posthog!')
})

await page.evaluate(async () => {
const ph = await page.posthog()
ph.capture('test_registered_property')
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$pageview', '$snapshot', 'test_registered_property'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } 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 Down Expand Up @@ -30,19 +30,19 @@ test.describe('Session recording - multiple ingestion controls', () => {

test('respects sampling when overriding linked flag', async ({ page }) => {
await page.waitingForNetworkCausedBy(['**/recorder.js*'], async () => {
await page.evaluate(async () => {
const ph = await page.posthog()
ph.opt_in_capturing()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.opt_in_capturing()
// this won't start recording because of the linked flag and sample rate
ph.startSessionRecording()
ph?.startSessionRecording()
})
})

await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])

await page.evaluate(async () => {
const ph = await page.posthog()
ph.startSessionRecording({ linked_flag: true })
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.startSessionRecording({ linked_flag: true })
})
await page.locator('[data-cy-input]').type('hello posthog!')
// there's nothing to wait for... so, just wait a bit
Expand All @@ -51,10 +51,10 @@ test.describe('Session recording - multiple ingestion controls', () => {
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])
await page.resetCapturedEvents()

await page.evaluate(async () => {
const ph = await page.posthog()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
// override all controls
ph.startSessionRecording(true)
ph?.startSessionRecording(true)
})
await page.locator('[data-cy-input]').type('hello posthog!')
await pollUntilEventCaptured(page, '$snapshot')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } 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 Down Expand Up @@ -28,20 +28,20 @@ test.describe('Session recording - linked flags', () => {

test('can opt in and override linked flag', async ({ page }) => {
await page.waitingForNetworkCausedBy(['**/recorder.js*'], async () => {
await page.evaluate(async () => {
const ph = await page.posthog()
ph.opt_in_capturing()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.opt_in_capturing()
// starting does not begin recording because of the linked flag
ph.startSessionRecording()
ph?.startSessionRecording()
})
})
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])

await page.resetCapturedEvents()

await page.evaluate(async () => {
const ph = await page.posthog()
ph.startSessionRecording({ linked_flag: true })
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.startSessionRecording({ linked_flag: true })
})
await page.locator('[data-cy-input]').type('hello posthog!')
await pollUntilEventCaptured(page, '$snapshot')
Expand Down
10 changes: 5 additions & 5 deletions playwright/session-recording/session-recording-sampling.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '../utils/posthog-playwright-test-base'
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'

const startOptions = {
Expand Down Expand Up @@ -44,10 +44,10 @@ test.describe('Session recording - sampling', () => {
})

test('can override sampling when starting session recording', async ({ page, context }) => {
await page.evaluate(async () => {
const ph = await page.posthog()
ph.startSessionRecording({ sampling: true })
ph.capture('test_registered_property')
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.startSessionRecording({ sampling: true })
ph?.capture('test_registered_property')
})
await page.expectCapturedEventsToBe(['test_registered_property'])
expect((await page.capturedEvents())[0]['properties']['$session_recording_start_reason']).toEqual(
Expand Down
72 changes: 36 additions & 36 deletions playwright/session-recording/session-recording.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '../utils/posthog-playwright-test-base'
import { expect, test, WindowWithPostHog } from '../utils/posthog-playwright-test-base'
import { start } from '../utils/setup'
import { Page } from '@playwright/test'
import { isUndefined } from '../../src/utils/type-utils'
Expand Down Expand Up @@ -74,30 +74,30 @@ test.describe('Session recording - array.js', () => {
})

test('captures session events', async ({ page }) => {
const startingSessionId = await page.evaluate(async () => {
const ph = await page.posthog()
return ph.get_session_id()
const startingSessionId = await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
return ph?.get_session_id()
})
await ensureActivitySendsSnapshots(page, ['$remote_config_received', '$session_options', '$posthog_config'])

await page.evaluate(async () => {
const ph = await page.posthog()
ph.stopSessionRecording()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.stopSessionRecording()
})

await ensureRecordingIsStopped(page)

await page.evaluate(async () => {
const ph = await page.posthog()
ph.startSessionRecording()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.startSessionRecording()
})

await ensureActivitySendsSnapshots(page, ['$session_options', '$posthog_config'])

// the session id is not rotated by stopping and starting the recording
const finishingSessionId = await page.evaluate(async () => {
const ph = await page.posthog()
return ph.get_session_id()
const finishingSessionId = await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
return ph?.get_session_id()
})
expect(startingSessionId).toEqual(finishingSessionId)
})
Expand Down Expand Up @@ -139,9 +139,9 @@ test.describe('Session recording - array.js', () => {
await page.locator('[data-cy-input]').fill('hello posthog!')
})

const firstSessionId = await page.evaluate(async () => {
const ph = await page.posthog()
return ph.get_session_id()
const firstSessionId = await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
return ph?.get_session_id()
})
const capturedEvents = await page.capturedEvents()
expect(new Set(capturedEvents.map((c) => c['properties']['$session_id']))).toEqual(new Set([firstSessionId]))
Expand All @@ -167,9 +167,9 @@ test.describe('Session recording - array.js', () => {
expect(capturedAfterActivity.map((x) => x.event)).toEqual(['$snapshot'])
expect(capturedAfterActivity[0]['properties']['$session_id']).toEqual(firstSessionId)

await page.evaluate(async () => {
const ph = await page.posthog()
ph.capture('some_custom_event')
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('some_custom_event')
})
await page.expectCapturedEventsToBe(['$snapshot', 'some_custom_event'])
const capturedAfterReload = await page.capturedEvents()
Expand All @@ -180,18 +180,18 @@ test.describe('Session recording - array.js', () => {

test('starts a new recording after calling reset', async ({ page }) => {
await page.resetCapturedEvents()
const startingSessionId = await page.evaluate(async () => {
const ph = await page.posthog()
return ph.get_session_id()
const startingSessionId = await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
return ph?.get_session_id()
})
expect(startingSessionId).not.toBeNull()

await ensureActivitySendsSnapshots(page, ['$remote_config_received', '$session_options', '$posthog_config'])

await page.resetCapturedEvents()
await page.evaluate(async () => {
const ph = await page.posthog()
ph.reset()
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.reset()
})

await page.waitingForNetworkCausedBy(['**/ses/*'], async () => {
Expand All @@ -211,9 +211,9 @@ test.describe('Session recording - array.js', () => {
await page.locator('[data-cy-input]').fill('hello posthog!')
})

await page.evaluate(async () => {
const ph = await page.posthog()
ph.capture('test_registered_property')
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$snapshot', 'test_registered_property'])
Expand All @@ -225,11 +225,11 @@ test.describe('Session recording - array.js', () => {
expect(capturedEvents[1]['properties']['$session_recording_start_reason']).toEqual('recording_initialized')

await page.resetCapturedEvents()
await page.evaluate(async () => {
const ph = await page.posthog()
const activityTs = ph.sessionManager?.['_sessionActivityTimestamp']
const startTs = ph.sessionManager?.['_sessionStartTimestamp']
const timeout = ph.sessionManager?.['_sessionTimeoutMs']
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
const activityTs = ph?.sessionManager?.['_sessionActivityTimestamp']
const startTs = ph?.sessionManager?.['_sessionStartTimestamp']
const timeout = ph?.sessionManager?.['_sessionTimeoutMs']

// move the session values back,
// so that the next event appears to be greater than timeout since those values
Expand All @@ -244,9 +244,9 @@ test.describe('Session recording - array.js', () => {
await page.locator('[data-cy-input]').type('hello posthog!')
})

await page.evaluate(async () => {
const ph = await page.posthog()
ph.capture('test_registered_property')
await page.evaluate(() => {
const ph = (window as WindowWithPostHog).posthog
ph?.capture('test_registered_property')
})

await page.expectCapturedEventsToBe(['$snapshot', 'test_registered_property'])
Expand Down
12 changes: 1 addition & 11 deletions playwright/utils/posthog-playwright-test-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,12 @@ declare module '@playwright/test' {
waitingForNetworkCausedBy: (urlPatterns: (string | RegExp)[], action: () => Promise<void>) => Promise<void>

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

posthog(): Promise<PostHog>
}
}

export const test = base.extend<{ mockStaticAssets: void; page: Page }>({
page: async ({ page }, use) => {
page.posthog = async function () {
const currentPosthog = await this.evaluate(() => {
return (window as WindowWithPostHog).posthog
})
if (!currentPosthog) {
throw new Error('PostHog not found on the page')
}
return currentPosthog
}
// Add custom methods to the page object
page.resetCapturedEvents = async function () {
await this.evaluate(() => {
;(window as WindowWithPostHog).capturedEvents = []
Expand Down

0 comments on commit 28d4f66

Please sign in to comment.