Enroll in Selenium Training

Wait is a very important concept in any automation project. Waits are very vital to any project primarily because of the unpredictability that comes with automation. Sometimes you may find that the element you are trying to perform an action on is loaded little late, as a result your automation script breaks. In case of Web application, this unpredictability increases quite considerably. It's because of the following factors

  • Slow internet connection
  • Ajax calls made by elements
  • Data base calls made by website
  • Slow browsers
  • Delay loaded elements, which are used to optimize bandwidth usage

There could be many other factors too. The key point to understand is that we have to cater to these delays and have to write code in such a way that any delay is handled properly and your test scripts don’t break unnecessarily. For this Selenium Webdriver comes with a some really good inbuilt waits. We will see them one by one. Assume that you have a test web application and in your application there is a link. This link takes a varying amount of time to appear on the page. Some time it's present on the page as soon as the page loads and at other times it appears on the page after a delay of some seconds. So if you write a simple IWebDriver.FindElement command it will sometimes pass and at other times is will cause an NoSuchElement exception in your application. How can you handle this situation? Here are some ways to handle this in your test. We have created an element with ID = “target” on this page, so that we can show concept of waits with example. http://toolsqa.com/automation-practice-switch-windows/

As shown in the image, the element is present at the bottom of the page and it comes there after some time.

SomethingPageLoad

Implicit wait

Implicit wait is used to inform webdriver that there could be cases when some elements on the webpage will not be present instantaneously. In those cases webdriver has to continuously retry for a given amount of time until either the time expires or the element is found, which ever occurs first. This is called implicit wait and below is the code to specify implicit wait

        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://toolsqa.com/automation-practice-switch-windows/");
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(50));
            IWebElement element = driver.FindElement(By.Id("target"));
        }

Important points to remember

  • Default value of implicit wait is 0 seconds. This means that WebDriver will try to find the element only once and after that it will throw an exception if element is not found.
  • Implicit wait has a default polling time of 250 milliseconds. This means that WebDriver will poll the Dom after every 250 milliseconds till the element is found or the timeout specified it exhausted.
  • Implicit wait once applied lasts for the whole session, this means till the time your IWebDriver object is alive.
  • Implicit wait can be changed any number of times till the IWebDriver session is alive.
  • If the element is not found within the specified limits of implicit wait, a NoSuchElementException is thrown by WebDriver.

Page Load Timeout

Purpose: Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(500));
            driver.Navigate().GoToUrl("https://toolsqa.com/automation-practice-switch-windows/");
            IWebElement element = driver.FindElement(By.Id("target"));
        }

Set Script Time Out

Purpose: Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(500));
            driver.Navigate().GoToUrl("https://toolsqa.com/automation-practice-switch-windows/");
            IWebElement element = driver.FindElement(By.Id("target"));
        }

Sleep Command

Purpose: This is rarely used, as it always force the browser to wait for a specific time. Thread .Sleep is never a good idea and that’s why Selenium provides wait primitives.

       static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://toolsqa.com/automation-practice-switch-windows/");
            Thread.Sleep(6000);
            IWebElement element = driver.FindElement(By.Id("target"));
        }

In the next chapter, we will discuss explicit waits and will try to understand the code behind it.

Practice Exercise

  1. Launch new Browser

  2. Navigate to the page "https://toolsqa.com/automation-practice-switch-windows/"

  3. Try to find an element with ID="target"

  4. Check if you get any exception while finding this element?

If you get an exception finding this element, try to put a Wait and see if you can handle this delay. Try to put a simple Thread sleep or maybe an Implicit wait.

Solution

        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(500));
            driver.Navigate().GoToUrl("https://toolsqa.com/automation-practice-switch-windows/");
            IWebElement element = driver.FindElement(By.Id("target"));
        }

How to Handle Multiple Browsers in Selenium CSharp
How to Handle Multiple Browsers in Selenium CSharp
Previous Article
Explicit Waits FluentWaits in Selenium C#
Explicit Waits FluentWaits in Selenium C#
Next Article
Virender Singh
I am Virender Singh, I have around 14 years of experience in the Technology domain.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback