Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm working on a Java Selenium-WebDriver. I added

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

and

 WebElement textbox = driver.findElement(By.id("textbox"));

 because my Applications takes a few seconds to load the User Interface. So I set 2 seconds implicit wait. but I got unable to locate element textbox

Then I add Thread.sleep(2000);

Now it works fine. Which one is a better way?

1 Answer

0 votes
by (62.9k points)
edited by

Basically, there are two types of waits and they are as follows:  explicit wait and implicit wait. The code for the explicit wait is as follows:

WebDriverWait.until(condition-that-finds-the-element);

 The code for the implicit wait is as follows:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

You can analyze the difference in detail here.

public WebElement fluentWait(final By locator) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);
    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });
    return  foo;
}

 Usage of fluentWait in your case will be something like the following:

WebElement textbox = fluentWait(By.id("textbox"));

Hope this helps!
If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s online automation testing training!
 

Browse Categories

...