0 votes
1 view
in Devops and Agile by (25k 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 (63.3k 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!

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...