4

I am pretty new to all the Automated Testing thing and recently I have been having trouble with selenium because sometimes fails finding certain input fields / buttons.

I added explicit waits for every element in the page, so the test is supposed to wait for that element to appear on screen, but sometimes randomly fails to do so. I will attach the latest error thrown by selenium in a simple test.

This is one of the tests that I am trying to run and randomly fails:

@Test
public void createTaskTest(){
    generateTestEnvironmentSalesForceHomePage();
    this.home.clickTasksTab();
    generateTestEnvironmentTaskPage();
    this.taskspage.clickNewTask();
    generateTestEnvironmentNewTask();
    this.newtask.setDateField("12/30/2016");
    this.newtask.setCostField("1500");
    this.newtask.setType("Personal Appointment");
    this.newtask.setPaymentType("End of the day");
    this.newtask.setDoctor("House");
    this.newtask.submitTask();
    Assert.assertTrue(this.newtask.checkDetailsPage());
}

Error thrown by selenium:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: span.dateOnlyInput>input (tried for 130 second(s) with 500 MILLISECONDS interval)

Here is the method to find the 'missing' date input:

public void setDateField(String date){
    WebDriverWait wait = new WebDriverWait(this.driver,130);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.dateOnlyInput>input")));
    this.driver.findElement(By.cssSelector("span.dateOnlyInput>input")).sendKeys(date);
}

3 Answers 3

2

Best option can be write your code inside a try catch block. Then if there are any error (which exception contains time out) , then simply run the same method inside the try catch block with a wait or sleep command which will increase the waiting time for the element..

If the time out error is a never stopping error you will be ended up with a never ending loop. But if you use a for loop with a count you can avoid being inside a never ending loop.

2
  • I do suggest to use - wait.until instead of try & catch before action. Dont you think it would be very difficult to maintain? Commented Dec 21, 2016 at 13:03
  • I already used wait.until and gave it enough time to search for the element, but it seems that is not enough. The try catch thing solved my problem for now Commented Dec 21, 2016 at 14:11
0

If it fails randomly, then there might be a lot of things going on there.

Catch the exception and dump the source of the page at the moment of the exception. Then analyze it and check whether you can find the input element. If it's there, make sure your CSS path is correct (maybe the HTML tree is not always the same?).

If it's not there, maybe you need to increase the timeout because the page is so slow.

3
  • The html is always the same so that cant be it. Another thing to notice is that all the tests work well isolated, but when running the 4 tests of the class random TimeoutExceptions appear sometimes. Maybe i need to do something in between tests? Thanks for the help Commented Dec 20, 2016 at 17:01
  • Which browser are you running the tests on? I've experienced random issues like that with the IE driver in the past.
    – Radek
    Commented Dec 20, 2016 at 19:48
  • Im using Firefox in this test Commented Dec 20, 2016 at 20:22
0

Few fails are really random, Observed same. Below few solutions might help:

  1. Change in wait function: Max. its working well for me

    wait.until(ExpectedConditions.visibilityOf("Your_Element's_locator));
    
  2. Try with different locators:

    • ID

    • Name

    • CSS

    • LinkText

    • Then combinations of above like ID & CSS, ID & name etc

2
  • How do you try combinations of id css and name? thank you for the help Commented Dec 21, 2016 at 11:27
  • You should use visibilityOfElementLocated not visibilityOf as the last method does not check if the element is available in the DOM. Commented May 26, 2020 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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