How to close all tabs at once using selenium Java?

Viewed 379

I am trying to automate certain scenarios in Salesforce using selenium but my tests are unstable as sometimes the click happens on the element present on the subtab. Hence i want to close all subtabs before starting my tests. The shortcut to close all tabs at once is Shift + w and Enter. I tried following code but its not working everytime.

try {
            while(close.isDisplayed())
                
            {
                LOGGER.info("Sub Tabs Open");
                
                try {
                    LOGGER.info("Method1");
                    String selectAll = Keys.chord(Keys.SHIFT,"w");
                    driver.findElement(By.xpath("//body[@class='desktop']")).sendKeys(selectAll);
                    pause(4000);
                    pg.jsClick(driver, closeAllButton);
                    LOGGER.info("Closed tabs1");
                }
                catch(Exception e1)
                {
                    LOGGER.info("Method2");
                    Robot r = new Robot();
                    r.keyPress(KeyEvent.VK_SHIFT);
                    r.keyPress(KeyEvent.VK_W);

                    r.keyRelease(KeyEvent.VK_SHIFT);
                    r.keyRelease(KeyEvent.VK_W);
                    r.keyPress(KeyEvent.VK_ENTER);
                    r.keyRelease(KeyEvent.VK_ENTER);
                    LOGGER.info("Closed tabs2");
                }
                finally
                {
                    LOGGER.info("Method3");
                     // Press SHIFT-W- Enter  
                    Actions a = new Actions(driver);
                       a.keyDown(Keys.SHIFT)
                        .sendKeys("w")
                        .build()
                        .perform();       
                       Thread.sleep(5000);

                       // Release SHIFT+CTRL keys   
                       a.keyUp(Keys.SHIFT)
                        .build()
                        .perform();
                       
                       a.keyDown(Keys.ENTER)
                        .build()
                        .perform();
                       Thread.sleep(5000);
                       
                       a.keyUp(Keys.ENTER)
                        .build()
                        .perform();


                    LOGGER.info("Closed tabs3");
                }
                
            }
        }
        catch (Exception e) {
            LOGGER.info("No tab Open.");
        } 
2 Answers

I want to share a solution that helped in my case. Before sending keys to close all tabs, call the method that verifies that there is no spinner on a page (a webelement that is displayed when the page is loading).

@FindBy(xpath = "//div[contains(@class,'spinner__dot-a') or contains(@class,'spinner__dot-b')]")
private List<WebElement> imgSpinner;

public static boolean waitUntilSpinnerIsNotDisplayed(WebDriver driver, List<WebElement> elements) {
    FluentWait<WebDriver> fluentWait = new FluentWait<>(driver);
    return fluentWait
            .withTimeout(Duration.ofSeconds(10))
            .pollingEvery(Duration.ofMillis(500))
            .ignoring(StaleElementReferenceException.class)
            .until(webdriver -> {
                boolean spinnerIsNotDisplayed = true;
                for (WebElement element : elements) {
                    if (element.isDisplayed()) {
                        spinnerIsNotDisplayed = false;
                    }
                }
                return spinnerIsNotDisplayed;
            });
}

As i understand you want to close all of your open tabs before you start your test. You can use below line of code to kill all processes you want before you initiate your driver.

Runtime.getRuntime().exec("taskkill /F /IM Chrome.exe");
        

Here you can use firefox.exe or Chrome.exe or geckodriver.exe or chromedriver.exe or any other process.

Related