Saturday, March 23, 2013

An example for creating an implicit wait in Selenium WebDriver C#

An example for creating an implicit wait in Selenium WebDriver C#

implicit wait - It's global setting applicable for all elements and if element appear before specified time than script will start executing otherwise script will throw NoSuchElementException.  In the below example an implicit wait of 1 second is added in the line 
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));


using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;

namespace BusinessCreation
{
    class WaitTimeoutsTimers
    {
            static void Main(string[] args)
            {
                IWebDriver driver = new FirefoxDriver();
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));
                driver.Navigate().GoToUrl("https://www.google.com.au");
                driver.FindElement(By.Name("q")).Clear();
                driver.FindElement(By.Name("q")).SendKeys("Chery Jose");
                if (driver.FindElement(By.Name("btnK")).Displayed)
                {
                    driver.FindElement(By.Name("btnK")).Click();
                 }
                else if (driver.FindElement(By.Name("btnG")).Displayed)
                {

                    driver.FindElement(By.Name("btnG")).Click();
                    driver.FindElement(By.XPath("//a[@href='http://au.linkedin.com/in/cheryjose']")).Click();
                }

            }
    }
}

No comments:

Post a Comment