In my batch execution, multiple browsers with multiple tabs are getting opened for first scenario. I wanted to close all these browsers before starting second scenario.
This code closes all the opened windows and then brings back control to the main window.
public static void switchTab() {
try {
Set windows = webDriver.getWindowHandles();
Iterator iter = windows.iterator();
String[] winNames=new String[windows.size()];
int i=0;
while (iter.hasNext()) {
winNames[i]=iter.next();
i++;
}
if(winNames.length > 1) {
for(i = winNames.length; i > 1; i--) {
webDriver.switchTo().window(winNames[i - 1]);
webDriver.close();
}
}
webDriver.switchTo().window(winNames[0]);
}
catch(Exception e){
e.printStackTrace();
}
}