You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2025/11/15 18:22:13 UTC

(superset) 02/18: fix(playwright): auth test isolation and directory creation

This is an automated email from the ASF dual-hosted git repository.

jli pushed a commit to branch feat/dataset-playwright-tests
in repository https://gitbox.apache.org/repos/asf/superset.git

commit e66e5bd5f2f3f3ee38e88e29e792422582e3031c
Author: Joe Li <jo...@preset.io>
AuthorDate: Mon Nov 3 10:41:00 2025 -0800

    fix(playwright): auth test isolation and directory creation
    
    1. Auth State Isolation:
       - Move test.use({ storageState: undefined }) to file scope
       - Previously scoped inside test.describe, which ran after worker
         loaded global auth state, causing login redirects
       - File-scope override creates fresh context for login tests only
    
    2. Directory Creation:
       - Add mkdir for playwright/.auth before saving storageState
       - Prevents failure on first run when directory doesn't exist
    
    3. Code Quality:
       - Remove test.describe nesting (Superset testing guidelines)
       - Create authPage per-test instead of file-scoped variable
       - Ensures tests remain parallel-safe if mode changes later
       - Use TIMEOUT.PAGE_LOAD constant instead of magic 10000
       - Use URL.WELCOME constant instead of hardcoded string
       - Rely on Playwright's inferred fixture types
    
    Fixes broken auth/login tests while keeping fast global auth for
    all other tests.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-Authored-By: Claude <no...@anthropic.com>
---
 superset-frontend/playwright/global-setup.ts       |  17 +++-
 .../playwright/tests/auth/login.spec.ts            | 103 +++++++++++----------
 2 files changed, 66 insertions(+), 54 deletions(-)

diff --git a/superset-frontend/playwright/global-setup.ts b/superset-frontend/playwright/global-setup.ts
index c6798da6c9..1f65e8e15a 100644
--- a/superset-frontend/playwright/global-setup.ts
+++ b/superset-frontend/playwright/global-setup.ts
@@ -17,7 +17,14 @@
  * under the License.
  */
 
-import { chromium, FullConfig, Browser, BrowserContext } from '@playwright/test';
+import {
+  chromium,
+  FullConfig,
+  Browser,
+  BrowserContext,
+} from '@playwright/test';
+import { mkdir } from 'fs/promises';
+import { dirname } from 'path';
 import { AuthPage } from './pages/AuthPage';
 
 /**
@@ -55,11 +62,15 @@ async function globalSetup(config: FullConfig) {
     await authPage.waitForLoginSuccess();
 
     // Save authentication state for all tests to reuse
+    const authStatePath = 'playwright/.auth/user.json';
+    await mkdir(dirname(authStatePath), { recursive: true });
     await context.storageState({
-      path: 'playwright/.auth/user.json',
+      path: authStatePath,
     });
 
-    console.log('[Global Setup] Authentication successful - state saved to playwright/.auth/user.json');
+    console.log(
+      '[Global Setup] Authentication successful - state saved to playwright/.auth/user.json',
+    );
   } catch (error) {
     console.error('[Global Setup] Authentication failed:', error);
     throw error;
diff --git a/superset-frontend/playwright/tests/auth/login.spec.ts b/superset-frontend/playwright/tests/auth/login.spec.ts
index 713cd9c1a7..3ad88b51cc 100644
--- a/superset-frontend/playwright/tests/auth/login.spec.ts
+++ b/superset-frontend/playwright/tests/auth/login.spec.ts
@@ -20,69 +20,70 @@
 import { test, expect } from '@playwright/test';
 import { AuthPage } from '../../pages/AuthPage';
 import { URL } from '../../utils/urls';
+import { TIMEOUT } from '../../utils/constants';
 
-test.describe('Login view', () => {
-  let authPage: AuthPage;
+// Opt out of global authentication for login tests
+// (global-setup.ts saves auth state for all tests, but login tests need a clean slate)
+// File-scope test.use ensures fresh context without cached auth
+test.use({ storageState: undefined });
 
-  test.beforeEach(async ({ page }: any) => {
-    authPage = new AuthPage(page);
-    await authPage.goto();
-    await authPage.waitForLoginForm();
-  });
+test('should redirect to login with incorrect username and password', async ({
+  page,
+}) => {
+  // Create page object and navigate to login
+  const authPage = new AuthPage(page);
+  await authPage.goto();
+  await authPage.waitForLoginForm();
 
-  test('should redirect to login with incorrect username and password', async ({
-    page,
-  }: any) => {
-    // Setup request interception before login attempt
-    const loginRequestPromise = authPage.waitForLoginRequest();
+  // Setup request interception before login attempt
+  const loginRequestPromise = authPage.waitForLoginRequest();
 
-    // Attempt login with incorrect credentials
-    await authPage.loginWithCredentials('admin', 'wrongpassword');
+  // Attempt login with incorrect credentials
+  await authPage.loginWithCredentials('admin', 'wrongpassword');
 
-    // Wait for login request and verify response
-    const loginResponse = await loginRequestPromise;
-    // Failed login returns 401 Unauthorized or 302 redirect to login
-    expect([401, 302]).toContain(loginResponse.status());
+  // Wait for login request and verify response
+  const loginResponse = await loginRequestPromise;
+  // Failed login returns 401 Unauthorized or 302 redirect to login
+  expect([401, 302]).toContain(loginResponse.status());
 
-    // Wait for redirect to complete before checking URL
-    await page.waitForURL((url: any) => url.pathname.endsWith('login/'), {
-      timeout: 10000,
-    });
+  // Wait for redirect to complete before checking URL
+  await page.waitForURL(url => url.pathname.endsWith(URL.LOGIN), {
+    timeout: TIMEOUT.PAGE_LOAD,
+  });
 
-    // Verify we stay on login page
-    const currentUrl = await authPage.getCurrentUrl();
-    expect(currentUrl).toContain(URL.LOGIN);
+  // Verify we stay on login page
+  const currentUrl = await authPage.getCurrentUrl();
+  expect(currentUrl).toContain(URL.LOGIN);
 
-    // Verify error message is shown
-    const hasError = await authPage.hasLoginError();
-    expect(hasError).toBe(true);
-  });
+  // Verify error message is shown
+  const hasError = await authPage.hasLoginError();
+  expect(hasError).toBe(true);
+});
 
-  test('should login with correct username and password', async ({
-    page,
-  }: any) => {
-    // Setup request interception before login attempt
-    const loginRequestPromise = authPage.waitForLoginRequest();
+test('should login with correct username and password', async ({ page }) => {
+  // Create page object and navigate to login
+  const authPage = new AuthPage(page);
+  await authPage.goto();
+  await authPage.waitForLoginForm();
 
-    // Login with correct credentials
-    await authPage.loginWithCredentials('admin', 'general');
+  // Setup request interception before login attempt
+  const loginRequestPromise = authPage.waitForLoginRequest();
 
-    // Wait for login request and verify response
-    const loginResponse = await loginRequestPromise;
-    // Successful login returns 302 redirect
-    expect(loginResponse.status()).toBe(302);
+  // Login with correct credentials
+  await authPage.loginWithCredentials('admin', 'general');
 
-    // Wait for successful redirect to welcome page
-    await page.waitForURL(
-      (url: any) => url.pathname.endsWith('superset/welcome/'),
-      {
-        timeout: 10000,
-      },
-    );
+  // Wait for login request and verify response
+  const loginResponse = await loginRequestPromise;
+  // Successful login returns 302 redirect
+  expect(loginResponse.status()).toBe(302);
 
-    // Verify specific session cookie exists
-    const sessionCookie = await authPage.getSessionCookie();
-    expect(sessionCookie).not.toBeNull();
-    expect(sessionCookie?.value).toBeTruthy();
+  // Wait for successful redirect to welcome page
+  await page.waitForURL(url => url.pathname.endsWith(URL.WELCOME), {
+    timeout: TIMEOUT.PAGE_LOAD,
   });
+
+  // Verify specific session cookie exists
+  const sessionCookie = await authPage.getSessionCookie();
+  expect(sessionCookie).not.toBeNull();
+  expect(sessionCookie?.value).toBeTruthy();
 });