/// <summary>Applies Find Logic to DOM and locates an Element. If find logic does not locate an element or locates more than one returns false and sets /// <see cref="TryException"/> to exception that would be thrown /// </summary> /// <param name="ParentName">Name of parent this Find is from - null for DOM Root level</param> /// <param name="RootElement">Element to search from. If null, search from DOM level</param> /// <param name="FindLogic">Find logic to be applied to DOM</param> /// <param name="FriendlyName">Human readable name of element being located</param> /// <param name="element">Populated with element reference if found. Null if not found (or more than one element found).</param> /// <returns>True if element found or False if not found (or more than one element found)</returns> public bool TryFindElement(Element ParentElement, ObjectMappingDetails Mapping, bool AllowMultipleMatches, TimeSpan?TimeoutOverride, TimeSpan?PollOverride, bool WaitUntilStable, out Element element) { WebDriverWait webDriverWait = GetPollAndTimeout(elementFindTimings, TimeoutOverride, PollOverride); List <Element> clauseResults = new List <Element>(); Stopwatch timer = Stopwatch.StartNew(); Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Timeout - {0} Seconds", webDriverWait.Timeout.TotalSeconds); while (clauseResults.Count == 0) { clauseResults = (ParentElement == null) ? FindElements(Mapping) : FindElements(ParentElement, Mapping); if (clauseResults.Count == 0) { Thread.Sleep(webDriverWait.PollingInterval); } if ((timer.Elapsed >= webDriverWait.Timeout)) { break; } } timer.Stop(); if (clauseResults.Count > 1 && !AllowMultipleMatches) { Logger.WriteLine(Logger.LogLevels.FrameworkInformation, "Found {0} matching elements in {1} mS. Do not allow multiple matches.", clauseResults.Count.ToString(), timer.ElapsedMilliseconds.ToString()); TryException = new FindLogicReturnedMultipleElements(((ParentElement == null) ? "DOM Top Level" : (string.IsNullOrEmpty(ParentElement.MappingDetails.FriendlyName) ? ("Unknown Parent (" + ParentElement.MappingDetails.FindLogic + ")") : ParentElement.MappingDetails.FriendlyName)), Mapping, clauseResults.Count); element = null; return(false); } if (clauseResults.Count == 1 || (clauseResults.Count > 1 && AllowMultipleMatches)) { Logger.WriteLine(Logger.LogLevels.FrameworkInformation, "Found {0} matching elements in {1} mS.{2}", clauseResults.Count.ToString(), timer.ElapsedMilliseconds.ToString(), AllowMultipleMatches ? " Allow multiple matches so using first match." : string.Empty); if (WaitUntilStable && !clauseResults[0].IsPositionStable) { if (clauseResults[0].WaitForElementPositionStable()) { if (TryFindElement(ParentElement, Mapping, AllowMultipleMatches, TimeoutOverride, PollOverride, false, out element)) { return(true); } else { TryException = new Exception("Could not find element after waiting for position stable!", TryException); element = null; return(false); } } } element = clauseResults[0]; return(true); } else { Logger.WriteLine(Logger.LogLevels.FrameworkInformation, "Found no matching elements in {0} mS", timer.ElapsedMilliseconds.ToString()); TryException = new FindLogicReturnedNoElements(ParentElement?.MappingDetails?.FriendlyName ?? "No parent (searched from top level of DOM)", Mapping, webDriverWait.Timeout.TotalSeconds.ToString(), webDriverWait.PollingInterval.TotalMilliseconds.ToString()); element = null; return(false); } }
public Element FindElement(Element ParentElement, ObjectMappingDetails Mapping, TimeSpan Timeout, bool WaitUntilStable) { Element Element; if (!TryFindElement(ParentElement, Mapping, false, Timeout, null, WaitUntilStable, out Element)) { throw TryException; } return(Element); }
public Element FindElement(bool WaitUntilStable, ObjectMappingDetails Mapping) { Element Element; if (!TryFindElement(null, Mapping, false, null, null, WaitUntilStable, out Element)) { throw TryException; } return(Element); }
/// <summary>Applies Find Logic to DOM and locates an Element. Throws exception if find logic does not locate an element or locates more than one. /// </summary> /// <param name="FindLogic">Find logic to be applied to DOM</param> /// <param name="FriendlyName">Human readable name of element being located</param> /// <returns>Reference to element found</returns> public Element FindElement(ObjectMappingDetails Mapping) { Element Element; if (!TryFindElement(null, Mapping, false, null, null, true, out Element)) { throw TryException; } return(Element); }
/// <summary>Applies Find Logic to DOM and locates an Element. Throws exception if find logic does not locate an element or locates more than one. /// </summary> /// <param name="FindLogic">Find logic to be applied to DOM</param> /// <param name="FriendlyName">Human readable name of element being located</param> /// <param name="AllowMultipleMatches">Indicates if multiple matches are allowed. If true the first match will be used.</param> /// <param name="Timeout">Overrides Find timeout (default uses set Element Find timout)</param> /// <returns>Reference to element found</returns> public Element FindElement(ObjectMappingDetails Mapping, bool AllowMultipleMatches, TimeSpan Timeout) { Element Element; if (!TryFindElement(null, Mapping, AllowMultipleMatches, Timeout, null, true, out Element)) { throw TryException; } return(Element); }
public Element FindElement(ObjectMappingDetails Mapping, TimeSpan Timeout, TimeSpan PollInterval, bool WaitUntilStable) { Element Element; if (!TryFindElement(null, Mapping, false, Timeout, PollInterval, WaitUntilStable, out Element)) { throw TryException; } return(Element); }
/// <summary>Applies Find Logic to DOM and locates an Element. Throws exception if find logic does not locate an element or locates more than one. /// </summary> /// <param name="ParentName">Human readable name of Root Element</param> /// <param name="RootElement">Element to search from. If null, search from DOM level</param> /// <param name="FindLogic">Find logic to be applied to DOM</param> /// <param name="FriendlyName">Human readable name of element being located</param> /// <param name="Timeout">Overrides Find timeout (default uses set Element Find timout)</param> /// <param name="PollInterval">Overrides Find poll interval (default uses set Element Find polling interval)</param> /// <returns>Reference to element found</returns> public Element FindElement(Element ParentElement, ObjectMappingDetails Mapping, TimeSpan Timeout, TimeSpan PollInterval) { Element Element; if (!TryFindElement(ParentElement, Mapping, false, Timeout, PollInterval, true, out Element)) { throw TryException; } return(Element); }
/// <summary>Applies Find Logic to DOM and locates an Element. Throws exception if find logic does not locate an element or locates more than one. /// </summary> /// <param name="ParentName">Human readable name of Root Element</param> /// <param name="RootElement">Element to search from. If null, search from DOM level</param> /// <param name="FindLogic">Find logic to be applied to DOM</param> /// <param name="FriendlyName">Human readable name of element being located</param> /// <param name="AllowMultipleMatches">Indicates if multiple matches are allowed. If true the first match will be used.</param> /// <returns>Reference to element found</returns> public Element FindElement(Element ParentElement, ObjectMappingDetails Mapping, bool AllowMultipleMatches) { Element Element; if (!TryFindElement(ParentElement, Mapping, AllowMultipleMatches, null, null, true, out Element)) { throw TryException; } return(Element); }
public Element FindElement(Element ParentElement, ObjectMappingDetails Mapping, bool AllowMultipleMatches, TimeSpan Timeout, TimeSpan PollInterval, bool WaitUntilStable) { Element Element; if (!TryFindElement(ParentElement, Mapping, AllowMultipleMatches, Timeout, PollInterval, WaitUntilStable, out Element)) { throw TryException; } return(Element); }
public Element FindElement(ObjectMappingDetails Mapping, bool WaitUntilStable, bool AllowMultipleMatches) { Element Element; if (!TryFindElement(null, Mapping, AllowMultipleMatches, null, null, WaitUntilStable, out Element)) { throw TryException; } return(Element); }
public Element FindElement(ObjectMappingDetails MappingDetails, bool FirstMatching, bool WaitUntilStable) { // We can only do this if this element has a Parent (so we can get a reference to the Selenium Driver) and it is bound to an IWebElement so that we can search from it.... if (!HasAParent) { throw new Exception("Cannot find WebElements without having a parent (SeleniumDriver or Element)!"); } if (!IsBoundToAWebElement) { throw new Exception("Cannot find child WebElements without being bound to a Selenium WebElement to search from"); } return(seleniumDriver.FindElement(this, MappingDetails, FirstMatching, WaitUntilStable)); }
/// <summary>Locates an element using find logic applied to the current element returning /// a child Element. /// </summary> /// <param name="ByFind">Find logic to locate element</param> /// <param name="FriendlyName">Meaningful name of element that can be used in errors, results and automated documentation (if used)</param> /// <remarks> /// When searching for matching elements, the visibility of elements is ignored. This could, therfore, return a hidden element</remarks> /// <returns>Reference to child Element</returns> /// <exception cref="FindLogicReturnedMultipleElements">Found multiple elements</exception> /// <exception cref="FindLogicReturnedNoElements">Found no elements</exception> public Element FindElement(ObjectMappingDetails Mapping, TimeSpan Timeout, TimeSpan PollInterval) { // We can only do this if this element has a Parent (so we can get a reference to the Selenium Driver) and it is bound to an IWebElement so that we can search from it.... if (!HasAParent) { throw new Exception("Cannot find WebElements without having a parent (SeleniumDriver or Element)!"); } if (!IsBoundToAWebElement) { throw new Exception("Cannot find child WebElements without being bound to a Selenium WebElement to search from"); } return(seleniumDriver.FindElement(this, Mapping, Timeout, PollInterval, true)); }
/// <summary>Sends a click event to an element within the control if that element is exists. Find logic must work from the top-element of the control. /// Does not throw an error if the element does not exist or is not visible. If element does not exist, nothing is done and no error thrown. /// </summary> /// <param name="findLogic">Find logic to element</param> /// <param name="friendlyName">Name of element</param> public void ClickElementIfExists(ObjectMappingDetails findLogic) { Element element; if (RootElement.TryFindElement(findLogic, out element)) { element.Click(); } else { Logger.WriteLine(Logger.LogLevels.TestInformation, $"[{findLogic.FindLogic}] didnt find any match. No click."); } }
/// <summary>Gets the attribute from an element within the control. If element does not exist a NotFound exception is thrown. If attribute does not exist an empty string is returned /// </summary> /// <param name="findLogic">Find logic reletive to the root element of the control</param> /// <param name="friendlyName">Friendly name of the element the text is coming from</param> /// <param name="attributeName">Attribute to get contents of</param> /// <returns>Contenst of attribute</returns> public string GetAttribute(ObjectMappingDetails findLogic, string attributeName) { Element element = FindElement(findLogic); try { return(element.GetAttribute(attributeName)); } catch { return(string.Empty); } }
public bool TryFindElement(ObjectMappingDetails MappingDetails, out Element foundElement) { try { foundElement = FindElement(MappingDetails); return(true); } catch (Exception ex) { TryException = ex; foundElement = null; return(false); } }
public bool TryFindElement(ObjectMappingDetails Mapping, TimeSpan Timeout, TimeSpan PollInterval, bool WaitUntilStable, out Element foundElement) { try { foundElement = FindElement(Mapping, Timeout, PollInterval, WaitUntilStable); return(true); } catch (Exception ex) { TryException = ex; foundElement = null; return(false); } }
/// <summary>Returns true if element has defined attribute and false if not. If element does not exist an exception is thrown /// </summary> /// <param name="findLogic">Find logic of the element, reletive to the root element of the control</param> /// <param name="friendlyName">Friendly name of the element to be checked</param> /// <param name="attributeName">Attribute to see if exists</param> /// <returns>true if attributeName exists, false if not</returns> public bool HasAttribute(ObjectMappingDetails findLogic, string friendlyName, string attributeName) { Element element = FindElement(findLogic); try { element.FindElement(new ObjectMappingDetails(string.Format(".[@{0}]", $"Attribute [{attributeName}] of {findLogic.FriendlyName ?? findLogic.FindLogic}"))); return(true); } catch { return(false); } }
public bool TryFindElement(ObjectMappingDetails MappingDetails, bool FirstMatching, bool WaitUntilStable, out Element foundElement) { try { foundElement = FindElement(MappingDetails, FirstMatching, WaitUntilStable); return(true); } catch (Exception ex) { TryException = ex; foundElement = null; return(false); } }
internal List <Element> FindElements(Element ParentElement, ObjectMappingDetails Mapping) { int index = 0; List <IWebElement> FoundElements; List <Element> returnList = new List <Element>(); if (ParentElement == null) { return(FindElements(Mapping)); } else { By SeleniumFindBy = Mapping.SeleniumBy; Logger.Write(Logger.LogLevels.FrameworkDebug, "Find Elements: Name - [{0}], Selenium logic - [{1}]", Mapping.FriendlyName, SeleniumFindBy.ToString()); try { FoundElements = ParentElement.WebElement.FindElements(SeleniumFindBy).ToList(); } catch (Exception ex) { throw new SeleniumFindElementError(Mapping, ParentElement, ex); } Logger.WriteLine(Logger.LogLevels.FrameworkDebug, " (Found [{0}] elements)", FoundElements.Count.ToString()); // // Our find logic has returned a bunch of elements. We _may_ want to use the find logic of a specific element again. So, when saving the elements add // an indexer to the ActualFindLogic..... // FoundElements.ForEach(iWebElement => { ObjectMappingDetails thisMapping = Mapping.Copy(); thisMapping.FriendlyName = string.Format("{0}[{1}]", thisMapping.FriendlyName, index.ToString()); if (thisMapping.FindType == ObjectMappingDetails.ByType.XPath) { thisMapping.FindLogicUsed = string.Format("({0})[{1}]", thisMapping.FindLogic, index.ToString()); } Element Element = new Element(ParentElement, iWebElement, thisMapping); returnList.Add(Element); index++; }); return(returnList); } }
public List <Element> FindElements(ObjectMappingDetails Mapping) { int index = 0; List <IWebElement> FoundElements; List <Element> returnList = new List <Element>(); // // Get the Selenium By and perform the Selenium FindElements from the top level to get a list of matching elements // By SeleniumFindBy = Mapping.SeleniumBy; Logger.Write(Logger.LogLevels.FrameworkInformation, "Find Elements: {0}", SeleniumFindBy.ToString()); try { FoundElements = webDriver.FindElements(SeleniumFindBy).ToList(); } catch (Exception ex) { throw new SeleniumFindElementError(Mapping, this, ex); } Logger.WriteLine(Logger.LogLevels.FrameworkInformation, " (Found [{0}] elements)", FoundElements.Count.ToString()); // // Now go through the list and bind each iWebElement to the framework element. // FoundElements.ForEach(iWebElement => { ObjectMappingDetails thisMapping = Mapping; if (thisMapping.FindType == ObjectMappingDetails.ByType.XPath) { thisMapping.FindLogicUsed = string.Format("({0})[{1}]", thisMapping.FindLogic, index.ToString()); } Element Element = new Element(this, iWebElement, thisMapping); returnList.Add(Element); index++; }); return(returnList); }
protected void SetRootElement(ObjectMappingDetails mappingDetails) { RootElement = new Element(mappingDetails); }
private static Element FindControlRootElement(ControlFindElement FindMechanism, ObjectMappingDetails FindLogic) { Element returnValue; returnValue = FindMechanism(FindLogic); if (returnValue == null) { throw new Exception("Unexected error! Find element returned null element. See Debug log."); } return(returnValue); }
public Element FindElement(ObjectMappingDetails findLogic) { return(RootElement.FindElement(findLogic)); }
static private string FormatMessage(ObjectMappingDetails mappingDetails, string Text) { return(string.Format("Setting or Getting text on element [{0}] ([{1}]) (Text: [{2}]) caused an exception from Selenium! See inner exception", mappingDetails?.FriendlyName ?? mappingDetails?.FindLogic ?? "", mappingDetails?.FindLogicUsed ?? "No find logic!", Text)); }
/// <summary>Set the text from an element within the control /// </summary> /// <param name="findLogic">Find logic reletive to the root element of the control</param> /// <param name="friendlyName">Friendly name of the element the text is coming from</param> /// <param name="text">text to set</param> public virtual void SetText(ObjectMappingDetails mapping, string friendlyName, string text) { FindElement(mapping).SetText(text); }
/// <summary>Gets the text from an element within the control. If element does not exist, exception is thrown. /// </summary> /// <param name="findLogic">Find logic reletive to the root element of the control</param> /// <param name="friendlyName">Friendly name of the element the text is coming from</param> /// <returns>text in identified element (uses Element.GetText)</returns> public string GetText(ObjectMappingDetails findLogic, string friendlyName) { Element element = FindElement(findLogic); return(element.GetText()); }
/// <summaryTrying to set/get text caused Selenium to throw an exception</summary> /// <param name="ElementName">Element text was being entered into</param> /// <param name="Text">Text being entered</param> /// <param name="ex">Inner Exception</param> public UnableToSetOrGetText(ObjectMappingDetails mappingDetails, string Text, Exception ex) : base(UnableToSetOrGetText.FormatMessage(mappingDetails, Text), ex) { }
static private string LocalFormatMessage(ObjectMappingDetails Mapping, dynamic Parent) { string Message = string.Format("Selenium threw an error finding element [{0}] with find logic [{1}]. Parent element [{2}]. See inner exception for details.", Mapping.FriendlyName, Mapping.FindLogic, ((Parent == null) ? "DOM Top Level" : (string.IsNullOrEmpty(Parent.MappingDetails.FriendlyName) ? ("Unknown Parent (" + Parent.MappingDetails.FindLogic + ")") : Parent.MappingDetails.FriendlyName))); return(Message); }
/// <summary>Sends a click event to an element within the control. Find logic must work from the top-element of the control. /// </summary> /// <param name="findLogic">Find logic for element to be clicked</param> /// <param name="friendlyName">Name of element to clicked</param> public void ClickElement(ObjectMappingDetails findLogic) { FindElement(findLogic).Click(); }
/// <summary>Sends a clear event to an element within the control. Find logic must work from the top-element of the control. /// </summary> /// <param name="findLogic">Find logic to element that is to be cleared</param> /// <param name="friendlyName">Name of element</param> public void ClearElement(ObjectMappingDetails findLogic, string friendlyName) { FindElement(findLogic).Clear(); }