0

I want my script to switch windows. It should switch to second window and again switch back to the first one so that i can continue operations in the current window. I cannot find why the first window is already closed.

   public void doDetails(){
        logger.info("########## Started ##########");

        // Store the current window handle
        String winHandleBefore = driver.getWindowHandle();
        getAllComponents();
        mouseOnArrow();
        WaitUtils.waitUntilVisiblityOfElement(driver,link,WaitUtils.FIVE_SECONDS);

        //Click operation opens another window/tab
        link.click();
        WaitUtils.waitForSeconds(WaitUtils.FIVE_SECONDS);

        //Switching to new window
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }
        addFilter();
        WaitUtils.waitForSeconds(WaitUtils.TEN_SECONDS);
        components.clickCustomize();
        WaitUtils.waitUntilVisiblityOfElement(driver,components.allCheckboxes,WaitUtils.TWENTY_SECONDS);
        components.clickAllChkBoxes();
        WaitUtils.waitForSeconds(WaitUtils.ONE_SECOND);
        clickAllDetail();
        WaitUtils.waitForSeconds(WaitUtils.ONE_SECOND);
        components.clickApply();
        WaitUtils.waitUntilInvisibilityOf(driver,components.customizeHeader,WaitUtils.TWENTY_SECONDS);
        sortByAllowedAmount();
        WaitUtils.waitUntilInvisibilityOf(driver,components.loading,WaitUtils.TWENTY_SECONDS);
        clickExcel();
        WaitUtils.waitForSeconds(WaitUtils.TEN_SECONDS);

        //Closing current window
        driver.close();

        //Switching back to first window
        driver.switchTo().window(winHandleBefore);

    }
Success Shrestha
  • 433
  • 1
  • 4
  • 19
  • 1
    _...should switch to second window and again switch back to the first one..._ Does the lines of code executes on the second window anyway? – undetected Selenium Jan 16 '19 at 09:04
  • Check this discussion https://stackoverflow.com/questions/51775122/nosuchwindowexception-no-such-window-window-was-already-closed-while-switchi/51788519#51788519 – undetected Selenium Jan 16 '19 at 09:26

1 Answers1

1

In your code you have to add a check while switching to new window. In your code you are at your current window.

   //Switching to new window
    for(String winHandle : driver.getWindowHandles()){
      if(!(winHandle.equals(winHandleBefore)){
        driver.switchTo().window(winHandle);
       }
    }

This should work for you.

mauryaAjay
  • 249
  • 2
  • 9