Wait Types – An important aspect in the Selenium automation process

Hello friends, in this blog I would be discussing the important aspect of different wait types in selenium automation process. I have seen that there is a huge confusion in understanding different wait types and its significance. So this blog would exclusively explain what are the different wait types.

There are basically three wait types in selenium automation:

  1. Implicit Wait type.
  2. Explicit Wait type.
  3. Fluent Wait type.

Implicit Wait Type


Selenium WebDriver has borrowed the idea of ‘Implicit Wait’ type from Watir. This basically tells selenium to wait for a certain amount of time before throwing an exception that it cannot find the element of the page. We should note that the implicit wait will be in place fir the entire time the browser is open. This means that any search for elements on the page by the webdriver should take the time the implicit wait is for.  If we do not provide selenium with this wait then Selenium would start throwing ElementNotFound exception because the time taken for selenium to interact with the corresponding element in the browser was not given. This is applicable for all the web elements in the browser. For example

from selenium import webdriver

class BrowserInteractions():
    def test(self):
        driver = webdriver.Firefox()
        driver.get('https://google.com')
        driver.implicitly_wait(10)

Explicit Wait Type


It is more extendable in the means that you can set it up to wait for any specific condition. Usually we use the ExpectedCondition object to specify the condition to provide the corresponding explicit wait. For example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BrowserInteractions():
    def test(self):
        driver = webdriver.Chrome()
        driver.get('http://www.quora.com/Kevin-Rose')
        element = WebDriverWait(driver, 2).until(
            EC.presence_of_element_located((By.
            PARTIAL_LINK_TEXT, "Followers")))
        element.click()

Fluent Wait Type


Fluent wait is a part of the Explicit Wait type. Fluent Wait instance defines the maximum amount of time to wait for a condition as well the frequency with which to check the condition. For example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
import time

class ExplicitWait():
    def test(self):
        baseURL = "http://www.cleartrip.com/"
        bin_path = "/usr/local/bin/chromedriver"
        driver = webdriver.Chrome(executable_path=bin_path)
        driver.get(baseURL)
        driver.implicitly_wait(.5)
        driver.maximize_window()
        input_from = driver.find_element(By.ID,"FromTag")
        input_from.send_keys("blr")
        input_to = driver.find_element(By.ID,"ToTag")
        input_to.send_keys("ccu")
        input_depart_date = driver.find_element(By.ID,"DepartDate")
        input_depart_date.send_keys("Sun, 4 Jun, 2017")
        input_depart_date.send_keys(Keys.ENTER)

        input_searchBtn = driver.find_element(By.ID,"SearchBtn")
        input_searchBtn.click()
        wait = WebDriverWait(driver, 10, poll_frequency=1,
               ignored_exceptions=[NoSuchElementException,
                                   ElementNotVisibleException,
                                   ElementNotSelectableException])
        element = wait.until(EC.
                  element_to_be_clickable((By.ID,"1_1_MULTI")))
        element.click()
        time.sleep(2)
        driver.quit()

ew = ExplicitWait()
ew.test()

This is a very important concept when we are performing automation using Selenium and I hope I was able to clear the difference between the three wait types.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s