0

My issue is somewhat similar to this question.

I am using cucumber framework for automated testing of an enterprise java website. I have written many *.feature files for testing various functionalities. Recently when I ran regression tests on Jenkins, some of the tests started breaking.The test was as follows.

1) Go to Temp mail inbox in chrome.

2) Open the latest email.

3) Click on the 'confirm registration link', which will open a new tab.

4) Switch to the newly opened tab and continue with login.

The test is failing at the fifth step with 'Element not found' exception. When I checked the screenshots from selenium, it shows that the test is still in the temp mail inbox. Tab switch had not taken place.

I have narrowed the issue to the following step definition.

    @And("^I switch to next window$")
    public void iSwitchToNextWindow() throws Throwable {
     for (String winHandle : getWebDriver().getWindowHandles()) {
       if (!winHandle.equalsIgnoreCase(windowHandle)) {
            getWebDriver().switchTo().window(winHandle); 
         }
       }
     }

windowHandle is obtained from the previous step.

Can someone please help me with this.Please comment if you need any more information.Thank you.

Note: Test is failing only when it is ran on Jenkins, it works fine when ran on local machine.

EDIT:

[ISSUE RESOLVED] The problem was with the chromedriver.exe version. Jenkins was using an older version of chromedriver than on my machine.

wardaddy
  • 383
  • 1
  • 4
  • 16

1 Answers1

0

The main issue here is you are trying to switch too early and your switching logic is missing to verify if the windowHandle is not the parent windowHandle . You can use the following code block/concept/algorithm :

  • Before @And("^I switch to next window$") :

    String first_win_handle = driver.getWindowHandle();
    //click on the element which opens the new TAB
    
  • Switching TABs :

        @And("^I switch to next window$")
        public void iSwitchToNextWindow() throws Throwable 
        {
            new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
            for (String winHandle : getWebDriver().getWindowHandles()) 
            {
                if (!first_win_handle.equalsIgnoreCase(winHandle)) 
                {
                    getWebDriver().switchTo().window(winHandle); 
                }
            }
        }
    

Here you can find a detailed discussion on Best way to keep track of Windows with Selenium in IE11?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @DebajanB thanks for the answer. Could you please help me understand the difference in switching logic? Also, my code works fine on PC but breaks on jenkins. why would that happen? – wardaddy Feb 12 '18 at 09:02
  • @aswinprabhakar Does my code still breaks on Jenkins? – undetected Selenium Feb 12 '18 at 09:05
  • Sorry for the delayed reply @DebanjanB. I have run the test on Jenkins and it fails with the following error - "org.openqa.selenium.TimeoutException: Expected condition failed: waiting for number of open windows to be 2 (tried for 5 second(s) with 500 MILLISECONDS interval)" – wardaddy Feb 13 '18 at 03:35