0

I got stuck in for loop as the automation code is entering a loop that has few actions to be done like this:

for (i = 0; i <= 2; i++) {
            // if(i==2)break;
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            WebDriverWait waitForInvstrDmo = new WebDriverWait(driver, 300);
            waitForInvstrDmo.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='#transferorDemographicsModal' and contains(@onclick,'transferorEditDemographics();')]")));

            driver.findElement(By.xpath("//a[@href='#transferorDemographicsModal' and contains(@onclick,'transferorEditDemographics();')]")).click();

            WebDriverWait waitForCitizn = new WebDriverWait(driver, 50);
            waitForCitizn.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorCitizenship']")));

            Select selectCitiZenship = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorCitizenship']")));
            selectCitiZenship.selectByValue("235");

            WebDriverWait waitForTaxResidency = new WebDriverWait(driver, 80);
            waitForTaxResidency.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorTaxResidency']")));

            Select selectTaxResidency = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorTaxResidency']")));
            selectTaxResidency.selectByValue(String.valueOf((int) (Math.random() * 50)));

            WebDriverWait waitForBirthPlace = new WebDriverWait(driver, 80);
            waitForBirthPlace.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorBirthPlace']")));

            Select selectBirthPlace = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorBirthPlace']")));
            selectBirthPlace.selectByValue(String.valueOf((int) (Math.random() * 50)));

            WebDriverWait waitForCountryOfIncor = new WebDriverWait(driver, 100);
            waitForCountryOfIncor.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferorIncorporationCountry']")));

            Select selectCountryOfIncor = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferorIncorporationCountry']")));
            selectCountryOfIncor.selectByValue(String.valueOf((int) (Math.random() * 50)));

            WebDriverWait waitForDomCountry = new WebDriverWait(driver, 300);
            waitForDomCountry.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='ddlTransferordomicileCountry']")));

            Select selectDomCountry = new Select(driver.findElement(By.xpath("//select[@id='ddlTransferordomicileCountry']")));
            selectDomCountry.selectByValue(String.valueOf((int) (Math.random() * 50)));

            WebDriverWait waitForSaveDemog = new WebDriverWait(driver, 300);
            waitForSaveDemog.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='modal-footer']/a[@href='#'and contains(@onclick,'SaveTransferorDemographics();')]")));

            // code for saving the Investor Demographic details
            driver.findElement(By.xpath("//div[@class='modal-footer']/a[@href='#'and contains(@onclick,'SaveTransferorDemographics();')]")).click();
        }

Now point is when I am executing this code for the first iteration its taking the select values from the drop down as mentioned in the code but its getting stuck in final iteration for 'selectCitiZenship' & 'selectTaxResidency' in above snippet within the loop. So in this case whole code is executing well for 1 and 2 iteration but it will failed in the last iteration throwing a TimeOut Exception - "Timed out after 50 seconds waiting for visibility of element located by By.xpath: //select[@id='ddlTransferorCitizenship']". I changed the wait timings too. Please provide me your valuable suggestions if you have.

P.S. Its working well for the first iteration but not for the rest iteration.

  • I am new to Selenium, so want to ask few basic questions here. 1. Isn't Thread.sleep() an explicit method of providing waiting time? In case the element is visible much before that, it'll still wait for that long. 2. Isn't hard-coding the element locators in test method a really bad idea? And in case you are storing it in another location, how do you use visibilityOfElementLocated method as this method takes only locator. – user12458 Jun 23 '15 at 15:48
3

Try changing the loop condition to i<3 and use Thread.sleep(); method of java to wait for the visibility of the element instead of the web driver's wait command.

Let me know if this works out.

In the meanwhile, will try to search a more robust solution for this.

  • 2
    Milin ,it worked as of now i just put Thread.sleep(2000); before each elements and it worked now :) But for loop like 160 times i need to iterate the another whole segment so in that case will this hard wait work ? or if you other remedy please let me know ! – Ranjan Gupta Oct 31 '14 at 7:41
  • 2
    It should work for any number of loop count. The only thing is, the larger the loop count, more the time taken for completing the test. If i do find a more robust solution i will make sure to post it here! Until then will have to make do with this work around :) – IAmMilinPatel Oct 31 '14 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.