microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.65k stars 3.57k forks source link

[Feature] BrowserContext.maximizeWindow() #4046

Open mxschmitt opened 3 years ago

mxschmitt commented 3 years ago

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();
})();
linvaux commented 3 years ago

It is a great feature

shtlrs commented 3 years ago

IT would be great to have this !

gigitalz commented 3 years ago

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

AlexKomanov commented 3 years ago

Voting this up!

tomekPawlak commented 2 years ago

+1 for this solution

GhMartingit commented 2 years ago

+1 for this solution

tmazumder commented 2 years ago

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

dsebata commented 2 years ago

Would really be great to have this

starosta357 commented 2 years ago

Solution for Java: image

alterhu2020 commented 2 years ago

Upvote

sg0nzalez commented 2 years ago

Upvote.

doppelganger23 commented 2 years ago

Upvote

AlexKomanov commented 2 years ago

Upvote

s2005lg commented 2 years ago

Upvote

azad-derakhshani-GS commented 2 years ago

Upvoting this as well. Use cases:

gvsharshavardhan commented 2 years ago

Solution for Java: image

Unfortunately, this is not working!

pavelfeldman commented 2 years ago

@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?

davidhprotective commented 2 years ago

Upvoting!

shibababa commented 2 years ago

Upvoting

azad-derakhshani-GS commented 2 years ago

@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.

naveenanimation20 commented 2 years ago

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

aalcatelz commented 2 years ago

Upvote

hoangngocanhskedulo commented 2 years ago

Upvote

davidhprotective commented 2 years ago

Upvote

dbhimar commented 1 year ago

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

mamoorkhan commented 1 year ago

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");
NitishJoggessur commented 1 year ago

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?

dbhimar commented 1 year ago

@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

mamoorkhan commented 1 year ago

@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.

dbhimar commented 1 year ago

@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

mamoorkhan commented 1 year ago

@dbhimar you are recreating the context in the last line. try the following code

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

Upvote

anandhaprakash-mani commented 1 year ago

Upvote!

trjo1016 commented 1 year ago

You can maximize the window by knowing which launch options each browser type has:

For Chromium, you need: --start-maximized For Firefox, you need: --kiosk

Currently trying to find out what to use for webkit. Knowing this, you can set up launch options like this, when you are launching the browser: (written in Playwright Java)

        List<String> launchOptions = new ArrayList<>();
        Playwright playwright = Playwright.create();

        // Change browser type
        BrowserType type = playwright.firefox();

        switch (type.name()) {
            case "chromium": {
                launchOptions.add("--start-maximized");
                launchOptions.add("--incognito");

                // Can be "msedge", "chrome-beta", "msedge-beta", "msedge-dev", etc.
                String browserChannel = "chrome";

                browser = type.launch(new BrowserType.LaunchOptions()
                        .setChannel(browserChannel)
                        .setArgs(launchOptions);

                break;
            }
            case "firefox": {
                launchOptions.add("--kiosk");
                launchOptions.add("--private-window");

                browser = type.launch(new BrowserType.LaunchOptions()
                        .setArgs(launchOptions);

                break;
            }
}
testgitdl commented 1 year ago

Hello,

We also need this. We cannot get it to work. We need to maximize chrome because setting the viewport has downfalls - cannot really see the popup's in our up.

Thnaks!

vishal7788 commented 1 year ago

We too need the maximized browser window. Viewport option splits the browser on my laptop and extended monitor desktop screen. Its funny to watch, half of the elements being interacted on laptop screen and other half on desktop :D

mohanbabu-zumen commented 1 year ago

How can I maximize the chromium window in js?

vbhardwaj87 commented 1 year ago

How we can maximize the browser as per screen size in playwright Java

SouhaB commented 1 year ago

Hi, I would be perfect if we have .maximize like selenium. Meanwhile, this works for me (version 1.29 java) image

mohanbabu-zumen commented 1 year ago

Does anyone have any idea about JavaScript?

dbhimar commented 1 year ago

Does anyone find the solution to maximize the window using C#.net ? @mamoorkhan its working for you?

VasanthVJ commented 1 year ago

Have tried all the possible ways but the maximize function isn't accurate. Also, when I launch browser browser in my extended screen I had to maximise manually, the inner page remains cropped and doesn't adopt the screen size. I am getting the feeling the maximise function is not accurate as Selenium.

mohanbabu-zumen commented 1 year ago

Have tried all the possible ways but the maximize function isn't accurate. Also, when I launch browser browser in my extended screen I had to maximise manually, the inner page remains cropped and doesn't adopt the screen size. I am getting the feeling the maximise function is not accurate as Selenium.

Yes, The same is occurring to me as well. It would be great If they address it ASAP!

areeba-shuja-10p commented 1 year ago

Hello. Following worked for me!

{
  name: "chromium",
  use: {
    ...devices["Desktop Chromium"],  
    viewport: null,
    launchOptions: {
      args: ["--start-maximized"]
    }
  },
},

Thanks,

mamoorkhan commented 11 months ago

Does anyone find the solution to maximize the window using C#.net ? @mamoorkhan its working for you?

hey sorry for missing this, but yes, this worked for me.

heenavasdani commented 9 months ago

Upvote!

heenavasdani commented 9 months ago

@mxschmitt , is there any workaround for it in .net when using PageTest class?

shridhar007 commented 9 months ago

Following code worked for me. image

EugeneMac commented 8 months ago

Proposed solution for .NET doesn't work for me as well. The page is still cropped and in case of dynamic UI interface with react controls and stuff PW works weird time to time which leads to flakiness for some tests. What frustrates the most the glitches often happen at nightly run on TeamCity agent and almost never when you run the same test from you laptop browser. When I inspect the trace I don't even see some web elements. When I open the trace viewport in a new page at fullsize - I see the missing web elements. So the feature is anticipated. At least as a workaround maybe there is a way to launch Chrome in some mode when it fits all the content on one viewport (zooming out the page maybe?) like it is possible in Chrome inspector.

PeterHevesi commented 6 months ago

HEY PLAYWIGHT TEAM !!!! Pleaease implement this feature ASAP. Do you see, how many of us have a problem with it? This feature reequest is from more than 3 years ago, and no improvement since then.... This is number 1 feature request you should work on, or if you don't want to solve it, then please go to remove Firefox from list of supported browsers.... Because without it, Firefox cannot be used reliably (of course I managed to run chromium based browsers maximized, that is not so hard to achieve) but why can't we run Firefox maximized?

PLEASE WORK ON THIS FEATURE ASAP... MANY PEOPLE FIND IT VERY IMPORTANT TO BE ABLE TO RUN RELIABLE TESTS !!!