In our previous post, we have seen the features / enhancements to selenium 4, among them one of the feature is handling windows like switching to a new browser tab / window out of box with selenium 4 & minimizing the window.
we will discuss this feature in detail
Minimizing browser window
If we look into selenium 4 api doc, there is a minimize() available under interface WebDriver.Window
we can minimize the browser window using below code line and continue performing the browser actions and also can take screenshots
driver.manage().window().minimize();
Here is one sample code implementation
package sel4;import io.github.bonigarcia.wdm.WebDriverManager;import org.apache.commons.io.FileUtils;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;import java.io.File;import java.io.IOException;import java.time.Duration;public class ScreenshotSamples {private static WebDriver driver;private static WebDriverWait wait;@BeforeMethodvoid Login() throws InterruptedException {//WebDriverManager.chromedriver().setup();//driver = new ChromeDriver();WebDriverManager.firefoxdriver().setup();driver = new FirefoxDriver();driver.get("https://www.saucedemo.com/");Thread.sleep(2000);wait = new WebDriverWait(driver, Duration.ofSeconds(5));}@Testpublic void ElScreenshot() throws InterruptedException {driver.manage().window().minimize();driver.findElement(By.id("user-name")).sendKeys("qavbox");driver.findElement(By.id("password")).sendKeys("qavbox1");driver.findElement(By.id("login-button")).click();try {wait.until(ExpectedConditions.presenceOfElementLocated(By.className("product_label")));} catch (Exception e) {System.out.println("Login Error" + e.getMessage());WebElement el = driver.findElement(By.cssSelector("[data-test='error']"));captureScreenShotOf(el, driver);}Thread.sleep(2000);driver.quit();}void captureScreenShotOf(WebElement el, WebDriver driver){File newImg = el.getScreenshotAs(OutputType.FILE);try{FileUtils.copyFile(newImg, new File("./screenshot/ElPageShot.jpg"));}catch(Exception e){e.printStackTrace();}}}
As you can see, we are minimising the browser window and performing login and then took screenshot.
Creating & switching to new Window / Tab
Before to selenium 4, we used to use robot class or some other library to create a new tab or window and then switch to that new window to perform browser actions
But with Selenium 4, with one line of code, we can create a new tab or window and switch to that window and perform any operation like loading a url
driver.switchTo().newWindow(WindowType.WINDOW).get("https://www.google.com/");//ordriver.switchTo().newWindow(WindowType.TAB).get("https://www.google.com/");
Let’s see one implementation
After we create a new window or tab, we can perform some browser actions and then use the getWindowHandles() to get the window strings and navigate to any of the window [we know all how to do :)]
package sel4;import io.github.bonigarcia.wdm.WebDriverManager;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WindowType;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.annotations.BeforeMethod;import org.testng.annotations.Test;import java.time.Duration;public class WindowTypeSamples {private static WebDriver driver;private static WebDriverWait wait;@BeforeMethodvoid Login() throws InterruptedException {//WebDriverManager.chromedriver().setup();//driver = new ChromeDriver();WebDriverManager.firefoxdriver().setup();driver = new FirefoxDriver();driver.get("https://www.saucedemo.com/");Thread.sleep(2000);wait = new WebDriverWait(driver, Duration.ofSeconds(5));}@Testpublic void Test_newWindowType() throws InterruptedException {System.out.println("Before navigating to new window" + driver.getCurrentUrl());String parentWindow = driver.getWindowHandle();Thread.sleep(2000);//driver.switchTo().newWindow(WindowType.WINDOW).get("https://www.google.com/");driver.switchTo().newWindow(WindowType.TAB).get("https://www.google.com/");System.out.println("After navigating to new window" + driver.getCurrentUrl());Thread.sleep(2000);//perform google operationsdriver.close();driver.switchTo().window(parentWindow);System.out.println("Parent windows" + driver.getCurrentUrl());Thread.sleep(2000);driver.close();}}
WindowType
is a enum having 2 constants TAB
& WINDOW
TAB will create a new tab on the same browser window currently driver is holding
WINDOW will create a new window out of current browser window.
1 Comment