/// <summary> /// Click and hold an element /// </summary> /// <param name="element">Page element</param> /// <param name="ms">Number of milliseconds to hold down the click</param> public static void ClickLong(this IBrowser browser, PageElement element, int ms) { var action = new Actions(browser.IWebDriver); action.ClickAndHold(browser.Find(element)).Build().Perform(); Thread.Sleep(ms); action.Release(browser.Find(element)).Build().Perform(); }
/// <summary> /// Click and hold an element /// </summary> /// <param name="element">Page element</param> /// <param name="ms">Number of milliseconds to hold down the click</param> public static void ClickLong(this IBrowser browser, PageElement element, int ms) { var action = new Actions(browser.IWebDriver); action.ClickAndHold(browser.Find(element)).Build().Perform(); Thread.Sleep(ms); action.Release(browser.Find(element)).Build().Perform(); }
/// <summary> /// Find a IWebElement from the current PageElement /// </summary> /// <param name="element">this Page Elemtn</param> /// <param name="locator">By object</param> /// <returns>IWebElement</returns> public static IWebElement Find(this PageElement element, By locator) { try { return(element.IWebElement.FindElement(locator)); } catch (NoSuchElementException) { throw new PageElementNotFoundException(); } }
/// <summary> /// Find a page element's IWebElement /// </summary> /// <param name="element">Page Element to search for</param> /// <returns>IWebElement of the page element</returns> public static IWebElement Find(this IBrowser browser, PageElement element) { try { switch (element.Type.ToLower()) { case "id": return(browser.IWebDriver.FindElement(By.Id(element.Locator))); case "class": return(browser.IWebDriver.FindElement(By.ClassName(element.Locator))); case "css": return(browser.IWebDriver.FindElement(By.CssSelector(element.Locator))); case "link": return(browser.IWebDriver.FindElement(By.LinkText(element.Locator))); case "name": return(browser.IWebDriver.FindElement(By.Name(element.Locator))); case "partial-link": return(browser.IWebDriver.FindElement(By.PartialLinkText(element.Locator))); case "tag": return(browser.IWebDriver.FindElement(By.TagName(element.Locator))); case "href": return(browser.IWebDriver.FindElement(By.CssSelector("[href='" + element.Locator + "']"))); case "xpath": return(browser.IWebDriver.FindElement(By.XPath(element.Locator))); default: if (element.Type.Contains("data-")) { return(browser.IWebDriver.FindElement(By.CssSelector("[" + element.Type + "='" + element.Locator + "']"))); } else { throw new PageElementTypeException("'" + element.Type + "' is not a valid type to find a page element by."); } } } catch (NoSuchElementException) { try { browser.WaitForElementPresent(element); return(browser.Find(element)); } catch (BrowserTimeoutException bte) { throw new PageElementNotFoundException("Browser timed out trying to find element.", bte); } } }
/// <summary> /// Find multiple IWebElement from the current PageElement /// </summary> /// <param name="element">this Page Elemtn</param> /// <param name="locator">By object</param> /// <returns>List of IWebElement</returns> public static IEnumerable <IWebElement> FindMultiple(this PageElement element, By locator) { try { return(element.IWebElement.FindElements(locator)); } catch (NoSuchElementException) { throw new PageElementNotFoundException(); } }
/// <summary> /// Find a page element's IWebElement /// </summary> /// <param name="element">Page Element to search for</param> /// <returns>IWebElement of the page element</returns> public static IWebElement Find(this IBrowser browser, PageElement element, bool attemptWait = true) { try { switch (element.Type.ToLower()) { case "id": return browser.IWebDriver.FindElement(By.Id(element.Locator)); case "class": return browser.IWebDriver.FindElement(By.ClassName(element.Locator)); case "css": return browser.IWebDriver.FindElement(By.CssSelector(element.Locator)); case "link": return browser.IWebDriver.FindElement(By.LinkText(element.Locator)); case "name": return browser.IWebDriver.FindElement(By.Name(element.Locator)); case "partial-link": return browser.IWebDriver.FindElement(By.PartialLinkText(element.Locator)); case "tag": return browser.IWebDriver.FindElement(By.TagName(element.Locator)); case "href": return browser.IWebDriver.FindElement(By.CssSelector("[href='" + element.Locator + "']")); case "xpath": return browser.IWebDriver.FindElement(By.XPath(element.Locator)); default: if (element.Type.Contains("data-")) { return browser.IWebDriver.FindElement(By.CssSelector("[" + element.Type + "='" + element.Locator + "']")); } else { throw new PageElementTypeException("'" + element.Type + "' is not a valid type to find a page element by."); } } } catch (NoSuchElementException) { if (attemptWait) { try { browser.WaitForElementPresent(element); return browser.Find(element, false); } catch (BrowserTimeoutException bte) { throw new PageElementNotFoundException("Browser timed out trying to find element.", bte); } } else { throw new PageElementNotFoundException("Browser timed out trying to find element."); } } }
/// <summary> /// Wait for a page element to disappear /// </summary> /// <param name="element">Page element waiting on to disappear</param> public static void WaitForElementNotPresent(this IBrowser browser, PageElement element) { var start = DateTime.Now; while (DateTime.Now.Subtract(start).Seconds < Common.BrowserTimeout.Seconds) { try { IWebElement temp = browser.Find(element); Thread.Sleep(200); } catch (NoSuchElementException) { return; } } throw new BrowserTimeoutException("Timeout: Element continues to stay present."); }
/// <summary> /// Wait for a page element to become present /// </summary> /// <param name="element">Page element waiting for</param> public static void WaitForElementPresent(this IBrowser browser, PageElement element) { var start = DateTime.Now; while (DateTime.Now.Subtract(start).Seconds < Common.BrowserTimeout.Seconds) { try { IWebElement temp = browser.Find(element, false); return; } catch (NoSuchElementException) { Thread.Sleep(200); } } throw new BrowserTimeoutException("Timeout: Element not found."); }
/// <summary> /// Hover the pointer over an element /// </summary> /// <param name="element">Page element</param> public static void Hover(this IBrowser browser, PageElement element) { var action = new Actions(browser.IWebDriver); action.MoveToElement(browser.Find(element)).Build().Perform(); }
/// <summary> /// Double click an element /// </summary> /// <param name="element">Page element</param> public static void ClickDouble(this IBrowser browser, PageElement element) { var action = new Actions(browser.IWebDriver); action.DoubleClick(browser.Find(element)).Build().Perform(); }
/// <summary> /// Click an element /// </summary> /// <param name="element">Page element</param> public static void Click(this IBrowser browser, PageElement element) { browser.Find(element).Click(); }
/// <summary> /// Double click an element /// </summary> /// <param name="element">Page element</param> public static void ClickDouble(this IBrowser browser, PageElement element) { var action = new Actions(browser.IWebDriver); action.DoubleClick(browser.Find(element)).Build().Perform(); }
/// <summary> /// Click an element /// </summary> /// <param name="element">Page element</param> public static void Click(this IBrowser browser, PageElement element) { browser.Find(element).Click(); }
/// <summary> /// Hover the pointer over an element /// </summary> /// <param name="element">Page element</param> public static void Hover(this IBrowser browser, PageElement element) { var action = new Actions(browser.IWebDriver); action.MoveToElement(browser.Find(element)).Build().Perform(); }