Sunday, April 16, 2023

Multi Tab Support in Selenium Web Driver

Selenium Web Driver is capable of handling multiple browser tabs. Though the requirement to automate workflows that span multiple tabs is one that automation engineers don’t come across all the time, it’s always good to know how it can be done. 


package com.dumiduh.other;
import com.dumiduh.constants.Constants;
import com.dumiduh.utils.TestBase;
import org.openqa.selenium.By;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.List;
import java.util.stream.Collectors;
public class MultiTabTest extends TestBase {
@BeforeClass
public static void setup() {
instantiateDriver();
driver.get(Constants.MULTI_WINDOW);
}
@Test
public static void multiWindowTest() {
System.out.println("URL:\t" + driver.getCurrentUrl());
//opening a new tab
driver.findElement(By.xpath("//*[text()='Click Here']"))
.click();
//A new tab has been opened.
System.out.println("Number of tabs\t" + driver.getWindowHandles()
.size());
List<String> windows = driver.getWindowHandles()
.stream()
.map(window -> {
return window;
})
.collect(Collectors.toList());
driver.switchTo()
.window(windows.get(windows.size() - 1));
driver.get("https://www.yahoo.com");
System.out.println("URL:\t" + driver.getCurrentUrl());
}
@AfterClass
public static void cleanUp() {
driver.quit();
}
}



No comments:

Post a Comment

What's in my Bag? EDC of a Tester