Browser Management with Selenium

Get started with the basics of browser management using Selenium.

Open browser in certain size

Many modern websites use the responsive web design, meaning the page content layout changes according to the browser window size. This increases the testing effort of testers who now have to test websites in different browser window sizes. Fortunately, Selenium has a convenient way of resizing the browser windows as:

driver.manage().window().setSize(1024, 768);

Maximize browser window

We can maximize the browser windows as:

driver.manage().window().maximize();
driver.sleep(300);  // add delay to see the effect
driver.manage().window().setSize(1024, 768);

Move browser window

We can move the browser window (started by the test script) to a certain position on screen; (0, 0) is the top left of the screen. The position of the browser’s window won’t affect the test results. This might be useful for utility applications. For example, a background video program can capture a certain area on the screen.

driver.manage().window().setPosition(100, 200);
driver.sleep(300);
driver.manage().window().setPosition(0, 0);

Minimize browser window

Surprisingly, there isn’t a minimize window function in Selenium, and the workaround is as follows:

driver.manage().window().setPosition(-2000, 0);  // hide
driver.findElement(By.linkText("Hyperlink")).click(); // still can use
driver.sleep(300);
driver.manage().window().setPosition(0, 0);

Get hands-on with 1200+ tech skills courses.