How to close the whole browser window by keeping the webDriver active?

前端 未结 3 503
自闭症患者
自闭症患者 2020-12-03 03:42

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.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 04:01

    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();
        }
    }
    

提交回复
热议问题