{"id":3320,"date":"2025-01-16T05:14:42","date_gmt":"2025-01-16T05:14:42","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3320"},"modified":"2025-01-17T10:41:18","modified_gmt":"2025-01-17T10:41:18","slug":"maximize-browser-window-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/maximize-browser-window-using-playwright-java\/","title":{"rendered":"Maximize browser window using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will utilize Playwright Java to maximize the browser window using 2 methods.<\/em><em>&nbsp;<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Maximize browser window (Viewportsize method)<\/li>\n<li>Maximize browser window (argument method)&nbsp;<\/li>\n<li>Source code (Viewportsize)<\/li>\n<li>Source code (argument)<\/li>\n<\/ul>\n<p><strong>Maximize browser window (Viewportsize method)<\/strong><\/p>\n<p>Let us run below piece of code<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3323\" width=\"559\" data-init-width=\"1168\" height=\"210\" data-init-height=\"438\" title=\"Screenshot 2025-01-16 at 10.46.10\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.46.10\u202fAM.png\" data-width=\"559\" data-height=\"210\" style=\"aspect-ratio: auto 1168 \/ 438;\"><\/span><\/p>\n<p>&nbsp;Notice below that the browser is not in maximized state&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3324\" width=\"516\" data-init-width=\"1168\" height=\"307\" data-init-height=\"696\" title=\"Screenshot 2025-01-16 at 10.46.44\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.46.44\u202fAM.png\" data-width=\"516\" data-height=\"307\" style=\"aspect-ratio: auto 1168 \/ 696;\"><\/span><\/p>\n<p>&nbsp;In order to maximize the browser window, we can make use of the <em>setViewportSize(width,height)<\/em> method in playwright.&nbsp;<\/p>\n<p>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 <a href=\"https:\/\/whatismyviewport.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/whatismyviewport.com\/<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3325\" width=\"591\" data-init-width=\"1168\" height=\"257\" data-init-height=\"508\" title=\"Screenshot 2025-01-16 at 10.47.25\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.47.25\u202fAM.png\" data-width=\"591\" data-height=\"257\" style=\"aspect-ratio: auto 1168 \/ 508;\"><\/span><\/p>\n<p>&nbsp;We can now set these as width and height, see line#29<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3326\" width=\"503\" data-init-width=\"1168\" height=\"500\" data-init-height=\"1160\" title=\"Screenshot 2025-01-16 at 10.48.00\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.48.00\u202fAM.png\" data-width=\"503\" data-height=\"500\" style=\"aspect-ratio: auto 1168 \/ 1160;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>The maximized browser gets launched, see below, however we still see little bit gap on the left hand side&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3327\" width=\"524\" data-init-width=\"1168\" height=\"312\" data-init-height=\"696\" title=\"Screenshot 2025-01-16 at 10.48.40\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.48.40\u202fAM.png\" data-width=\"524\" data-height=\"312\" style=\"aspect-ratio: auto 1168 \/ 696;\"><\/span><\/p>\n<p>&nbsp;We will see how to overcome this shortcoming in next topic. &nbsp;<\/p>\n<p><strong>Maximize browser window (argument method)<\/strong><\/p>\n<p>In order to fully maximize the browser window, we can make use of the <em>start-maximized<\/em> argument.<\/p>\n<p>We have to create an array list of arguments and simply add <em>start-maximized<\/em> argument as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3328\" width=\"644\" data-init-width=\"1168\" height=\"249\" data-init-height=\"452\" title=\"Screenshot 2025-01-16 at 10.49.13\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.49.13\u202fAM.png\" data-width=\"644\" data-height=\"249\" style=\"aspect-ratio: auto 1168 \/ 452;\"><\/span><\/p>\n<p>&nbsp;We than have to set the view port size as \u2018null\u2019 as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3329\" width=\"526\" data-init-width=\"1168\" height=\"473\" data-init-height=\"1050\" title=\"Screenshot 2025-01-16 at 10.49.48\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.49.48\u202fAM.png\" data-width=\"526\" data-height=\"473\" style=\"aspect-ratio: auto 1168 \/ 1050;\"><\/span><\/p>\n<p>&nbsp;So our entire code looks like:<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3330\" width=\"501\" data-init-width=\"1168\" height=\"300\" data-init-height=\"700\" title=\"Screenshot 2025-01-16 at 10.50.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.50.26\u202fAM.png\" data-width=\"501\" data-height=\"300\" style=\"aspect-ratio: auto 1168 \/ 700;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>Notice below this time that the browser does get 100percent maximized<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3331\" width=\"646\" data-init-width=\"1168\" height=\"405\" data-init-height=\"732\" title=\"Screenshot 2025-01-16 at 10.50.55\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-10.50.55\u202fAM.png\" data-width=\"646\" data-height=\"405\" style=\"aspect-ratio: auto 1168 \/ 732;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>So this is how we can use the 2 browser maximizing techniques based on our needs.<\/p>\n<p><strong>Source code (Viewportsize)<\/strong><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> org.junit.jupiter.api.Test;<strong>import<\/strong> com.microsoft.playwright.Browser;<strong>import<\/strong> com.microsoft.playwright.BrowserContext;<strong>import<\/strong> com.microsoft.playwright.BrowserType;<strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<strong>import<\/strong> com.microsoft.playwright.Page;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>public<\/strong><strong>class<\/strong> Blog18_MaximizeWindowMethod1 {@Test<strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {Playwright playwright = Playwright.<em>create<\/em>();LaunchOptions launchOptions = <strong>new<\/strong> LaunchOptions();launchOptions.setChannel(&#8220;msedge&#8221;);launchOptions.setHeadless(<strong>false<\/strong>);BrowserType browserType = playwright.chromium();Browser browser = browserType.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));\/\/Page page = browser.newPage();\/\/Method 1 to maximize window:BrowserContext browserContext = browser.newContext(<strong>new<\/strong> Browser.NewContextOptions().setViewportSize(1366,768));Page page = browserContext.newPage();page.navigate(&#8220;https:\/\/rahulshettyacademy.com\/&#8221;);page.pause();}}<strong>Source code (argument)<\/strong><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> java.util.ArrayList;<strong>import<\/strong> org.junit.jupiter.api.Test;<strong>import<\/strong> com.microsoft.playwright.Browser;<strong>import<\/strong> com.microsoft.playwright.BrowserContext;<strong>import<\/strong> com.microsoft.playwright.BrowserType;<strong>import<\/strong> com.microsoft.playwright.Page;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>public<\/strong><strong>class<\/strong> Blog18_MaximizeWindowMethod2 {@Test<strong>public<\/strong><strong>void<\/strong> PlaywrightJTest() <strong>throws<\/strong> InterruptedException {Playwright playwright = Playwright.<em>create<\/em>();ArrayList&lt;String&gt; arguments = <strong>new<\/strong> ArrayList&lt;&gt;();arguments.add(&#8220;&#8211;start-maximized&#8221;);Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setChannel(&#8220;chrome&#8221;).setHeadless(<strong>false<\/strong>).setArgs(arguments));BrowserContext browserContext = browser.newContext(<strong>new<\/strong> Browser.NewContextOptions().setViewportSize(<strong>null<\/strong>));Page page = browserContext.newPage();page.navigate(&#8220;https:\/\/rahulshettyacademy.com\/learning-path&#8221;);page.pause();}}Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will utilize Playwright Java to maximize the browser window using 2 methods.&nbsp; Topics that we will cover: Maximize browser window (Viewportsize method) Maximize browser window (argument method)&nbsp; Source code (Viewportsize) Source code (argument) Maximize browser window (Viewportsize method) Let us run below piece of code &nbsp;Notice below that the browser is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3320"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3320\/revisions"}],"predecessor-version":[{"id":3721,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3320\/revisions\/3721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}