Reputation: 2981
When you're developing E2E tests and running in headed mode, you can skip some steps (like login) to save time or add some info only in debug mode.
I've looked for a property in the documentation and in other questions but there is no answer.
Upvotes: 1
Views: 300
Reputation: 2981
Disclaimer: This isn't guaranteed to continue working in next versions (>"@playwright/test": "1.44.1"
) as it's using private parameters. But it will give the actual state of the browser regardless of how you chose it's head mode.
function isHeadless(browser) {
return browser._browserType._defaultLaunchOptions.headless
}
Of course, this requires to get the browser
param. I've used the setup method setup("Your setup...", async ({ browser }) => {
to do this.
This works on chromium and firefox as far as I've tested.
[Optional robustness addition]
public static safeAssign<T>(expression: () => T, defaultValue: T): T {
try {
return expression();
} catch (error) {
console.error('An error occurred during safeAssign:', error);
return defaultValue;
}
}
const isHeadless = Utils.safeAssign(() => (browser as any)._browserType._defaultLaunchOptions.headless, null);
Hope this will help someone.
Upvotes: 0
Reputation: 3418
Just get it from browser properties.
import { test } from "@playwright/test";
test("from browser", async ({ browser }) => {
console.log(browser["_options"].headless);
});
test("from page", async ({ page }) => {
const browser = page.context().browser()!;
console.log(browser["_options"].headless);
});
Works for use: { headless: boolean }
in playwright config. And --headed
playwright test param.
Upvotes: 1
Reputation: 21695
I would rather use the testInfo
property:
test('Only in hedless', async ({ page }, testInfo) => {
test.fixme(!testInfo.project.use.headless, 'Only works in headless mode');
});
Upvotes: 2