Playwright - JavaScript -最大化浏览器

7gcisfzg  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(187)

我正在尝试使用Playwright最大化浏览器窗口。我尝试了下面的代码,但浏览器没有最大化到完整模式。

hooks.cjsclass:

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()
});

使用npm:npm run test -- --tags "@Begin"
我已经尝试了很多方法,但我不能启动浏览器最大化。我该怎么做?

wh6knrhe

wh6knrhe1#

如果你想最大化地启动浏览器,那么在启动浏览器时使用--start-maximized标志,并在启动上下文或页面时禁用固定视口。例如

global.browser = await playwright['chromium'].launch({ headless: false,
                 args:['--start-maximized']});
global.context = await global.browser.newContext({ viewport: null})

在python中,使用等效的no_viewport arg,并在启动上下文时将其作为True传递

gwbalxhn

gwbalxhn2#

您可以在浏览器启动期间传递如下参数:

const browser=  await chromium.launch({ headless: false,
                    args:['--window-size=1920,1040']})

const context= await browser.newContext()

const page = await context.newPage()

相关问题