playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] BrowserContext.maximizeWindow()

Open mxschmitt opened this issue 5 years ago • 50 comments
trafficstars

Selenium feature: https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html#selenium.webdriver.remote.webdriver.WebDriver.maximize_window

Requested via Slack.

Scenario: Automate a website and then let the user complete something. Its great if the max possible width/height could be determined so the user has "maximum available viewport".

But not sure how or if its easy implementable since we in CR e.g. emulate the viewport independent from the window size.

For Chromium it works already like that:

// @ts-check
const playwright = require('playwright');

(async () => {
    const browser = await playwright.chromium.launch({
      args: ["--start-maximized"],
      headless: false,
    });
    const context = await browser.newContext({
      viewport: null
    });
    const page = await context.newPage();
    await page.goto('http://whatsmyuseragent.org/');
    await browser.close();
})();

mxschmitt avatar Oct 02 '20 18:10 mxschmitt

It is a great feature

linvaux avatar May 07 '21 07:05 linvaux

IT would be great to have this !

shtlrs avatar Jun 21 '21 21:06 shtlrs

Voting this up. It would "obviously" include a full screen trigger too which brings us to https://github.com/microsoft/playwright/issues/1086.

gigitalz avatar Sep 05 '21 01:09 gigitalz

Voting this up!

AlexKomanov avatar Sep 05 '21 05:09 AlexKomanov

+1 for this solution

tomekPawlak avatar Oct 01 '21 08:10 tomekPawlak

+1 for this solution

GhMartingit avatar Oct 13 '21 04:10 GhMartingit

in Selenium we have .maximize(), so de we have any such in playwright?

tmazumder avatar Nov 16 '21 11:11 tmazumder

Would really be great to have this

dsebata avatar Nov 24 '21 12:11 dsebata

Solution for Java: image

starosta357 avatar Nov 25 '21 14:11 starosta357

Upvote

alterhu2020 avatar Jan 27 '22 13:01 alterhu2020

Upvote.

sg0nzalez avatar Jan 27 '22 13:01 sg0nzalez

Upvote

doppelganger23 avatar Feb 04 '22 23:02 doppelganger23

Upvote

AlexKomanov avatar Feb 06 '22 07:02 AlexKomanov

Upvote

s2005lg avatar Feb 07 '22 05:02 s2005lg

Upvoting this as well. Use cases:

  • being able to see the whole page clearly while working on tests locally
  • allowing multiple people with different screens to work on testautomation without any problems (fixed viewport would be problematic because everyone has different monitors and resolutions)
  • running tests on Browserstack and clearly seeing what's actually happening in the videos (see the issue reported by another user here: https://playwright.slack.com/archives/CSUHZPVLM/p1646804973919919)

azad-derakhshani-GS avatar Mar 09 '22 14:03 azad-derakhshani-GS

Solution for Java: image

Unfortunately, this is not working!

gvsharshavardhan avatar Mar 17 '22 06:03 gvsharshavardhan

@azad-derakhshani-GS

allowing multiple people with different screens to work on testautomation without any problems (fixed viewport would be problematic because everyone has different monitors and resolutions)

This looks backwards to me: maximize would make pages behave differently, while fixed viewport guarantees consistent behavior. What do I miss?

pavelfeldman avatar Mar 22 '22 01:03 pavelfeldman

Upvoting!

davidhprotective avatar Mar 22 '22 17:03 davidhprotective

Upvoting

shibababa avatar Apr 05 '22 20:04 shibababa

@azad-derakhshani-GS This looks backwards to me: maximize would make pages behave differently, while fixed viewport guarantees consistent behavior. What do I miss?

A well-designed page behaves consistently across different viewports, so this is nothing for the testautomation framework to worry about. Trust me, in several years of TA experience, maximizing the browser window never caused any such issues, not even once. It's also closer to the way a real user would behave. But Playwright artificially shrinks the viewport so the pages aren't even displayed in full, which (despite it scrolling into elements) leads to page elements not being fully displayed while PW interacts with them.

Also to elaborate a bit further on the second point from my previous post: Not having a flexible (= maximized) resolution means I have to define a specific screen resolution (even null does mean 1280x720). But a static resolution could be incompatible with other people's screens. Sizing the browser window flexibly via maximize eliminates that problem.

azad-derakhshani-GS avatar Apr 06 '22 12:04 azad-derakhshani-GS

Solution in Java:

try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = (int)screenSize.getWidth(); int height = (int)screenSize.getHeight();

BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(width,height) Page page = context.newPage();

naveenanimation20 avatar May 22 '22 09:05 naveenanimation20

Upvote

aalcatelz avatar Aug 17 '22 09:08 aalcatelz

Upvote

hoangngocanhskedulo avatar Aug 29 '22 12:08 hoangngocanhskedulo

Upvote

davidhprotective avatar Sep 01 '22 14:09 davidhprotective

does anyone have solution in .net(c#) for maximizing the browser window?

dbhimar avatar Oct 10 '22 12:10 dbhimar

does anyone have solution in .net(c#) for maximizing the browser window?

The following worked for me at last.

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
    Args = new [] { "--start-maximized" },
    SlowMo = 100
});

var context = await browser.NewContextAsync(new BrowserNewContextOptions {
    ViewportSize = ViewportSize.NoViewport
});

var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");

mamoorkhan avatar Oct 10 '22 20:10 mamoorkhan

does anyone have solution in .net(c#) for maximizing the browser window?

The following worked for me at last.

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
    Args = new [] { "--start-maximized" },
    SlowMo = 100
});

var context = await browser.NewContextAsync(new BrowserNewContextOptions {
    ViewportSize = ViewportSize.NoViewport
});

var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");

Thanks Mamoorkhan,

At least the above now changes the browser size. But It is not maximizing to the full screen. Like only half - as if split screen with another app.

Any idea?

NitishJoggessur avatar Oct 11 '22 19:10 NitishJoggessur

@pavelfeldman mamoorkhan

does anyone have solution in .net(c#) for maximizing the browser window?

The following worked for me at last.

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
    Args = new [] { "--start-maximized" },
    SlowMo = 100
});

var context = await browser.NewContextAsync(new BrowserNewContextOptions {
    ViewportSize = ViewportSize.NoViewport
});

var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet/docs/intro");

@mamoorkhan Thanks, but still I am not seeing the maximized browser by using the above code

dbhimar avatar Oct 12 '22 12:10 dbhimar

@NitishJoggessur and @dbhimar I was getting the same half screen maximised window when I was using only the --start-maximized flag, but when I added the viewport settings and created a new context for the page, it worked. I am using windows 10 x64 Enterprise. I am not sure if that helps.

mamoorkhan avatar Oct 12 '22 13:10 mamoorkhan

@NitishJoggessur and @dbhimar I was getting the same half screen maximised window when I was using only the --start-maximized flag, but when I added the viewport settings and created a new context for the page, it worked. I am using windows 10 x64 Enterprise. I am not sure if that helps. @mamoorkhan

I am also using the windows 10 x64

_playwright = await Playwright.CreateAsync();

        _browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
        {
            Headless = false,
            Channel = "msedge",
            Args = new[]
                {
                    "--start-maximized"
                },
        });
       _browserContext = await _browser.NewContextAsync();
        await _browserContext.Tracing.StartAsync(new()
        {
            Screenshots = true,
            Snapshots = true,
            Sources = true
        });
        _page = await _browserContext.NewPageAsync();
        await _browser.NewContextAsync(new BrowserNewContextOptions()
        {
            RecordVideoDir = "videos/",
            ViewportSize = ViewportSize.NoViewport
        });

above is the code which I am trying to maximize the browser window

dbhimar avatar Oct 12 '22 13:10 dbhimar