Launching Maximized Chrome Window in Playwright Test (C#)

How can I start the Browser Windows from a PlayWright test (Using C#) in a Maximized state on chrome?

1 Like

Hey Mark,

// Use Playwright's C# API to launch a browser window in a maximized state on Chrome

// Create launch options for the browser
var options = new BrowserTypeLaunchOptions
{
    // Set Headless to false to run the browser in non-headless mode (visible)
    Headless = false,
    
    // Specify the browser channel (e.g., "msedge" for Microsoft Edge, "firefox" for Firefox)
    Channel = browser.GetLocalBrowser(),

    // Provide additional arguments to the browser instance
    Args = new[] { "--start-maximized" }
};

// Launch the browser with the specified options
var browserInstance = await playwright
    .GetBrowserType(browser)
    .LaunchAsync(options)
    .ConfigureAwait(false);

// Return the browser instance for further interaction in your Playwright test
return browserInstance;

Explanation:

  1. Headless Mode: The Headless property is set to false to run the browser in non-headless mode, which means the browser window will be visible.

  2. Browser Channel: The Channel property is used to specify the browser channel. For example, “msedge” for Microsoft Edge or “firefox” for Firefox. The browser.GetLocalBrowser() method is assumed to return the desired browser channel.

  3. Additional Arguments: The Args property is used to pass additional command-line arguments to the browser instance. In this case, "--start-maximized" is added to ensure that the browser window is launched in a maximized state.

  4. Launching the Browser: The LaunchAsync method is called on the BrowserType with the specified options to launch the browser instance.

  5. Returning the Browser Instance: The browser instance is returned from the function, allowing you to interact with it in your Playwright test.

Thank you for your inquiry! We’re always happy to help, so don’t hesitate to ask more.