TESTEROPS

A pragmatic approach to QA and OPS

Implicit Waits and Explicit Waits

Implicit Waits

As we have learnt before, implicit waits are a method to make the test wait for the webpage’s components to load, so that they can be available during our tests. Selenium originally borrowed this idea from Watir. Now what does implicit wait does actually. Suppose we wrote a script like this

 


from selenium import webdriver
import unittest

class AlertHandle(unittest.TestCase):

   def setUp(self):
      self.driver=webdriver.Firefox()
      self.driver.implicitly_wait(30)
      self.driver.maximize_window()

      self.driver.get('http://www.questionselenium.com/2013/09/sample-confirmation-box.html')

  def test_Alterbox(self):
      print("This is a test to show implicit wait")
  def tearDown(self):
      self.driver.quit()

if __name__=="__main__"
unittest.main(versbosity=2)

Now the implicitly_wait( ) method tells the script , more precisely, it tells the Webdriver to poll the DOM for a certain amount of time , here for 30 seconds, when trying to find an element or elements if they are not immediately available. Here by poll, we mean to check the DOM again and again.

Once defined, implicit wait will be defined for the whole life time of a Webdriver object instance, until it is changed. So once defined in a script, it will be active for the lifetime of a script, until modified. By default, the implicit wait time set is 0.

Also, implicit wait won’t allow you to set any predefined conditions to wait for any webelement to be present or as such. If you want such kinda flexibility, you need to take the help of explicit wait, which is why we are going to discuss this in next section.

 

Explicit Wait

Not in every situation you want a predefined wait to be available in tests and wait for that long duration, for the element to be present or be visible in the DOM. We want to define a  wait condition, so that we can be able to wait for only that specific web element to be present and then carry on with the script functioning. To achieve this Selenium webdriver API provides us with explicit waits.

Explicit waits are a good way to organize a test script and provide more flexibility, by allowing us to design out tests in a way, where we can wait for some predefined or custom conditions and then carry on with what we want.

Explicit waits are implemented using the WebDriverWait and expected_conditions class of the python/Java API.

Let see an suppose we have a JS alert on a webpage, and we want to see if that clicking on that button, which fires the alert , actually fires the alert on not. Now, we would want this test to wait to click on the button and then only wait for that amount of time when the alert is not present. Once the alert appears, we would just want to accept it and then go on and accept the alert.

 


import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

class Explicit_waiting(unittest.TestCase):

   def setUp(self):
       self.driver=webdriver.Firefox()
       self.driver.maximize_window()

       #getting the browser navigation
       self.driver.get('')

   def test_BrowserNav(self):
       driver=self.driver

       tt1=driver.find_element_by_id('timingAlert')
       tt1.click()

       WebDriverWait(self.driver,10).until(expected_conditions.alert_is_present())

       alert1=driver.switch_to.alert

       alert1.accept()

   def tearDown(self):
       self.driver.quit()

if __name__="__main__":
unittest.main(verbosity=2)

 

Now, focus specifically on this line of code

 WebDriverWait(self.driver,10).until(expected_conditions.alert_is_present())

Here, we are using the WebDriverWait to make the driver wait a maximum of 10 seconds, until, the alert shows up. If the alert doesn’t appear in that stipulated time, the Webdriver will throw an NoAlertPresentException

During the course of test execution, Webdriver calls the ExpectedCondition every 500 milliseconds, untill it returns successfully.

 

Expected Conditions :

These are some common conditions which are frequently encountered when automating web applications.

Selenium Python binding provides some convenience methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable

You can read about the whole list of the expected conditions in the official Selenium python documentation here.

 

 

6 thoughts on “Implicit Waits and Explicit Waits

  1. Hi ,

    I have tried all waits for page load but still i am unable to proceed the actions .Could you please help me from this .

    Thanks,
    Sai

    Like

      1. I am implementing automation script for web application .In this application every entry internally page getting refreshing (It’s waiting for DOM load ) . I have tried all waits which i know like :
        1. wait for page to load
        2.wait for element visible
        3.wait for element clickable
        4.wait for element present
        5.wait for element not visible
        6.implicit wait

        Still i am getting pass cases .when i am using time.sleep(2) ..it’s getting pass.

        Thanks,
        Sai

        Liked by 1 person

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: