2

My apologies in advance if my question sounds primary, I am very new at QA and Selenium.

I am using Java and Selenium to write a test, some times I need to wait for a web element to be accessible, below is my snippet code that I used to use:

        int counter = 0;
        while (true) {
            counter++;
             boolean breakIt = false;
            try {
                WebElement element= driverChrome.findElement(By.xpath("bla bla"));
                element.click();
                breakIt = true;
            } catch (Exception e) {             
                Thread.sleep(1000);
            }
            if (breakIt) {
               break;
            }
            if (counter > 4) {
               System.out.println("Failed");
               tearDown();
               System.exit(1);
             }
        }

But now somewhere I saw this:

WebDriverWait wait = new WebDriverWait(driverChrome, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("bla bla"))).click();

for sure the second one is much shorter but I do not know if it is better or not, in other words, are they different? and if yes, how? and which one is better for which purposes?

LoveLovelyJava
  • 735
  • 4
  • 9
  • 21
  • Using a WebDriverWait is always ideal over a sleep statement. Once the element is loaded then it becomes intractable immediately, rather than waiting a set amount of time. Especially if you have many tests that utilize the same step, it saves a lot of time in the long run. – AutomatedOrder Oct 20 '15 at 15:48
  • @AutomatedOrder Thanks for your reply, could you please post it as the answer? and if you know, could you tell me where can I learn more about ExpectedConditions. methods? – LoveLovelyJava Oct 20 '15 at 15:50
  • Possible duplicate of [Selenium wait until element is present](http://stackoverflow.com/questions/20903231/selenium-wait-until-element-is-present) – JeffC Oct 20 '15 at 16:19

2 Answers2

6

The answer you accepted is incorrect.

What WebDriverWait does is roughly speaking the same thing as what your code snippet does: it polls the DOM periodically to check whether the condition you want it to check for is true. If it is not true then it uses Thread.sleep for you. (You can ascertain what I'm saying here by reading the implementation of FluentWait, which is the class on which WebDriverWait is based, and of Sleeper because FluentWait uses Sleeper.SYSTEM_SLEEPER to go to sleep between polls.)

Contrarily to what the accepted answer asserts ("Once the element is loaded then it becomes intractable immediately"), when the condition you were waiting for becomes true, WebDriverWait won't immediately allow you to interact with the element. If the WebDriverWait instance is sleeping while the condition becomes true, it will continue sleeping until its sleeping interval is over (0.5 second by default) and then it will check again the condition and return immediately if the condition is found to be true. Otherwise, it will go back to sleep unless the timeout you gave to it has expired.

The difference between your home-made wait look and WebDriverWait is that WebDriverWait is reusable, flexible, and designed to handle a bunch of conditions that your code does not handle. Or to put it differently, it is more robust. In particular, it will let exceptions that it does not know what to do with trickle up instead of hiding them like your code does. There is no condition I can imagine under which your code would be preferable.

Community
  • 1
  • 1
Louis
  • 146,715
  • 28
  • 274
  • 320
  • Thanks a lot for your help, where can I download javadoc jar for org.openqa.selenium.support.ui that I can add it to my project and when I hover over a method it shows some information about it? – LoveLovelyJava Oct 20 '15 at 17:37
  • @LoveLovelyJava I can't help with linking javadoc to your project, I'm afraid. I can read Java but it's been a good decade since I've had to write code in it in any substantial capacity. – Louis Oct 20 '15 at 17:39
2

Using a WebDriverWait is always ideal over a sleep statement. Once the element is loaded then it becomes intractable immediately, rather than waiting a set amount of time. Especially if you have many tests that utilize the same step, it saves a lot of time in the long run.

As for ExpectedConditions, I would recommend this page,.

AutomatedOrder
  • 501
  • 4
  • 14
  • where can I download javadoc **jar** for org.openqa.selenium.support.ui that I can add it to my project and when I hover over a method it shows some information about it? – LoveLovelyJava Oct 20 '15 at 15:57
  • I use selenium in c#, so I'm not 100% positive but this link should be what you're looking for, you'll just need to get your version: http://www.java2s.com/Code/Jar/s/selenium-support.htm – AutomatedOrder Oct 20 '15 at 16:00