Commands for Wait Statements in Selenium WebDriver

Posted By : Neha Saini | 31-Oct-2017

Wait statements commands plays an important role in tests execution in Selenium WebDriver. To wait for a certain amount of time before executing next step in automation, we use the wait commands.

Different types of Wait Statements:-

1. Implicit Wait

2. Explicit Wait

3.Fluent Wait

4.PageLoadTimeOut Wait

5.Thred.sleep()

1. Implicit Wait -

Implicit Wait is used to tell Web Driver to wait for a certain amount of time when trying to find element on the page. Implicit Wait can be applied to all the web elements. In implicit wait the web driver polls the DOM to check the availability of the web Element and waits till the maximum time specified before throwing an  exception that it cannot find the element on the page.

Syntax -

  1. WebDriver driver = new FirefoxDriver();
  2. driver.manage().timeouts().implicitlyWait(25,TimeUnit.SECONDS);
In this code, the value 25 specified in implicit wait method is the maximum time in seconds till which WebDriver will wait before throwing “NoSuchElementException” while locating a WebElement on WebPage. 

2.Explicit Waits -
 
Explicit waits can also be applied to each and every WebElement. In explicit wait, WebDriver instance waits for defined conditions before locating WebElements on web page or performing any actions on them.

Syntax -
  1. WebDriverWait wait = new WebDriverWait(driver, 25);
  2. wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));
In this code, WedDriver instance will wait until the specified condition is met i.e. the presence Of Element located by the ElementLocator with the maximum wait time of 25 seconds after which if the condition is still not met than it will throw exception. 

3.Fluent Wait -

Fluent wait statement is used to tell the WebDriver instance to wait for a specified condition, as well as the frequency(repeat cycle with the time frame to check the condition at the regular interval of time.) with which to check the condition before throwing an "ElementNotVisibleException" exception.

Syntax -

  1. Wait wait = new FluentWait(driver);

  2. .withTimeout(30,SECONDS);.

  3. .poolingEvery(5,SECONDS);

  4. .ignoring(NOSUCHELEMENTEXCPTION.class)

 

4.PageLoadTImeOut Wait -

PageLoadTimeOut Wait Statement, is used to sets the amount of time to wait for a page load, before throwing an error.

Syntax -

  1. WebDriver driver = new FirefoxDriver();
  2. driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

In this code, WebDriver instance waits for 20 seconds for every page to get loaded before throwing an exception. If the page is not loaded in 20 seconds, then it throws an “TimedOutException” exception at run time.

5.Thread.sleep() -

It always force the browser to wait for a specific time.Thread.Sleep() is not a good idea and that’s why Selenium provides WaitStatements.

Syntax -


Thread.sleep(1000);

Related Tags

About Author

Author Image
Neha Saini

Neha is a QA Engineer in Oodles and have good knowledge of Manual Testing and Automation Testing , always eager to learn new things.

Request for Proposal

Name is required

Comment is required

Sending message..