0

I want to apply a one specification to my selenium test's code as webdriver have to wait for web element to display or load on a webpage for anytime. It should be applicable for a every web element in the code. Is there any way to get this. Instead of applying implict wait or Webdriver wait when ever it required. I can use this specification so that even though in future any webElement take sometimes to get visible it will wait default till it is visible.

slckayhn
  • 1,168
  • 1
  • 14
  • 21
Satish Rongala
  • 157
  • 3
  • 17
  • @DebanjanB That dup is not what OP is asking. He's asking how to apply a wait to ALL `.findElement()` calls without using implicit waits or `WebDriverWait` everywhere. Your dup is how to apply a wait to ONE call. See Guy's answer for an example of what OP is looking for. – JeffC Oct 25 '18 at 13:13

1 Answers1

4

You can create a method which receives By as parameter and returns WebElement, and use it for all the element search instead of driver.findElement()

// Java syntax
public WebElement findElement(By by) {
    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    return element;
}

You can also put WebDriverWait wait = new WebDriverWait(driver, 30); at class level instead of creating a new instance every time.

Guy
  • 40,130
  • 10
  • 33
  • 74