[Feature] create context in fullscreen mode · Issue #1086 · microsoft/playwright · GitHub
Skip to content

[Feature] create context in fullscreen mode #1086

Open

Description

@star-kashyap

How to launch browser.newContext() or browser.newPage() in fullscreen ? There is no way to pass full-screen via options while calling browser.newContext(options).

Activity

  1. aesyondu commented on Feb 25, 2020

    @aesyondu
    Contributor

    I'm not sure what you mean. Currently the default is:

    browser.newConext({
      viewport: {
        width: 800, // 1280 in commit 1ee6578
        height: 600, // 720 in commit 1ee6578
      }
    })

    If I understood correctly, you were expecting that viewport width and height will automatically be set to your current desktop/laptop's viewport?

  2. star-kashyap commented on Feb 25, 2020

    @star-kashyap
    Author

    Screenshot 2020-02-25 at 11 54 23 AM

    I am talking about this new cognito window size, which I am launching by context.newPage(). Viewport is visible area which is visible on device, however browser window size is different. Opening new browser window in full screen size is my concern. How to launch it in full screen. Passing viewport do not help.

  3. pavelfeldman commented on Feb 25, 2020

    @pavelfeldman
    Member

    I know that the user can maximize the browser window and I know that the page can request full screen mode via the web api. But I'm not sure user can create a new full screen page.

    Could you share more about your use case?

  4. star-kashyap commented on Feb 25, 2020

    @star-kashyap
    Author

    Sorry, my bad. My point is user maximize browser window. How can we get it done ? Before context.newPage(), I am getting context by browser.newContext(). Can we get new context in fullscreen mode ?

  5. changed the title [-][Question] How to launch newContext() or newPage() in fullscreen ?[/-] [+][Question] How to launch newContext() in fullscreen ?[/+] on Feb 25, 2020
  6. pavelfeldman commented on Feb 25, 2020

    @pavelfeldman
    Member

    My point is user maximize browser window

    If you call browser.newContext({ viewport: null }), it would tell Playwright to not emulate certain device size and will follow the browser size when maximized. Is that what you are looking for?

  7. aesyondu commented on Feb 26, 2020

    @aesyondu
    Contributor

    Before the viewport: null I'm getting:

    width: 800 height: 600
    

    which is expected of course, since that is the default. I tried the browser.newContext({ viewport: null }) and now I'm getting:

    width: 1200 height: 610
    

    Which does take the whole size of the browser, but the browser itself is not full screen. For reference, my full screen browser should measure:

    {width: 1280, height: 721}
    

    Side Note: not sure why there is an extra 1 pixel height, I used this method on an about:blank page:

    {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight
    }
  8. star-kashyap commented on Feb 27, 2020

    @star-kashyap
    Author

    arjun27 What do you think here ?

  9. arjunattam commented on Feb 27, 2020

    @arjunattam
    Contributor

    Synced offline with star-kashyap to understand this better. Due to DRM constraints, the tests are executed in headful mode. A small browser window impacts verifiability of execution, and looks suboptimal to an observer.

    While it is possible to use something like --start-fullscreen or --window-size as args to launch a browser, it is not possible to enlarge the size of a new window launched with newContext. Combination of enlarged window size and viewport: null would fix this.

  10. pavelfeldman commented on Feb 27, 2020

    @pavelfeldman
    Member

    I think launching with args: ['--window-size=1280,720'] and using viewport: null should bring you close to where you want to be. But that would be Chromium-specific solution. Non-chromium you should run with viewport: { width: 1280, height: 720 }.

    Please feel free to reopen if there is something else we can do for you here!

  11. pavelfeldman commented on Oct 23, 2020

    @pavelfeldman
    Member

    Reopening as a feature request.

  12. changed the title [-][Question] How to launch newContext() in fullscreen ?[/-] [+][Feature] create context in fullscreen mode[/+] on Oct 24, 2020
  13. 17 remaining items

  14. kputyra commented on Jan 25, 2023

    @kputyra

    I've solved this problem (in Python) by using launch_persistent_context() instead of launch() and tweaking the arguments a little but. This change prevents opening pages in new windows.

    from playwright.sync_api import sync_playwright as spl
    
    pwt = spl().start()
    browser = pwt.chromium.launch_persistent_context(
        # Required (emtpy string means a temporary profile)
        user_data_dir="",
        headless=False,
        # This hides the annoying infobar "Chrome is being controlled by automated test software",
        # but it may break some functionality of playwright
        ignore_default_args=["--enable-automation", "--no-startup-window"],
        # Also possible: --start-fullscreen
        args=["--kiosk"],
        no_viewport=True
    )
    page = browser.new_page()
    page.goto("https://google.com/")
    
    # Check that JavaScript can be evaluated despite removing --enable-automation
    print(page.evaluate("location.href"))
    print(page.evaluate("document.body.textContent = 'Hello world!'"))
    dimensions = page.evaluate('''() => {
            return {
                width: document.documentElement.clientWidth,
                height: document.documentElement.clientHeight,
                deviceScaleFactor: window.devicePixelRatio,
            }
        }''')
    print(dimensions)

    I've removed the default flag --enable-automation in order to hide the annoying infobar "Chrome is being controlled by automated test software". This is likely to break some features of playwright, but for me all that is important is to communicate with the page using JavaScript, which seems fine.

    Cons:

    • I don't know if this code works with other browsers as well.
    • You cannot change the context in which pages are opened.
  15. ssv-w32 commented on Jan 30, 2023

    @ssv-w32

    +1, I find this feature very useful.

  16. gs-vchauhan commented on May 17, 2023

    @gs-vchauhan

    +1 => absolutely required

  17. ManiMozaffar commented on Jun 1, 2023

    @ManiMozaffar

    do we have some solution for firefox apart from hardcoding the size? willing to see one!

  18. dron4ik86 commented on Aug 12, 2023

    @dron4ik86

    This worked for me:

    from playwright.sync_api import sync_playwright
    import time
    
    p = sync_playwright().start()
    browser = p.chromium.launch(args=['--start-maximized'], headless=False)
    page = browser.new_page(no_viewport=True)
    page.goto('https://www.google.com')
    time.sleep(3600)

    Upvoted this issue aswell 👍

    Credits/thanks to Pavel Feldman (@pavelfeldman) (https://giters.com/microsoft/playwright-python/issues/585)

    This solution in working good for me, thanks!

  19. imbdb commented on Nov 25, 2023

    @imbdb

    For some tests we needed fullscreen mode but I was not able to get it working.
    After embarrasing count of hours I figured out that Playwright exposes the underlying CDP Session and we can use it to make the window fullscreen.
    This works in chromium

      const client = await page.context().newCDPSession(page);
      // get window id from CDP session
      const { windowId } = await client.send("Browser.getWindowForTarget");
      await client.send("Browser.setWindowBounds", {
        windowId: windowId,
        bounds: { windowState: "fullscreen" },
      });

    I don't know of any side effects this might cause, will update here if found any.

  20. cybairfly commented on Feb 21, 2024

    @cybairfly

    Bharat D Bhadresha (@imbdb) You know your way around! Nice of you to share.

  21. craitose commented on Feb 28, 2024

    @craitose

    For some tests we needed fullscreen mode but I was not able to get it working. After embarrasing count of hours I figured out that Playwright exposes the underlying CDP Session and we can use it to make the window fullscreen. This works in chromium

      const client = await page.context().newCDPSession(page);
      // get window id from CDP session
      const { windowId } = await client.send("Browser.getWindowForTarget");
      await client.send("Browser.setWindowBounds", {
        windowId: windowId,
        bounds: { windowState: "fullscreen" },
      });

    I don't know of any side effects this might cause, will update here if found any.

    Yeah, I needed to test the full screen button on a web app but apparently playwright does not allow full screen to be triggered through events as it might interfere with accessibility of elements while the browser is full screen. You code snippet was the only way that allowed me to force full screen after the button was clicked.

  22. AbhineetSharmax commented on Mar 4, 2024

    @AbhineetSharmax

    For some tests we needed fullscreen mode but I was not able to get it working. After embarrasing count of hours I figured out that Playwright exposes the underlying CDP Session and we can use it to make the window fullscreen. This works in chromium

      const client = await page.context().newCDPSession(page);
      // get window id from CDP session
      const { windowId } = await client.send("Browser.getWindowForTarget");
      await client.send("Browser.setWindowBounds", {
        windowId: windowId,
        bounds: { windowState: "fullscreen" },
      });

    I don't know of any side effects this might cause, will update here if found any.

    I just made a little change to this configuration and this may help you get what you want to achieve. Try it out.

    bounds: { windowState: 'maximized' },

    Also, this is my playwright.config.ts
    projects: [
    {
    name: 'chromium',
    use: {
    // ...devices['Desktop Chrome'],
    headless: false,
    launchOptions: {
    args: ['--auth-server-allowlist="*"']
    },
    viewport: null,
    }
    },

  23. nir-tal-talkspace commented on Mar 28, 2024

    @nir-tal-talkspace

    wrote a blog post on how to resolve this issue when using playwright + python + pytest fixtures:
    https://www.linkedin.com/pulse/test-automation-maximizing-browser-window-playwright-nir-tal-c24hf/

  24. ilorwork commented on Aug 1, 2024

    @ilorwork
  25. Dav1s-Ops commented on Apr 24, 2025

    @Dav1s-Ops

    For some tests we needed fullscreen mode but I was not able to get it working. After embarrasing count of hours I figured out that Playwright exposes the underlying CDP Session and we can use it to make the window fullscreen. This works in chromium

    const client = await page.context().newCDPSession(page);
    // get window id from CDP session
    const { windowId } = await client.send("Browser.getWindowForTarget");
    await client.send("Browser.setWindowBounds", {
    windowId: windowId,
    bounds: { windowState: "fullscreen" },
    });
    I don't know of any side effects this might cause, will update here if found any.

    This is EXACTLY what I was looking for, converted to .NET in a LINQPad script for anyone looking to maximize the browser

    class Program
    {
    	static async Task Main(string[] args)
    	{
    		using var playwright = await Playwright.CreateAsync();
    
    		await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
    		{
    			Headless = false
    		});
    
    		var context = await browser.NewContextAsync();
    		var page = await context.NewPageAsync();
    		var client = await context.NewCDPSessionAsync(page);
    
    		var getWindowResult = await client.SendAsync("Browser.getWindowForTarget");
    		var windowId = getWindowResult.Value.GetProperty("windowId").GetInt32();
    
                    // Initial max to grab width and height
    		await client.SendAsync("Browser.setWindowBounds", new Dictionary<string, object>
    		{
    			{ "windowId", windowId },
    			{ "bounds", new Dictionary<string, object> { { "windowState", "maximized" } } }
    		});
                    var width = await page.EvaluateAsync<int>(@"() => window.innerWidth");
    		var height = await page.EvaluateAsync<int>(@"() => window.innerHeight");
    		
    		await page.SetViewportSizeAsync(width, height);
    
    		// Remaximize based on screen dimensions
    		await client.SendAsync("Browser.setWindowBounds", new Dictionary<string, object>
    		{
    			{ "windowId", windowId },
    			{ "bounds", new Dictionary<string, object> { { "windowState", "maximized" } } }
    		});
    		await page.GotoAsync("https://example.com");
    		await Task.Delay(5000);
    	}
    }

    Here's a link to the other options for windowState as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions