In this blog we will utilize Playwright Java to maximize the browser window using 2 methods.
Topics that we will cover:
- Maximize browser window (Viewportsize method)
- Maximize browser window (argument method)
- Source code (Viewportsize)
- Source code (argument)
Maximize browser window (Viewportsize method)
Let us run below piece of code
Notice below that the browser is not in maximized state
In order to maximize the browser window, we can make use of the setViewportSize(width,height) method in playwright.
We can set the width and height according to dimensions of our computer. To find out the width and height of your computer, can simply go to https://whatismyviewport.com/
We can now set these as width and height, see line#29
Execute.
The maximized browser gets launched, see below, however we still see little bit gap on the left hand side
We will see how to overcome this shortcoming in next topic.
Maximize browser window (argument method)
In order to fully maximize the browser window, we can make use of the start-maximized argument.
We have to create an array list of arguments and simply add start-maximized argument as shown below
We than have to set the view port size as ‘null’ as shown below
So our entire code looks like:
Execute.
Notice below this time that the browser does get 100percent maximized
So this is how we can use the 2 browser maximizing techniques based on our needs.
Source code (Viewportsize)
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public
class Blog18_MaximizeWindowMethod1 {
@Test
public
void PlaywrightJTest() throws InterruptedException {
Playwright playwright = Playwright.create();
LaunchOptions launchOptions = new LaunchOptions();
launchOptions.setChannel("msedge");
launchOptions.setHeadless(false);
BrowserType browserType = playwright.chromium();
Browser browser = browserType.launch((new BrowserType.LaunchOptions().setHeadless(false)));
//Page page = browser.newPage();
//Method 1 to maximize window:
BrowserContext browserContext = browser.newContext(new Browser.NewContextOptions().setViewportSize(1366,768));
Page page = browserContext.newPage();
page.navigate("https://rahulshettyacademy.com/");
page.pause();
}
}
Source code (argument)
package com.rsa.playwrightjava;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public
class Blog18_MaximizeWindowMethod2 {
@Test
public
void PlaywrightJTest() throws InterruptedException {
Playwright playwright = Playwright.create();
ArrayList<String> arguments = new ArrayList<>();
arguments.add("--start-maximized");
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false).setArgs(arguments));
BrowserContext browserContext = browser.newContext(new Browser.NewContextOptions().setViewportSize(null));
Page page = browserContext.newPage();
page.navigate("https://rahulshettyacademy.com/learning-path");
page.pause();
}
}
Thanks.