Intellipaat Back

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

I'm writing some automated tests for using the selenium chrome driver. I trying to write a reusable method that will explicitly wait for elements to appear and then call this method in other classes. Seems pretty straight forward but it is not doing what I want it to do. Here is the method that I have.

public String waitForElement(String item) {

    WebDriverWait wait = new WebDriverWait(driver,30);

    WebElement element = wait.until(

                        ExpectedConditions.elementToBeClickable(By.id(item)));

    return item;

}

Then I call the method and pass it a parameter like this:

waitForElement("new-message-button");

That doesn't seem to become working, can someone give some insight?

1 Answer

0 votes
by (62.9k points)

You can use Explicit wait or Fluent Wait

Example of Explicit Wait -

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); 

WebElement aboutMe:

aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));

Example of Fluent Wait -

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(20, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);

WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {

public WebElement apply(WebDriver driver) {

return driver.findElement(By.id("about_me")); 

}

});

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...