Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(playwright): log in via page.request instead of in-page fetch (#73275)

Georges-Antoine Assic68ca5fae352
diff --git a/playwright/e2e/sql-editor-dual-mode-postgres.spec.ts b/playwright/e2e/sql-editor-dual-mode-postgres.spec.ts
index 3305faf2a24e..d847625f5d84 100644
--- a/playwright/e2e/sql-editor-dual-mode-postgres.spec.ts
+++ b/playwright/e2e/sql-editor-dual-mode-postgres.spec.ts
@@ -63,19 +63,9 @@ test.describe('SQL Editor dual-mode synced Postgres source', () => {
INSERT INTO ${tableName} (id, label) VALUES (1, 'alpha'), (2, 'beta');
`,
])
- await page.goto('/login')
- await page.evaluate(
- async ({ email, password }) => {
- await fetch('/api/login/', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ email, password }),
- })
- },
- { email: LOGIN_USERNAME, password: LOGIN_PASSWORD }
- )
+ await page.request.post('/api/login/', {
+ data: { email: LOGIN_USERNAME, password: LOGIN_PASSWORD },
+ })
await page.goto('/')
await expect(page).toHaveURL(/\/project\/\d+/)
diff --git a/playwright/e2e/workflows-property-filter-category-dropdown.spec.ts b/playwright/e2e/workflows-property-filter-category-dropdown.spec.ts
index 58673ef77737..1338e7481c1c 100644
--- a/playwright/e2e/workflows-property-filter-category-dropdown.spec.ts
+++ b/playwright/e2e/workflows-property-filter-category-dropdown.spec.ts
@@ -93,36 +93,18 @@ test.describe('Workflows conditional branch property filter category dropdown',
const teamId: number = meData.team.id
const workflowPayload = buildConditionalBranchWorkflow()
- const workflowId = await page.evaluate(
- async ({ teamId, payload }) => {
- const csrfToken =
- document.cookie
- .split(';')
- .map((c) => c.trim())
- .find((c) => c.startsWith('posthog_csrftoken='))
- ?.split('=')
- .slice(1)
- .join('=') || ''
- if (!csrfToken) {
- throw new Error('CSRF cookie missing')
- }
- const response = await fetch(`/api/environments/${teamId}/hog_flows/`, {
- method: 'POST',
- credentials: 'include',
- headers: {
- 'Content-Type': 'application/json',
- 'X-CSRFToken': decodeURIComponent(csrfToken),
- },
- body: JSON.stringify(payload),
- })
- if (!response.ok) {
- throw new Error(`hog_flows POST failed: ${response.status} ${await response.text()}`)
- }
- const data = await response.json()
- return data.id as string
- },
- { teamId, payload: workflowPayload }
- )
+ const csrfToken = (await page.context().cookies()).find((c) => c.name === 'posthog_csrftoken')?.value
+ if (!csrfToken) {
+ throw new Error('CSRF cookie missing')
+ }
+ const response = await page.request.post(`/api/environments/${teamId}/hog_flows/`, {
+ data: workflowPayload,
+ headers: { 'X-CSRFToken': decodeURIComponent(csrfToken) },
+ })
+ if (!response.ok()) {
+ throw new Error(`hog_flows POST failed: ${response.status()} ${await response.text()}`)
+ }
+ const workflowId: string = (await response.json()).id
await test.step('open the workflow editor and select the conditional branch', async () => {
await page.goto(`/workflows/${workflowId}/workflow`)
diff --git a/playwright/utils/playwright-setup.ts b/playwright/utils/playwright-setup.ts
index 2529df306ffa..eb39c2737a2e 100644
--- a/playwright/utils/playwright-setup.ts
+++ b/playwright/utils/playwright-setup.ts
@@ -10,7 +10,7 @@
* - Team: Environment within project where data lives (e.g., "Production", "Staging")
* - User: Configurable via LOGIN_USERNAME/LOGIN_PASSWORD env vars (defaults: [email protected]/12345678)
*/
-import { APIRequestContext, Page, request as playwrightRequest } from '@playwright/test'
+import { APIRequestContext, APIResponse, Page, request as playwrightRequest } from '@playwright/test'
import { LOGIN_PASSWORD } from './playwright-test-core'
@@ -274,39 +274,28 @@ export class PlaywrightSetup {
}
async login(page: Page, workspace: PlaywrightWorkspaceSetupResult): Promise<void> {
- await page.goto(`${this.baseURL}/login`)
- await page.waitForLoadState('networkidle')
- await page.evaluate(
- async ({ email, password }) => {
- const maxAttempts = 3
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
- try {
- const res = await fetch('/api/login/', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ email, password }),
- })
- if (res.ok) {
- return
- }
- if (attempt === maxAttempts || res.status < 500) {
- throw new Error(`Login failed with status ${res.status}`)
- }
- } catch (e) {
- if (attempt === maxAttempts) {
- throw e
- }
- }
- await new Promise((r) => setTimeout(r, 500 * attempt))
+ const maxAttempts = 3
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
+ let response: APIResponse
+ try {
+ response = await page.request.post(`${this.baseURL}/api/login/`, {
+ data: { email: workspace.user_email, password: LOGIN_PASSWORD },
+ })
+ } catch (e) {
+ if (attempt === maxAttempts) {
+ throw e
}
- },
- {
- email: workspace.user_email,
- password: LOGIN_PASSWORD,
+ await new Promise((r) => setTimeout(r, 500 * attempt))
+ continue
+ }
+ if (response.ok()) {
+ return
}
- )
+ if (attempt === maxAttempts || response.status() < 500) {
+ throw new Error(`Login failed with status ${response.status()}`)
+ }
+ await new Promise((r) => setTimeout(r, 500 * attempt))
+ }
}
/**