/// <summary> /// Wait for the element to have the desired value in the css style /// </summary> /// <param name="pageElement"></param> /// <param name="styleProperty"></param> /// <param name="desiredValue"></param> public void WaitForCssStyleValue(PageElement pageElement, string styleProperty, string desiredValue) { var webElement = GetElement(pageElement); Wait.UpTo(10.Seconds()).For(() => new TestResponse(desiredValue, webElement.GetCssValue(styleProperty), $"Failed to wait for the '{pageElement.Description}' element to have the expected value in the '{styleProperty}' property")); }
public void WaitForLocatedOnScreen(PageElement pageElement, TimeSpan timeout = default(TimeSpan)) { WaitForAny(pageElement, timeout == default(TimeSpan)? 30.Seconds() : timeout); var webElement = GetElement(pageElement); WaitForLocatedOnScreen(webElement, pageElement.Description); }
/// <summary> /// Clear out a textbox by using Ctrl+A, Delete. Particularly useful for custom editors/inputs /// </summary> /// <param name="pageElement"></param> public void SelectAllThenDelete(PageElement pageElement) { var webElement = GetElement(pageElement); try { try { // some page elements may not be directly clickable, and already have focus, so don't fail on this webElement.Click(); Thread.Sleep(500.Milliseconds()); } catch (Exception e) { Log($"Tried to click on '{pageElement.Description}' in order to clear it, but the click failed. Continuing.\nException details: {e.Message}"); } var action = new Actions(_webdriver) .KeyDown(Keys.Control) .SendKeys("a") .KeyUp(Keys.Control) .SendKeys(Keys.Delete); action.Build().Perform(); Thread.Sleep(500.Milliseconds()); } catch (Exception e) { throw new Exception("Tried to remove the text from '" + pageElement.Description + "' using Ctrl+A, Delete but it failed!", e); } }
public void SwitchToIFrame(PageElement pageElement) { WaitForAny(pageElement, 30.Seconds()); var webElement = GetElement(pageElement); _webdriver.SwitchTo().Frame(webElement); }
public void WaitForEnabled(PageElement pageElement, TimeSpan timeout) { WaitForAny(pageElement, timeout); var webElement = GetElement(pageElement); Wait.UpTo(timeout).For(() => new TestResponse(webElement.Enabled, $"Failed waiting for '{pageElement.Description}' to be enabled")); }
public void Uncheck(PageElement pageElement) { var webElement = GetElement(pageElement); if (webElement.Selected) { webElement.Click(); } }
public void Check(PageElement pageElement) { WaitForAny(pageElement, 30.Seconds()); var element = GetElement(pageElement); if (!element.Selected) { element.Click(); } }
public void WaitForAngularRequestsToStop(PageElement pageElement) { if (pageElement.Css == null) { throw new Exception("Unable to wait for angular on '" + pageElement.Description + "' since it doesn't have a css selector value"); } Wait.UpTo(30.Seconds()).For(() => new TestResponse(DoesElementHaveNoOutstandingAngularRequests(_webdriver, pageElement.Css), "Tried to wait for the element: " + pageElement.Description + "' to have no more angular requests but it was still busy. Consider increasing the timeout or checking network speed.")); }
public bool Exists(PageElement pageElement) { try { var elements = GetElements(pageElement); return(elements.Count > 0); } catch { return(false); } }
/// <summary> /// Wait for the element to have one of the desired values in the css style /// </summary> /// <param name="pageElement"></param> /// <param name="styleProperty"></param> /// <param name="desiredValues"></param> public void WaitForACssStyleValue(PageElement pageElement, string styleProperty, params string[] desiredValues) { var webElement = GetElement(pageElement); Wait.UpTo(10.Seconds()).For(() => { var cssValue = webElement.GetCssValue(styleProperty); return(new TestResponse(desiredValues.Any(finalValue => finalValue == cssValue), $"Failed to wait for the '{pageElement.Description}' element to have one of the expected values ({string.Join(", ", desiredValues)}) in the '{styleProperty}' property, but it was: {cssValue}")); }); }
public void WaitForCssStyleValuesNotPresent(PageElement pageElement, string styleProperty, params string[] nonDesiredValues) { var webElement = GetElement(pageElement); Wait.UpTo(10.Seconds()).For(() => { var cssValue = webElement.GetCssValue(styleProperty); return(new TestResponse(!nonDesiredValues.Contains(cssValue), $"Waiting for '{pageElement.Description}' to not have any of these values '{string.Join(", ", nonDesiredValues)}' in the style attribute: {styleProperty}.\nBut it had the value: {cssValue}")); }); }
public string GetAttributeValue(PageElement pageElement, string attributeName) { var webElement = GetElement(pageElement); try { return(webElement.GetAttribute(attributeName)); } catch (Exception e) { throw new Exception("Failed to get the '" + attributeName + "' attribute value of the element: " + pageElement.Description, e); } }
public bool IsVisible(PageElement pageElement) { try { var webElement = GetElement(pageElement); ScrollIntoView(webElement); return(webElement.Displayed); } catch { return(false); } }
public void RightClick(PageElement pageElement, bool scrollIntoViewBeforeClick = true) { // Adding a micro sleep to provide a little more robustness before trying to execute a click Thread.Sleep(200); var webElement = GetElement(pageElement); if (scrollIntoViewBeforeClick) { ScrollIntoView(webElement); } RightClick(webElement, pageElement.Description); }
public void FileUpload(PageElement pageElement, string filePath) { var webElement = GetElement(pageElement); try { ScrollIntoView(webElement); webElement.SendKeys(filePath); } catch (Exception e) { throw new Exception("Tried to upload a file from '" + filePath + "' into element: " + pageElement.Description + "\nBut I got an error", e); } }
public string GetText(PageElement pageElement) { var webElement = GetElement(pageElement); try { return(webElement.Text); } catch (Exception e) { throw new Exception("Tried to get the text for the element '" + pageElement.Description + "' but got an error.", e); } }
public bool IsEnabled(PageElement pageElement) { var webElement = GetElement(pageElement); try { ScrollIntoView(webElement); return(webElement.Enabled); } catch { return(false); } }
/// <summary> /// Send the provided keys to the given element /// </summary> /// <param name="pageElement"></param> /// <param name="text"></param> public void SendKeys(PageElement pageElement, string text) { var webElement = GetElement(pageElement); try { webElement.SendKeys(text); } catch (Exception e) { throw new Exception( "Tried to submit text '" + text + "' to element '" + pageElement.Description + "' but got an error", e); } }
public void PressTab(PageElement pageElement) { var webElement = GetElement(pageElement); try { ScrollIntoView(webElement); webElement.SendKeys(Keys.Tab); } catch (Exception e) { throw new Exception( "Tried to send 'TAB' to element: " + pageElement.Description + ".\nBut got an error", e); } }
public bool IsSelected(PageElement pageElement) { var webElement = GetElement(pageElement); try { return(webElement.Selected); } catch (Exception e) { throw new Exception( "Tried to determine if the '" + pageElement.Description + "' element was selected, but go an error.", e); } }
/// <summary> /// Wait for any instance of this element to be found /// </summary> /// <param name="element"></param> /// <param name="timeout"></param> /// <returns></returns> public void WaitForAny(PageElement element, TimeSpan timeout) { var searchContext = GetElementFinder(element, out _); try { var wait = new WebDriverWait(_webdriver, timeout); wait.Until(d => d.FindElements(searchContext).Any()); } catch (Exception e) { throw new Exception("Waiting for element: " + element.Description + " for " + timeout.TotalSeconds + " seconds, but got an error:\n" + e.Message + ",\nStack Trace: \n" + e.StackTrace); } }
private ReadOnlyCollection <IWebElement> GetElements(PageElement element) { var selectorTried = ""; var searchContext = GetElementFinder(element, out selectorTried); try { return(_webdriver.FindElements(searchContext)); } catch (Exception e) { throw new Exception("Failed to find elements '" + element.Description + "'.\nSelector used: " + selectorTried + "\nException thrown: " + e.Message); } }
/// <summary> /// Wait for the element to be visible, but don't throw exception if it doesn't load /// in that time /// </summary> /// <param name="pageElement"></param> /// <param name="timeout"></param> public void SilentWaitForVisible(PageElement pageElement, TimeSpan timeout) { var stopWatch = new Stopwatch(); stopWatch.Start(); while (stopWatch.Elapsed < timeout) { if (IsVisible(pageElement)) { stopWatch.Stop(); return; } Thread.Sleep(1.Seconds()); } stopWatch.Stop(); }
public void WaitForText(PageElement pageElement, string text, TimeSpan timeout) { WaitForExists(pageElement, timeout); var webElement = GetElement(pageElement); try { new WebDriverWait(_webdriver, timeout).Until(d => webElement.Text.ToLower().Contains(text.ToLower())); } catch (Exception e) { throw new Exception( $"Tried to wait for the element '{pageElement.Description}' to have the text '{text}'.\nCurrent text: {webElement.Text}", e); } }
public void Clear(PageElement pageElement) { var element = GetElement(pageElement); try { element.Clear(); Thread.Sleep(500); // forcing the sleep here as the clear action is slightly delayed and caused problems in the next actions like sendkeys } catch (Exception e) { throw new Exception("Tried to clear: " + pageElement.Description + ",\n\nbut got an error: " + e.Message + ",\n\nStack Trace: \n" + e.StackTrace); } }
public void EnterText(PageElement pageElement, string text) { var webElement = GetElement(pageElement); try { webElement.Clear(); // short delay to let the clear event take affect Thread.Sleep(500.Milliseconds()); webElement.SendKeys(text); } catch (Exception e) { throw new Exception("Tried to enter the text '" + text + "' into element '" + pageElement.Description + "' but failed", e); } }
public void SwitchToIFrame(int iFrameIndex) { var desiredCount = iFrameIndex + 1; var iFrameElement = new PageElement("Iframe") { Css = "iframe" }; Wait.UpTo(10.Seconds()).For(() => { var iFrameCount = GetElements(iFrameElement).Count; return(new TestResponse(iFrameCount >= desiredCount, "Failed to wait for enough iFrames, current count '" + iFrameCount + "', required: " + desiredCount)); }); _webdriver.SwitchTo().Frame(iFrameIndex); }
public List <string> GetSelectOptions(PageElement pageElement) { WaitForAny(pageElement, 30.Seconds()); var webElement = GetElement(pageElement); try { var selectElement = new SelectElement(webElement); return(selectElement.Options.Select(option => option.Text).ToList()); } catch (Exception e) { throw new Exception( "Tried to get the list of options for the select element '" + pageElement.Description + "' but got an error.", e); } }
public List <string> GetSelectedOptions(PageElement pageElement) { var webElement = GetElement(pageElement); try { var selectElement = new SelectElement(webElement); var options = selectElement.AllSelectedOptions; return(options.Select(option => option.Text).ToList()); } catch (Exception e) { throw new Exception( "Tried to get the currently selected option(s) from '" + pageElement.Description + "' but there was an error", e); } }
/// <summary> /// Select option <paramref name="optionToSelect"/> from the drop down element <paramref name="pageElement"/> /// </summary> public void Select(PageElement pageElement, string optionToSelect, bool partialMatch = false) { WaitForAny(pageElement, 30.Seconds()); var webElement = GetElement(pageElement); var selectElement = new SelectElement(webElement); Wait.UpTo(10.Seconds()).For(() => new TestResponse(selectElement.Options.Count > 0, $"Expected the Select element '{pageElement.Description}' to have at least 1 option, so i could select '{optionToSelect}' but it had none available")); try { selectElement.SelectByText(optionToSelect, partialMatch); } catch (Exception e) { throw new Exception("Tried to select the option '" + optionToSelect + "' from the select element '" + pageElement.Description + "'.\nBut it failed, current options: " + string.Join(", ", selectElement.Options.Select(option => option.Text)), e); } }