zoukankan      html  css  js  c++  java
  • WebDriverExtensionsByC#

    测试工具//****************************************************************************************************

    //ClassName:       WebDriverExtensions
     
    //****************************************************************************************************
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Support.UI;
    using System.Threading;
     
    namespace Edu24ol.eCommerce.Test.Comm
    {
        /// <summary>
        /// A set of CSS and form based extension methods for <see cref="IWebDriver"/>.
        /// </summary>
        public static class WebDriverExtensions
        {
            /// <summary>
            /// Clicks a button that has the given value.
            /// </summary>
            /// <param name="buttonValue">The button's value (input[value=])</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void ClickButtonWithValue(this IWebDriver webdriver, string buttonValue)
            {
                webdriver.FindElement(By.CssSelector("input[value='" + buttonValue + "']")).Click();
            }
     
            /// <summary>
            /// Clicks a button with the id ending with the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void ClickButtonWithId(this IWebDriver webdriver, string idEndsWith)
            {
                webdriver.FindElement(By.CssSelector("input[id$='" + idEndsWith + "']")).Click();
            }
     
            /// <summary>
            /// Selects an item from a drop down box using the given CSS id and the itemIndex.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which drop down box to target from the CSS selector (assuming
            /// the CSS selector returns more than one drop down box).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SelectItemInList(this IWebDriver webdriver, string selector, int itemIndex)
            {
                SelectElement element = new SelectElement(webdriver.FindElement(By.CssSelector(selector)));
                element.SelectByIndex(itemIndex);
            }
            /// <summary>
            /// Selects an item from a drop down box using the given CSS id and the value.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="value">the value.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SelectItemByValue(this IWebDriver webdriver, string selector, string value)
            {
                var element = new SelectElement(webdriver.FindElement(By.CssSelector(selector)));
                element.SelectByValue(value);
            }
     
            /// <summary>
            /// Selects an item from the nth drop down box (based on the elementIndex argument), using the given CSS id and the itemIndex.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which drop down box to target from the CSS selector (assuming
            /// the CSS selector returns more than one drop down box).</param>
            /// <param name="elementIndex">The element in the drop down list to select.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SelectItemInList(this IWebDriver webdriver, string selector, int itemIndex, int elementIndex)
            {
                SelectElement element = new SelectElement(webdriver.FindElements(By.CssSelector(selector))[elementIndex]);
                element.SelectByIndex(itemIndex);
            }
     
            /// <summary>
            /// Returns the number of elements count that match the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The number of elements found.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static int ElementCount(this IWebDriver webdriver, string selector)
            {
                return webdriver.FindElements(By.CssSelector(selector)).Count;
            }
     
            /// <summary>
            /// Returns the number of ElementIList that match the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The number of elements found.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static IList<IWebElement> ElementIList(this IWebDriver webdriver, string selector)
            {
                return webdriver.FindElements(By.CssSelector(selector));
            }
     
            /// <summary>
            /// Gets the selected index from a drop down box using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The index of the selected item in the drop down box</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static int SelectedIndex(this IWebDriver webdriver, string selector)
            {
                SelectElement element = new SelectElement(webdriver.FindElement(By.CssSelector(selector)));
     
                for (int i = 0; i < element.Options.Count; i++)
                {
                    if (element.Options[i].Selected)
                        return i;
                }
     
                return -1;
            }
     
            /// <summary>
            /// Gets the selected index from a drop down box using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <param name="selectedIndex"></param>
            /// <returns>The index of the selected item in the drop down box</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static int SetSelectedIndex(this IWebDriver webdriver, string selector, int selectedIndex)
            {
                SelectElement element = new SelectElement(webdriver.FindElement(By.CssSelector(selector)));
                if (selectedIndex >= element.Options.Count)
                    selectedIndex = 0;
                element.SelectByIndex(selectedIndex);
                return -1;
            }
            /// <summary>
            /// Gets the selected index from the nth drop down box (based on the elementIndex argument), using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which drop down box to target from the CSS selector (assuming
            /// the CSS selector returns more than one drop down box).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The index of the selected item in the drop down box</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static int SelectedIndex(this IWebDriver webdriver, string selector, int itemIndex)
            {
                SelectElement element = new SelectElement(webdriver.FindElements(By.CssSelector(selector))[itemIndex]);
     
                for (int i = 0; i < element.Options.Count; i++)
                {
                    if (element.Options[i].Selected)
                        return i;
                }
     
                return -1;
            }
     
            /// <summary>
            /// Gets the selected value from a drop down box using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The value of the selected item.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static string GetSelectedItemValue(this IWebDriver webdriver, string selector)
            {
                SelectElement element = new SelectElement(webdriver.FindElement(By.CssSelector(selector)));
                return element.SelectedOption.GetAttribute("value");
            }
     
            /// <summary>
            /// Clicks a link with the text provided. This is case sensitive and searches using an Xpath contains() search.
            /// </summary>
            /// <param name="linkContainsText">The link text to search for.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void ClickLinkWithText(this IWebDriver webdriver, string linkContainsText)
            {
                webdriver.FindElement(By.XPath("//a[contains(text(),'" + linkContainsText + "')]")).Click();
            }
     
            /// <summary>
            /// Clicks a link with the id ending with the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void ClickLinkWithId(this IWebDriver webdriver, string idEndsWith)
            {
                webdriver.FindElement(By.CssSelector("a[id$='" + idEndsWith + "']")).Click();
            }
     
            /// <summary>
            /// Clicks an element using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void Click(this IWebDriver webdriver, string selector)
            {
                webdriver.FindElement(By.CssSelector(selector)).Click();
            }
     
            /// <summary>
            /// Clicks an element using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which element to target from the CSS selector (assuming
            /// the CSS selector returns more than one element).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void Click(this IWebDriver webdriver, string selector, int itemIndex)
            {
                webdriver.FindElements(By.CssSelector(selector))[itemIndex].Click();
            }
     
            /// <summary>
            /// Gets an input element with the id ending with the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>An <see cref="IWebElement"/> for the item matched.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static IWebElement InputWithId(this IWebDriver webdriver, string idEndsWith)
            {
                return webdriver.FindElement(By.CssSelector("input[id$='" + idEndsWith + "']"));
            }
     
            /// <summary>
            /// Gets an element's value with the id ending with the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The element's value.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static string ElementValueWithId(this IWebDriver webdriver, string idEndsWith)
            {
                return webdriver.FindElement(By.CssSelector("input[id$='" + idEndsWith + "']")).GetAttribute("value");
            }
     
            /// <summary>
            /// Gets an element's value using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The element's value.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static string ElementValue(this IWebDriver webdriver, string selector)
            {
                return webdriver.FindElement(By.CssSelector(selector)).GetAttribute("value");
            }
     
            /// <summary>
            /// Gets an element's value using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which element to target from the CSS selector (assuming
            /// the CSS selector returns more than one element).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The element's value.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static string ElementValue(this IWebDriver webdriver, string selector, int itemIndex)
            {
                return webdriver.FindElements(By.CssSelector(selector))[itemIndex].GetAttribute("value");
            }
     
            /// <summary>
            /// Gets an element's text using the given CSS selector.
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="itemIndex">A zero-based index that determines which element to target from the CSS selector (assuming
            /// the CSS selector returns more than one element).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>The element's text.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static string ElementText(this IWebDriver webdriver, string selector, int itemIndex)
            {
                return webdriver.FindElements(By.CssSelector(selector))[itemIndex].Text;
            }
     
            /// <summary>
            /// Return true if the checkbox with the id ending with the provided value is checked/selected.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <returns>True if the checkbox is checked.</returns>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static bool IsCheckboxChecked(this IWebDriver webdriver, string idEndsWith)
            {
                return webdriver.FindElement(By.CssSelector("input[id$='" + idEndsWith + "']")).Selected;
            }
     
            /// <summary>
            /// Clicks the checkbox with the id ending with the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void ClickCheckbox(this IWebDriver webdriver, string idEndsWith)
            {
                webdriver.FindElement(By.CssSelector("input[id$='" + idEndsWith + "']")).Click();
            }
     
            /// <summary>
            /// Sets an element's (an input field) value to the provided text by using SendKeys().
            /// </summary>
            /// <param name="value">The text to type.</param>
            /// <param name="element">A <see cref="IWebElement"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SetValue(this IWebElement element, string value)
            {
                element.SendKeys(value);
            }
     
            /// <summary>
            /// Sets an element's (an input field) value to the provided text, using the given CSS selector and using SendKeys().
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="value">The text to type.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SetValue(this IWebDriver webdriver, string selector, string value)
            {
                webdriver.FindElement(By.CssSelector(selector)).Clear();
                webdriver.FindElement(By.CssSelector(selector)).SendKeys(value);
            }
     
            /// <summary>
            /// Sets an element's (an input field) value to the provided text, using the given CSS selector and using SendKeys().
            /// </summary>
            /// <param name="selector">A valid CSS selector.</param>
            /// <param name="value">The text to type.</param>
            /// <param name="itemIndex">A zero-based index that determines which element to target from the CSS selector (assuming
            /// the CSS selector returns more than one element).</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void SetValue(this IWebDriver webdriver, string selector, string value, int itemIndex)
            {
                webdriver.FindElements(By.CssSelector(selector))[itemIndex].Clear();
                webdriver.FindElements(By.CssSelector(selector))[itemIndex].SendKeys(value);
            }
     
            /// <summary>
            /// Sets the textbox with the given CSS id to the provided value.
            /// </summary>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="value">The text to type.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void FillTextBox(this IWebDriver webdriver, string idEndsWith, string value)
            {
                webdriver.SetValue("input[id$='" + idEndsWith + "']", value);
            }
     
            /// <summary>
            /// Sets the textarea with the given CSS id to the provided value.
            /// </summary>
            /// <param name="value">The text to set the value to.</param>
            /// <param name="idEndsWith">A CSS id.</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            /// <exception cref="OpenQA.Selenium.NoSuchElementException">No element was found.</exception>
            public static void FillTextArea(this IWebDriver webdriver, string idEndsWith, string value)
            {
                webdriver.SetValue("textarea[id$='" + idEndsWith + "']", value);
            }
     
            /// <summary>
            /// Waits the specified time in second (using a thread sleep)
            /// </summary>
            /// <param name="seconds">The number of seconds to wait (this uses TimeSpan.FromSeconds)</param>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            [Obsolete("Use WaitForElementDisplayed instead, as Wait uses Thread.Sleep")]
            public static void Wait(this IWebDriver webdriver, double seconds)
            {
                Thread.Sleep(TimeSpan.FromSeconds(seconds));
            }
     
            /// <summary>
            /// Waits 2 seconds, which is *usually* the maximum time needed for all Javascript execution to finish on the page.
            /// </summary>
            /// <param name="webdriver">A <see cref="IWebDriver"/> instance.</param>
            [Obsolete("Use WaitForElementDisplayed instead, as WaitForPageLoad uses Thread.Sleep")]
            public static void WaitForPageLoad(this IWebDriver webdriver, int second = 2)
            {
                webdriver.Wait(second);
            }
     
            /// <summary>
            /// Waits for an elements to be displayed on the page for the time specified.
            /// </summary>
            /// <param name="driver">A <see cref="IWebDriver"/> instance.</param>
            /// <param name="by">The selector to find the element with.</param>
            /// <param name="timeoutInSeconds">The number of seconds to wait</param>
            /// <returns></returns>
            public static IWebElement WaitForElementDisplayed(this IWebDriver driver, By by, int timeoutInSeconds = 10)
            {
                try
                {
                    if (driver.IsElementDisplayed(by))
                    {
                        return driver.FindElement(by);
                    }
                    return null;
                }
                catch
                {
                    return null;
                }
            }
     
            /// <summary>
            /// Determines whether the element provided by the selector is displayed or not, waiting a 
            /// certain amount of time for it to be displayed.
            /// </summary>
            /// <param name="driver">A <see cref="IWebDriver"/> instance.</param>
            /// <param name="by">The selector to find the element with.</param>
            /// <param name="timeoutInSeconds">The number of seconds to wait.</param>
            /// <returns>True if the element is displayed, false otherwise.</returns>
            public static bool IsElementDisplayed(this IWebDriver driver, By by, int timeoutInSeconds = 10)
            {
                try
                {
                    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                    return wait.Until<bool>(x => x.FindElement(by).Displayed);
                }
                catch
                {
                    return false;
                }
            }
     
            /// <summary>
            /// 设置  send Key
            /// </summary>
            /// <param name="element"></param>
            /// <param name="keys"></param>
            /// <returns></returns>
            public static IWebElement SetKeyInfo(this IWebElement element, string keys)
            {
                if (element == null)
                    return null;
                element.Clear();
                element.SendKeys(keys);
                return element;
            }
     
     
            /// <summary>
            /// 获取元素
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="id"></param>
            /// <returns></returns>
            public static IWebElement GetElementById(this IWebDriver driver, string id)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.Id(id));
            }
            /// <summary>
            /// 查找的元素是否存在
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="by"></param>
            /// <returns></returns>
            public static bool IsElementExits(this IWebDriver driver, By by)
            {
                if (driver == null)
                    return false;
                try
                {
                    driver.FindElement(by);
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
            /// <summary>
            /// 获取元素
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="id"></param>
            /// <returns></returns>
            public static bool IsExitsElementById(this IWebDriver driver, string id)
            {
                if (driver == null)
                    return false;
                try
                {
                    driver.FindElement(By.Id(id));
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
     
            }
            /// <summary>
            /// ClassName 
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="className"></param>
            /// <returns></returns>
            public static IWebElement GetElementByClassName(this IWebDriver driver, string className)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.ClassName(className));
            }
     
     
            /// <summary>
            /// 
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public static IWebElement GetElementByName(this IWebDriver driver, string name)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.Name(name));
            }
            /// <summary>
            /// get name
            /// </summary>
            /// <param name="webElement"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public static IWebElement GetElementByName(this IWebElement webElement, string name)
            {
                if (webElement == null)
                    return null;
                return webElement.FindElement(By.Name(name));
            }
            /// <summary>
            ///  TagName
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="tagName"></param>
            /// <returns></returns>
            public static IWebElement GetElementByTagName(this IWebDriver driver, string tagName)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.TagName(tagName));
            }
            /// <summary>
            /// ElementByTagName
            /// </summary>
            /// <param name="webElement"></param>
            /// <param name="tagName"></param>
            /// <returns></returns>
            public static IWebElement GetElementByTagName(this IWebElement webElement, string tagName)
            {
                if (webElement == null)
                    return null;
                return webElement.FindElement(By.TagName(tagName));
            }
            /// <summary>
            /// GetElemenstByTagName
            /// </summary>
            /// <param name="webElement"></param>
            /// <param name="tagName"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElemenstByTagName(this IWebElement webElement, string tagName)
            {
                if (webElement == null)
                    return null;
                return webElement.FindElements(By.TagName(tagName));
            }
     
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="webElement"></param>
            /// <param name="tagName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static IWebElement GetElementByTagName(this IWebElement webElement, string tagName, string key,
                string value)
            {
                if (webElement == null)
                    return null;
                var searchList = webElement.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
     
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var element = searchList[elementIndex];
                        if (element.GetAttribute(key).Trim() == value.Trim())
                        {
                            return element;
                        }
                    }
                }
                return null;
            }
     
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="tagName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static IWebElement GetElementByTagName(this IWebDriver driver, string tagName, string key, string value)
            {
                if (driver == null)
                    return null;
                var searchList = driver.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
     
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var element = searchList[elementIndex];
                        if (element.GetAttribute(key).Trim() == value.Trim())
                        {
                            return element;
                        }
                    }
                }
                return null;
            }
     
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="tagName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByTagName(this IWebDriver driver, string tagName, string key,
                string value)
            {
                var elementList = new List<IWebElement>();
                if (driver == null)
                    return elementList;
                var searchList = driver.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var element = searchList[elementIndex];
                        if (element.Displayed)
                        {
                            if (element.GetAttribute(key).Trim() == value.Trim())
                            {
                                elementList.Add(element);
                            }
                        }
     
                    }
                }
                return elementList;
            }
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="element"></param>
            /// <param name="tagName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByTagName(this IWebElement element, string tagName, string key,
                string value)
            {
                var elementList = new List<IWebElement>();
                if (element == null)
                    return elementList;
                var searchList = element.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var elementSub = searchList[elementIndex];
                        if (elementSub.GetAttribute(key).Trim() == value.Trim())
                        {
                            elementList.Add(elementSub);
                        }
                    }
                }
                return elementList;
            }
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="tagName"></param>
            /// <param name="key1"></param>
            /// <param name="value1"></param>
            /// <param name="key2"></param>
            ///<param name="value2"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByTagName(this IWebDriver driver, string tagName, string key1, string value1,
                string key2, string value2)
            {
                if (driver == null)
                    return null;
                var elementList = new List<IWebElement>();
                var searchList = driver.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var element = searchList[elementIndex];
                        if (element.GetAttribute(key1).Trim() == value1.Trim() && element.GetAttribute(key2) == value2)
                        {
                            elementList.Add(element);
                        }
                    }
                }
                return elementList;
            }
            /// <summary>
            /// ByTagName
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="tagName"></param>
            /// <param name="key1"></param>
            /// <param name="value1"></param>
            /// <param name="key2"></param>
            ///<param name="value2"></param>
            /// <returns></returns>
            public static IWebElement GetElementByTagName(this IWebDriver driver, string tagName, string key1, string value1,
                string key2, string value2)
            {
                if (driver == null)
                    return null;
                var searchList = driver.FindElements(By.TagName(tagName));
                if (searchList != null && searchList.Count > 0)
                {
     
                    for (int elementIndex = 0; elementIndex < searchList.Count; elementIndex++)
                    {
                        var element = searchList[elementIndex];
                        if (element.GetAttribute(key1).Trim() == value1.Trim() && element.GetAttribute(key2) == value2)
                        {
                            return element;
                        }
                    }
                }
                return null;
            }
     
            /// <summary>
            /// ByCssSelector
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="cssSelector"></param>
            /// <returns></returns>
            public static IWebElement GetElementByCssSelector(this IWebDriver driver, string cssSelector)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.CssSelector(cssSelector));
            }
     
            /// <summary>
            /// ByCssSelector
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="cssSelector"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByCssSelector(this IWebDriver driver, string cssSelector)
            {
                if (driver == null)
                    return null;
                return driver.FindElements(By.CssSelector(cssSelector));
            }
            /// <summary>
            /// ByCssSelector
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="cssName"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByCss(this IWebDriver driver, string cssName)
            {
                if (driver == null)
                    return null;
                return driver.FindElements(By.ClassName(cssName));
            }
            /// <summary>
            /// ByPartialLinkText
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="partialLinkText"></param>
            /// <returns></returns>
            public static IWebElement GetElementByPartialLinkText(this IWebDriver driver, string partialLinkText)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.PartialLinkText(partialLinkText));
            }
            /// <summary>
            /// ByPartialLinkText
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="partialLinkText"></param>
            /// <returns></returns>
            public static IList<IWebElement> GetElementsByPartialLinkText(this IWebDriver driver, string partialLinkText)
            {
                if (driver == null)
                    return null;
                return driver.FindElements(By.PartialLinkText(partialLinkText));
            }
            /// <summary>
            /// ByLinkTex
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="linkText"></param>
            /// <returns></returns>
            public static IWebElement GetElementByLinkText(this IWebDriver driver, string linkText)
            {
                if (driver == null)
                    return null;
                return driver.FindElement(By.LinkText(linkText));
            }
     
            /// <summary>
            /// 
            /// </summary>
            /// <param name="element"></param>
            /// <param name="linkText"></param>
            /// <returns></returns>
            public static IWebElement GetElementByLinkText(this IWebElement element, string linkText)
            {
                return element.FindElement(By.LinkText(linkText));
            }
     
            /// <summary>
            /// get links by css selector(rules:parentCssSelecter + " a",默认body a),去除重复数据
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="parentCssSelecter"></param>
            /// <returns></returns>
            public static IList<string> HrefList(this IWebDriver driver, string parentCssSelecter = "body")
            {
                return driver.FindElements(By.CssSelector(parentCssSelecter + " a"))
                    .Select(l => l.GetAttribute("href")).Distinct().ToList();
            }
     
            /// <summary>
            /// get page links by css selector(rules:parentCssSelecter + " .pager ul li a",默认body .pager ul li a),去除重复数据
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="parentCssSelecter"></param>
            /// <returns></returns>
            public static IList<string> GetPageListLinks(this IWebDriver driver, string parentCssSelecter = "body")
            {
                var result = driver.FindElements(By.CssSelector(parentCssSelecter + " .pager ul li a"))
                    .Select(l => l.GetAttribute("href")).Distinct().ToList();
                result.Add(driver.Url);//current
                return result;
            }
     
            #region 页面跳转
            /// <summary>
            /// 跳转
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="url"></param>
            public static void Redirct(this IWebDriver driver, string url)
            {
                driver.Navigate().GoToUrl(url);
            }
            #endregion
     
            #region 保存截图和详情
            ///  <summary>
            ///  This will Take the screen shot of the webpage and will save it at particular location
            ///  </summary>      ///
            /// <param name="driver"></param>
            /// <param name="screenshotFirstName"></param>
            /// <param name="imageFormat"></param>
            public static void SaveScreenShot(this IWebDriver driver, string screenshotFirstName, string imageFormat = ".png")
            {
                var folderLocation = Environment.CurrentDirectory.Replace("Out", "\ScreenShot\");
                if (!Directory.Exists(folderLocation))
                {
                    Directory.CreateDirectory(folderLocation);
                }
                var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
                var filename = new StringBuilder(folderLocation);
                filename.Append(screenshotFirstName);
                filename.Append(DateTime.Now.ToString("dd-mm-yyyy HH_mm_ss"));
                filename.Append(imageFormat);
     
                if (File.Exists(filename.ToString()))
                {
                    File.Delete(filename.ToString());
                }
     
                screenshot.SaveAsFile(filename.ToString(), System.Drawing.Imaging.ImageFormat.Png);
            }
            /// <summary>
            /// 保存文件
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="fileName"></param>
            /// <param name="details"></param>
            public static void SaveFile(this IWebDriver driver, string fileName, string details)
            {
                var folderLocation = Environment.CurrentDirectory.Replace("Out", "\ScreenShot\");
                if (!Directory.Exists(folderLocation))
                {
                    Directory.CreateDirectory(folderLocation);
                }
                var filename = new StringBuilder(folderLocation);
                filename.Append(fileName);
                filename.Append(DateTime.Now.ToString("dd-mm-yyyy HH_mm_ss"));
                filename.Append(".txt");
     
                var fullPath = filename.ToString();
                if (File.Exists(filename.ToString()))
                {
                    File.Delete(filename.ToString());
                }
                using (var file = new StreamWriter(fullPath))
                {
                    file.WriteLine(details);
                }
            }
            #endregion
     
            /// <summary>
            ///  判断是否弹框出现
            /// </summary>
            /// <param name="driver"></param>
            /// <returns></returns>
            public static bool IsAlertPresent(this IWebDriver driver)
            {
                try
                {
                    driver.SwitchTo().Alert();
                    return true;
                }   // try 
                catch (NoAlertPresentException ex)
                {
                    return false;
                }   // catch 
            }
     
            /// <summary>
            /// 确定
            /// </summary>
            /// <param name="driver"></param>
            /// <returns></returns>
            public static bool AlertAccept(this IWebDriver driver)
            {
                try
                {
                    driver.SwitchTo().Alert().Accept();
                    return true;
                }   // try 
                catch (NoAlertPresentException ex)
                {
                    return false;
                }   // catch 
            }
     
            #region javascript
            /// <summary>
            /// Js   Scripts execute
            /// </summary>
            /// <param name="driver"></param>
            /// <returns></returns>
            public static IJavaScriptExecutor Scripts(this IWebDriver driver)
            {
                return (IJavaScriptExecutor)driver;
            }
     
            /// <summary>
            /// 执行js脚本
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="js"></param>
            public static object ExcuteScripts(this IWebDriver driver, string js)
            {
                return driver.Scripts().ExecuteScript(js);
            }
     
            /// <summary>
            /// 兼容FF google 的A click执行方式
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="aWebElement"></param>
            public static void AClick(this IWebDriver driver, IWebElement aWebElement)
            {
                driver.Scripts().ExecuteScript("arguments[0].click();", aWebElement);
            }
     
            /// <summary>
            /// 执行jquery 选择器
            /// </summary>
            /// <param name="driver"></param>
            /// <param name="cssSelecter"></param>
            /// <returns></returns>
            public static IWebElement DisplayByCssSelecter(this IWebDriver driver, string cssSelecter)
            {
                return
                    (IWebElement)
                        driver.ExcuteScripts(string.Format("var result=$("{0}");result.show();return result[0];",
                            cssSelecter));
            }
            #endregion
        }
    }
  • 相关阅读:
    gmap4rails
    gmap4rails
    devise的使用的中文教程
    对每个 IP 访问量实时监控。
    下载文件总结
    安装Wamp后 Apache无法启动的解决方法
    CSS,fontfamily,好看常用的中文字体
    TP框架下载功能  不想下天桥  博客园
    用PHP,怎么获取PHP.ini中的文件上传最大的字节数。也就是默认的2M
    这个技术wiki的内容很不错
  • 原文地址:https://www.cnblogs.com/wcLT/p/3692552.html
Copyright © 2011-2022 走看看