Hiya Champs! Things don’t always work the way we want them to and that’s where the challenges take their form. With Selenium Webdriver, our testing lives are made far easier than we would expect them to be. One such situation is maximizing the browser window.
Screenshots are a life saver and in order to view all the web elements while grabbing one, it is important to maximize the browser window. Therefore, instead of scrolling down to a particular element, just maximize the window and complete the task at hand.
// Maximize the new window
driver.manage().window().maximize();
Can you believe it? This one line is all that you were looking for! Finding it difficult to digest? No worries. Let us see an example to see this piece of code in action.
Scenario
- Open Firefox browser
- Navigate to the demo site
- Get the current window handle
- Locate ‘Click to open a small window!’ button using Id
- Click the button to open the small window
- Get the window handles of both the open windows
- Loop through both handles
- Switch to the new window with the handle’s reference
- Get the title and print it to console
- Maximize the small window to full-screen size
- Close the new window
- Switch the control back to parent window and print the URL to the console
- Verify Eclipse IDE console output screen and JUnit pane for success result
JUnit code for this scenario is,
package com.blog.junitTests;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WindowMaximize {
// Declaring variables
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
// Selenium version 3 beta releases require system property set up
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
+ "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
// Create a new instance for the class FirefoxDriver
// that implements WebDriver interface
driver = new FirefoxDriver();
// Implicit wait for 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Assign the URL to be invoked to a String variable
baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/";
}
@Test
public void testPageTitle() throws Exception {
// Open baseUrl in Firefox browser window
driver.get(baseUrl);
// Get current window handle
String parentWinHandle = driver.getWindowHandle();
// Locate 'Click to open a small window!' button using id
WebElement newWindowBtn = driver.findElement(By.id("win2"));
// Click the button to open a new window
newWindowBtn.click();
// Get the window handles of all open windows
Set<String> winHandles = driver.getWindowHandles();
// Loop through all handles
for (String handle : winHandles) {
if (!handle.equals(parentWinHandle)) {
driver.switchTo().window(handle);
System.out.println("Title of the new window: " + driver.getTitle());
// Maximize the new window
driver.manage().window().maximize();
driver.close();
}
}
// Switching the control back to parent window
driver.switchTo().window(parentWinHandle);
// Print the URL to the console
System.out.println("Parent window URL: " + driver.getCurrentUrl());
} // End of @Test
Execution Result:
Clear comments make the code self-explanatory.
In Eclipse IDE -> JUnit pane -> green bar shows that the test case is executed successfully. Console window shows the absence of any errors. It also shows all the printed messages as expected.
Don’t forget to shout in the comments section in case of any clarifications!
I will see you soon in another post. Have a great day!