Windows
Sometimes there might be a situations where you need to test the application by changing the windows size or you need to see what is the current size of the window where the page is rendered. Selenium provides certain methods to deal with such situations. Lets see some of the common methods used.
Minimize Window
The method driver.manage().window().minimize() minimizes the current window if it is not already minimized.
Maximize Window
The method driver.manage().window().maximize() maximizes the current window if it is not already maximized
Fullscreen
The method driver.manage().window().fullscreen() fullscreen the current window if it is not already fullscreen
Get Window Size
The method driver.manage().window().getSize() get the size of the current window. This will return the outer window dimension.
Set Window Size
The method driver.manage().window().setSize() set the size of the current window. This will change the outer window dimension.
Following example covers all the above method implementation.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/browser/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); driver.manage().window().minimize(); driver.manage().window().fullscreen(); Dimension winSize = driver.manage().window().getSize(); System.out.println("Width : " + winSize.width + ", Height : " + winSize.height); driver.manage().window().setSize(new Dimension(400,600)); driver.manage().window().maximize(); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/browser/"; driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); driver.Manage().Window.Maximize(); driver.Manage().Window.Minimize(); driver.Manage().Window.FullScreen(); Size winSize = driver.Manage().Window.Size; Console.WriteLine("Width : " + winSize.Width + ", Height : " + winSize.Height); driver.Manage().Window.Size = new Size(400, 600); driver.Manage().Window.Maximize(); driver.Quit();
Handling Multiple Windows
Handling windows is a crucial aspect of Selenium automation, especially when dealing with web applications that involve multiple browser windows or tabs. Selenium provides methods in the WebDriver interface to handle window management scenarios.
Get Window Handle
To get the handle of the current window, selenium uses getWindowHandle() method of WebDriver interface
Get All Window Handles
To get handles of all currently open windows, selenium uses getWindowHandles() method of WebDriver interface. This will return a set of strings.
Switch to a Window
To switch to a specific window using its handle, driver.switchTo().window(windowHandle) is used. It will switch the focus of future commands for this driver to the window with the given handle. If the handle or window does not exist, it will throw NoSuchWindowException.
Get Current URL
The method getCurrentUrl() is used to get the current URL that the browser is looking at.
Get Current Browser Title
The method getTitle() of WebDriver will return the title of the current page, with leading and trailing whitespace stripped, or null if one is not already set.
Close Current Window
The method close() is used to close the current window, quitting the browser if it's the last window currently open.
Let's see the implementation of these methods in the following example.
WebDriver driver = new ChromeDriver(); driver.get("https://www.testautomationstudio.com/demo/browser/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); //Getting first window handle String firstWindowHandle = driver.getWindowHandle(); //Click on link to open second window driver.findElement(By.id("page2Link")).click(); //Getting all opened window handles Set<String> allOpenWindowHandles = driver.getWindowHandles(); //Looping through all window handles to switch to new window handle for (String windowHandle : allOpenWindowHandles) { if(!windowHandle.equals(firstWindowHandle)) { driver.switchTo().window(windowHandle); break; } } //Getting title of new window String page2Title = driver.getTitle(); System.out.println(page2Title); //Getting new window url String page2Url = driver.getCurrentUrl(); System.out.println(page2Url); //Closing the new window driver.close(); //switching back to first window driver.switchTo().window(firstWindowHandle); driver.quit();
IWebDriver driver = new ChromeDriver(); driver.Url = "https://www.testautomationstudio.com/demo/browser/"; driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); driver.Manage().Window.Maximize(); //Getting first window handle String firstWindowHandle = driver.CurrentWindowHandle; //Click on link to open second window driver.FindElement(By.Id("page2Link")).Click(); //Getting all opened window handles var allOpenWindowHandles = driver.WindowHandles; //Looping through all window handles to switch to new window handle foreach (String windowHandle in allOpenWindowHandles) { if (!windowHandle.Equals(firstWindowHandle)) { driver.SwitchTo().Window(windowHandle); break; } } //Getting title of new window String page2Title = driver.Title; Console.WriteLine(page2Title); //Getting new window url String page2Url = driver.Url; Console.WriteLine(page2Url); //Closing the new window driver.Close(); //switching back to first window driver.SwitchTo().Window(firstWindowHandle); driver.Quit();