You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Playwright运行时如何判断当前是否处于headless模式?

解决Playwright Headed/Debug模式下跳过步骤、添加调试信息的方案

1. 用环境变量控制流程分支

通过自定义环境变量,在测试代码里做逻辑分支判断,灵活跳过登录或添加调试信息:

启动测试时设置环境变量:

# Linux/macOS
DEBUG_MODE=1 npx playwright test --headed

# Windows
set DEBUG_MODE=1 && npx playwright test --headed

测试代码中判断并执行对应逻辑:

import { test } from '@playwright/test';

test('示例测试', async ({ page }) => {
  // 非debug模式走完整登录流程
  if (!process.env.DEBUG_MODE) {
    await page.goto('/login');
    await page.fill('#username', 'testuser');
    await page.fill('#password', 'testpass');
    await page.click('#submit');
  } else {
    // debug/headed模式直接进入目标页,跳过登录
    await page.goto('/dashboard');
    // 仅debug模式下添加调试操作
    console.log('Debug模式:已跳过登录,当前页面URL:', page.url());
    await page.screenshot({ path: 'debug-dashboard.png' });
  }

  // 通用测试逻辑
  await page.click('#create-item');
  // ...
});

2. 复用登录状态(最省心的跳过登录方案)

提前保存登录后的会话状态,在debug/headed模式下直接加载,无需重复执行登录步骤:

先写一个单独的脚本保存登录状态:

// save-login-state.js
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('/login');
  await page.fill('#username', 'testuser');
  await page.fill('#password', 'testpass');
  await page.click('#submit');
  await page.waitForURL('/dashboard'); // 等待登录跳转完成
  await page.context().storageState({ path: 'login-state.json' }); // 保存状态到文件
  await browser.close();
})();

测试代码中根据模式加载状态:

import { test } from '@playwright/test';

test.use({
  // 仅debug/headed模式加载登录状态
  storageState: process.env.DEBUG_MODE ? 'login-state.json' : undefined
});

test('示例测试', async ({ page }) => {
  if (!process.env.DEBUG_MODE) {
    await page.goto('/login');
    await page.fill('#username', 'testuser');
    await page.fill('#password', 'testpass');
    await page.click('#submit');
  } else {
    await page.goto('/dashboard');
    await page.pause(); // 开启Playwright Inspector调试(仅debug模式可用)
  }

  // 后续测试逻辑
  // ...
});

3. 利用Playwright内置标识判断模式

直接通过Playwright的配置参数判断当前是否为headed或debug模式:

import { test } from '@playwright/test';

test('示例测试', async ({ page }) => {
  const isHeaded = test.info().config.projects[0].use.headed;
  const isDebug = test.info().config.debug;

  if (!isHeaded && !isDebug) {
    // 仅无头模式执行登录
    await page.goto('/login');
    await page.fill('#username', 'testuser');
    await page.fill('#password', 'testpass');
    await page.click('#submit');
  } else {
    await page.goto('/dashboard');
    if (isDebug) {
      // 仅debug模式输出详细调试信息
      console.log('页面所有元素数量:', await page.locator('*').count());
      console.log('当前页面标题:', await page.title());
    }
  }

  // 通用测试逻辑
  // ...
});

启动debug模式直接用:

npx playwright test --debug

启动headed模式用:

npx playwright test --headed

内容的提问来源于stack exchange,提问作者Moshisho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 10:45:06