I am trying to maximize the browser window using Playwright. I tried below code but the browser is not maximized to full mode.
hooks.cjs class:
const playwright = require('playwright');
const { BeforeAll, Before, After, AfterAll , Status } = require('@cucumber/cucumber');
// Launch options.
const options = {
slowMo: 100,
ignoreHTTPSErrors: true,
};
// Create a global browser for the test session.
BeforeAll(async () => {
console.log('Launch Browser');
global.browser = await playwright['chromium'].launch({ headless: false,
args:['--window-size=1920,1040']}); // --start-maximized //defaultViewport: null
//global.context = await playwright['chromium'].launch({ args: ['--start-maximized'] });
})
// Create a new browser context for each test.
Before(async () => {
console.log('Create a new Context and Page')
global.context = await global.browser.newContext()
global.page = await global.context.newPage()
})
// Close the page and context after each test.
After(async () => {
console.log('Close Context and Page');
await global.page.close();
await global.context.close();
})
// Take Screenshot if Scenario Fails
After(async function (scenario) {
if (scenario.result.status === Status.FAILED) {
const buffer = await global.page.screenshot({ path: `reports/${scenario.pickle.name}.png`, fullPage: true })
this.attach(buffer, 'image/png');
}
})
// Close Browser
AfterAll(async () => {
console.log('close Browser')
await global.browser.close()
});
I'm running with npm: npm run test -- --tags "@Begin"
I have tried in many ways, but I cannot launch browser maximized. How can I do this?
I am trying to maximize the browser window using Playwright. I tried below code but the browser is not maximized to full mode.
hooks.cjs class:
const playwright = require('playwright');
const { BeforeAll, Before, After, AfterAll , Status } = require('@cucumber/cucumber');
// Launch options.
const options = {
slowMo: 100,
ignoreHTTPSErrors: true,
};
// Create a global browser for the test session.
BeforeAll(async () => {
console.log('Launch Browser');
global.browser = await playwright['chromium'].launch({ headless: false,
args:['--window-size=1920,1040']}); // --start-maximized //defaultViewport: null
//global.context = await playwright['chromium'].launch({ args: ['--start-maximized'] });
})
// Create a new browser context for each test.
Before(async () => {
console.log('Create a new Context and Page')
global.context = await global.browser.newContext()
global.page = await global.context.newPage()
})
// Close the page and context after each test.
After(async () => {
console.log('Close Context and Page');
await global.page.close();
await global.context.close();
})
// Take Screenshot if Scenario Fails
After(async function (scenario) {
if (scenario.result.status === Status.FAILED) {
const buffer = await global.page.screenshot({ path: `reports/${scenario.pickle.name}.png`, fullPage: true })
this.attach(buffer, 'image/png');
}
})
// Close Browser
AfterAll(async () => {
console.log('close Browser')
await global.browser.close()
});
I'm running with npm: npm run test -- --tags "@Begin"
I have tried in many ways, but I cannot launch browser maximized. How can I do this?
Share Improve this question edited Nov 7, 2022 at 2:49 ggorlen 57k8 gold badges110 silver badges150 bronze badges asked Nov 7, 2022 at 1:52 nosequeweaponernosequeweaponer 6781 gold badge18 silver badges43 bronze badges 2 |4 Answers
Reset to default 7If you want to start your browser maximized, then use the --start-maximized
flag when starting the browser, and disable fixed viewport when launching a context or page. Example
global.browser = await playwright['chromium'].launch({ headless: false,
args:['--start-maximized']});
global.context = await global.browser.newContext({ viewport: null})
In python, use the equivalent no_viewport
arg and pass it as True
when starting a context
You need 3 things:
- Add
args: ["--start-maximized"]
to thelaunchOptions
- Remove the DeviceDescriptor from your project
- Set
viewport: null
in your project
Example playwright.config.ts
file:
import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
use: {
launchOptions: {
// 1
args: ["--start-maximized"],
},
},
projects: [
{
name: "chromium",
use: {
// 2 (Make sure device is not set)
// ...devices["Desktop Chrome"],
// 3
viewport: null,
},
},
],
});
In my example I'm using Playwright's config file - best practice in most cases,
but this can be done in the test itself - as long as you follow the three key-things list.
You can pass arguments like below during browser launch:
const browser= await chromium.launch({ headless: false,
args:['--window-size=1920,1040']})
const context= await browser.newContext()
const page = await context.newPage()
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'],
deviceScaleFactor: undefined,
viewport:null,
launchOptions: {
args: ["--start-maximized"],
}
},
}
]
Please find the projects
in playwright.config.ts file and this should work.
viewport: { width: 1920, height: 1040 }
instead of args in the launch options. – Mohit Kushwaha Commented Nov 7, 2022 at 6:31