Monday, December 6, 2010

Webdriver Extensions

I was playing with Selenium2 Webdriver, and the IWebDriverExtensions class gives you the ability to extend the base api.  I used this to create an api more like selenium1.  Now when I create a new webdriver driver, I have access to all the following functions.  Being able to do things like driver.isVisible saved much time.


    public static class IWebDriverExtensions
    {
              public static void click(this IWebDriver driver, By bylocator)
        {
            driver.WaitForElementPresent(bylocator);
            IWebElement element = driver.FindElement(bylocator);
            element.Click();
        }
        public static void type(this IWebDriver driver, By bylocator, string text)
        {
            driver.WaitForElementPresent(bylocator);
            IWebElement element = driver.FindElement(bylocator);
            element.SendKeys(text);
        }

        // This is a basic wait for element not present a'la Selenium RC
        // but sharing the same timeout value as the driver
        public static bool isVisible(this IWebDriver driver, By bylocator)
        {
            try
            {
                IRenderedWebElement element;
                element = (IRenderedWebElement)driver.FindElement(bylocator);
                return element.Displayed;

            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
        public static bool isText(this IWebDriver driver, By bylocator, string textString)
        {
            string elementText = driver.FindElement(bylocator).Text;
            if (elementText == textString)
                return true;
            else
                return false;
        }
        public static bool isTextPresent(this IWebDriver driver, By bylocator, string textString)
        {
            string pageText = driver.PageSource.ToString();
            string elementText = driver.FindElement(bylocator).Text;

            if (elementText == pageText)
                return true;
            else
                return false;
        }
        public static bool isPresent(this IWebElement driver, By bylocator)
        {
            try
            {
                IWebElement element = driver.FindElement(bylocator);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        public static void WaitForElementNotPresent(this IWebDriver driver, By bylocator)
        {
            int timeoutinteger = Common.DriverTimeout.Seconds;

            for (int second = 0; ; second++)
            {
                Thread.Sleep(1000);

                if (second >= timeoutinteger) Assert.Fail("Timeout: Element not found: " + bylocator);
                try
                {
                    IWebElement element = driver.FindElement(bylocator);
                }
                catch (NoSuchElementException)
                {
                    break;
                }
            }
        }
        public static void WaitForElementPresent(this IWebDriver driver, By bylocator)
        {
            int timeoutinteger = Common.DriverTimeout.Seconds;

            for (int second = 0; ; second++)
            {
                Thread.Sleep(1000);

                if (second >= timeoutinteger) Assert.Fail("Timeout: Element not found: " + bylocator);
                try
                {
                    IWebElement element = driver.FindElement(bylocator);
                    break;
                }
                catch (NoSuchElementException)
                {

                }
            }
        }
        public static void WaitForElementNotVisible(this IWebDriver driver, By bylocator)
        {
            int timeoutinteger = Common.DriverTimeout.Seconds;

            for (int second = 0; ; second++)
            {
                Thread.Sleep(1000);

                if (second >= timeoutinteger) Assert.Fail("Timeout: Element still visible at: " + bylocator);
                try
                {

                    if (!driver.isVisible(bylocator)) break;

                }
                catch (NoSuchElementException)
                {
                    break;
                }
            }
        }
        public static void WaitForElementVisible(this IWebDriver driver, By bylocator)
        {
            int timeoutinteger = Common.DriverTimeout.Seconds;

            for (int second = 0; ; second++)
            {
                Thread.Sleep(1000);

                if (second >= timeoutinteger) Assert.Fail("Timeout: Element not visible at: " + bylocator);
                try
                {



                    if (driver.isVisible(bylocator)) break;

                }
                catch (NoSuchElementException)
                {
                    break;
                }
            }
        }
    }

2 comments:

  1. HI,which package include the class named IWebDriverExtensions,i can't find it

    ReplyDelete
  2. Very useful post.. But i am not able to get the package which uses IWebDriverExtensions

    ReplyDelete