Wait in Selenium

Let’s see the concept of wait in Selenium. In these days, many web applications are using AJAX. When a page is loaded into the browser, the elements within that page may load at different time intervals.

This makes locating elements difficult, if the element is not present in the DOM, it will raise ElementNotVisibleExceptionexception.

 

Using waits, we can solve this issue. Waiting provides some time interval between actions performed – mostly locating element or any other operation with the element.

Selenium Webdriver provides three types of waits.

  • ImplicitWait
  • ExplicitWait
  • FluentWait

 

Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

 

Implicit Wait in Selenium

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is zero. Implicit waits are used to provide a default waiting time between each consecutive test step/command across the entire test script.

Syntax –

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

 

Explicit Wait in Selenium

Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied for a particular instance only.

The worst case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

 

WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.

In above code, the WebDriver wait until this ‘Someid’ element gets to appear and wait max time up to 10 sec as declared in the WebDriverWait class.

Here we need to notice two things, First is WebDriverWait class and Second is ExpectedConditions. Now question is, What could be the ExpectedConditions.

 

Types of ExpectedConditions in Explicit wait

ExpectedConditions class provides a great help to deal with scenarios where we have to ascertain for a condition to occur before executing the actual test step.

ExpectedConditions class comes with a wide range of expected conditions that can be accessed with the help of the WebDriverWait reference variable and until() method.

1. elementToBeClickable() – The expected condition waits for an element to be clickable i.e. it should be present/displayed/visible on the screen as well as enabled.

Example –
wait.until(ExpectedConditions.elementToBeClickable(By.id(“button1”)));

 

2. textToBePresentInElement() – The expected condition waits for an element having a certain string pattern.

Example –
wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be found”));

 

3. alertIsPresent() – The expected condition waits for an alert box to appear.

Example –
wait.until(ExpectedConditions.alertIsPresent()) !=null);

 

4. titleIs() – The expected condition waits for a page with a specific title.

Example –
wait.until(ExpectedConditions.titleIs(“Deal of the Day”));

 

5. frameToBeAvailableAndSwitchToIt() – The expected condition waits for a frame to be available and then as soon as the frame is available, the control switches to it automatically.

Example –
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(“Vodafone_TOB”);

 

Fluent Wait in Selenium

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

 

This is all about the waits in Selenium, We have seen all three types of wait in Selenium.

We will see the use of these all incoming tutorial.

0 Comment

Leave a Reply