18using System.Collections.Generic;
22using System.Threading.Tasks;
27using System.Windows.Automation;
29using System.Reflection;
43 internal class SmallSelectorNode
45 public InteractionType InteractionType {
get;
internal set; }
46 public int OffsetX {
get;
internal set; }
47 public int OffsetY {
get;
internal set; }
48 public int DeltaX {
get;
internal set; }
49 public int DeltaY {
get;
internal set; }
58 internal SelectorSettings selectorSettings =
null;
59 public SelectorSettings SelectorSettings
63 return selectorSettings;
67 selectorSettings = value;
71 public SelectorType SelectorType
75 return SelectorSettings.SelectorType;
80 selectorSettings.SelectorType = value;
85 public Selector ToGPALObject()
99 public delegate CallIfStatus
webMatchingFunction(List<IGPALElement> foundElements, out List<IGPALElement> matchedElements, out
bool matchedAll, Selector selector,
bool ExactMatch =
true);
110 public delegate CallIfStatus
appMatchingFunction(List<GPALAutomationElement> foundElements, out List<GPALAutomationElement> matchedElements, out
bool matchedAll, Selector selector,
bool ExactMatch =
true);
113 internal Selector(
Browser.Browser browser, Selector selector)
115 SelectorSettings =
new SelectorSettings();
118 selectorSettings = ObjectCopier.DeepCopyObject(selector.SelectorSettings, selectorSettings, DeepCopy.FieldsAndProperties);
119 if (
true ==
string.IsNullOrEmpty(selector.
Name))
120 Name = $
"Selector{browser.CurrentUOW.WithSelectorList.Count}";
130 Name = $
"Selector{browser.CurrentUOW.WithSelectorList.Count}";
135 SelectorSettings =
new SelectorSettings();
137 MyApplication = application;
138 selectorSettings = ObjectCopier.DeepCopyObject(selector.SelectorSettings, selectorSettings, DeepCopy.FieldsAndProperties);
139 if (
true ==
string.IsNullOrEmpty(selector.
Name))
140 Name = $
"Selector{application.CurrentUOW.WithSelectorList.Count}";
146 internal Selector(Application.Application application)
148 MyApplication = application;
149 SelectorSettings =
new SelectorSettings();
150 Name = $
"Selector{application.CurrentUOW.WithSelectorList.Count}";
154 SelectorSettings =
new SelectorSettings();
156 internal bool AnyMatchCriteria()
158 return 0 < MatchCriteria.Count;
161 #region <Fluent Settings>
171 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
183 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
195 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
207 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
219 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
231 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
243 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
259 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
277 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
286 [SingleUse(Description =
"")]
289 SelectorSettings.SearchForSelector = searchForElement;
298 [SingleUse(Description =
"")]
301 SelectorSettings.Name = name;
309 [SingleUse(Description =
"")]
312 SelectorSettings.DeltaX = deltaX;
320 [SingleUse(Description =
"")]
323 SelectorSettings.DeltaY = deltaY;
331 [SingleUse(Description =
"")]
334 SelectorSettings.OffsetX = offsetX;
342 [SingleUse(Description =
"")]
345 SelectorSettings.OffsetY = offsetY;
353 [SingleUse(Description =
"")]
356 SelectorSettings.SimulateMouse = moveWithArc;
361 [SingleUse(Description =
"")]
364 SelectorSettings.AttributeName = attributeName;
374 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Custom, AppMatchingFunction = matchingFunction };
375 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
385 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Custom, WebMatchingFunction = matchingFunction };
386 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
396 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Attribute, StringToMatch = attributeToMatch };
397 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
407 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Attribute, StringToMatch = attributeToMatch };
408 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
418 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Href, StringToMatch = hrefToMatch };
419 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
429 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Href, StringToMatch = hrefToMatch };
430 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
440 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Placeholder, StringToMatch = placeholderToMatch };
441 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
451 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Placeholder, StringToMatch = placeholderToMatch };
452 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
462 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Src, StringToMatch = srcToMatch };
463 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
473 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Src, StringToMatch = srcToMatch };
474 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
484 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Text, StringToMatch = textToMatch };
485 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
495 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Text, StringToMatch = textToMatch };
496 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
506 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Value, StringToMatch = valueToMatch };
507 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
517 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Regex | MatchType.Value, StringToMatch = valueToMatch };
518 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
535 throw new NotImplementedException();
544 SelectorSettings.StopOnNotFound = stopOnNotFound;
554 SelectorSettings.CallIfNotFound.Add(callIfNotFound);
567 foreach (
string filename
in imageToFindPath.Filenames)
570 SelectorSettings.SelectorPaths.Add(selectorPathEntry);
577 selectorSettings.HRef = HRefFullOrPartial;
580 #endregion <Fluent Settings>
582 #region <Getter/Setters>
607 SelectorSettings.RequestedInteractionType =
InteractionType.JavaScript;
611 [Obsolete(
"This method is not preferred. Consider using WithJavaScript instead.",
false)]
617 SelectorSettings.RequestedInteractionType =
InteractionType.JavaScript;
642 return SelectorSettings.ElementsFoundAndMatchedCount;
646 SelectorSettings.ElementsFoundAndMatchedCount = value;
649 internal CallIfStatus AppCallIfFoundHandled
657 SelectorSettings.AppCallIfFoundHandled = value;
660 internal CallIfStatus AppCallIfNotFoundHandled
664 return SelectorSettings.AppCallIfNotFoundHandled;
668 SelectorSettings.AppCallIfNotFoundHandled = value;
671 internal CallIfStatus CallIfFoundHandled
675 return SelectorSettings.CallIfFoundHandled;
679 SelectorSettings.CallIfFoundHandled = value;
682 internal CallIfStatus CallIfNotFoundHandled
686 return SelectorSettings.CallIfNotFoundHandled;
690 SelectorSettings.CallIfNotFoundHandled = value;
693 internal Application.Application Application
697 return MyApplication;
701 MyApplication = value;
704 internal List<Application.Application.CallIfDelegate> AppCallIfFound
708 return SelectorSettings.AppCallIfFound;
711 internal List<Application.Application.CallIfDelegate> AppCallIfNotFound
715 return SelectorSettings.AppCallIfNotFound;
718 internal string AttributeName
722 return SelectorSettings.AttributeName;
734 return SelectorSettings.InteractionType;
738 SelectorSettings.InteractionType = value;
757 return SelectorSettings.SelectorPaths[0].SelectorPath;
768 return SelectorSettings.SelectorPaths;
786 return SelectorSettings.SearchForSelector;
798 return SelectorSettings.OffsetX;
802 SelectorSettings.OffsetX = value;
814 return SelectorSettings.OffsetY;
818 SelectorSettings.OffsetY = value;
829 return SelectorSettings.DeltaX;
833 SelectorSettings.DeltaX = value;
844 return SelectorSettings.DeltaY;
848 SelectorSettings.DeltaY = value;
861 return SelectorSettings.Name;
866 SelectorSettings.Name = value;
872 internal bool DeleteMe
880 SelectorSettings.DeleteMe = value;
888 internal List<
Browser.Browser.CallIfDelegate> RemovedMethods
892 return SelectorSettings.RemovedMethods;
896 SelectorSettings.RemovedMethods = value;
909 return SelectorSettings.RequestedInteractionType;
913 SelectorSettings.RequestedInteractionType = value;
924 return SelectorSettings.SimulateMouse;
935 return SelectorSettings.StopOnNotFound;
939 public List<GPALElement> WebSelectorFoundResults
944 if (
null != selectorPathEntry.WebSelectorFoundResults)
945 return selectorPathEntry.WebSelectorFoundResults;
952 selectorPathEntry.WebSelectorFoundResults = value;
955 public List<GPALElement> WebSelectorMatchedResults
960 if (
null != selectorPathEntry.WebSelectorMatchedResults)
961 return selectorPathEntry.WebSelectorMatchedResults;
968 selectorPathEntry.WebSelectorMatchedResults = value;
972 #endregion <Getter/Setters>
974 #region Implicit String Conversion, so wherever Selector parameter is expected, a string can be used directly
976 public static implicit
operator string(Selector sel) => sel.
Name;
978 private static int implicitCount = 0;
982 public static implicit
operator Selector(
string selectorString)
984 var sel =
new Selector();
986 if (
string.IsNullOrWhiteSpace(selectorString))
989 GPAL.PublishSimpleEvent(
990 GPALEventType.WARNING,
991 "Invalid selector path treating as empty selector (no paths added).",
993 GPALObjectType.Selector
997 sel.WithSelectorName($
"EmptySelector{implicitCount++}");
1006 sel.WithXPath(selectorString);
1007 sel.WithCSS(selectorString);
1012 sel.WithCSS(selectorString);
1013 sel.WithXPath(selectorString);
1017 sel.WithSelectorName($
"Implicit{implicitCount++}.{(looksLikeXPath ? "XPath
" : "CSS
")}.{Shorten(selectorString, 10)}");
1020 GPAL.PublishSimpleEvent(GPALEventType.DEBUG, $
"Implicit selector [{sel.Name}] created for [{selectorString}]");
1023 if (1 == implicitCount)
1024 GPAL.PublishSimpleEvent(GPALEventType.DEBUG, $
"Implicit selector conversions lack equality, even if paths match. Create instance using GPAL.Selector for reuse.", sel, GPALObjectType.Selector);
1030 private static string Shorten(
string s,
int maxLen)
1032 if (s.Length <= maxLen)
return s;
1033 return s.Substring(0, maxLen - 3) +
"...";
1044 if (
string.IsNullOrWhiteSpace(s))
return false;
1048 if (s.StartsWith(
"/") || s.StartsWith(
".//") || s.StartsWith(
"//") || s.StartsWith(
"id("))
1052 if (
char.IsLetter(s[0]) && s.Contains(
"/") && s.Contains(
"["))
1056 if (s.Contains(
"::") ||
1057 s.Contains(
"/following") ||
1058 s.Contains(
"/preceding") ||
1059 s.Contains(
"/ancestor") ||
1060 s.Contains(
"/parent") ||
1061 s.Contains(
"/child") ||
1062 s.Contains(
"position()") ||
1063 s.Contains(
"last()") ||
1064 s.Contains(
"normalize-space") ||
1065 s.Contains(
"contains(text()") ||
1066 (s.Contains(
"@") && s.Contains(
"=") && !s.Contains(
"[")))
1072 if (s.Contains(
"@") && !s.Contains(
"["))
1076 if (s.StartsWith(
"#") ||
1077 s.StartsWith(
".") ||
1081 s.Contains(
":not(") ||
1082 s.Contains(
":nth-child") ||
1083 s.Contains(
":nth-of-type"))
1089 if (s.Contains(
"[") && !s.Contains(
":") && !s.Contains(
"#") && !s.Contains(
"."))
1099 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Attribute, StringToMatch = attributeToMatch };
1100 matchCriteriaEntry.ExactMatch =
false;
1101 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
1105 public IAllowSelectorSettings ContainsHRef(
string hrefToMatch)
1107 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Href, StringToMatch = hrefToMatch };
1108 matchCriteriaEntry.ExactMatch =
false;
1109 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
1113 public IAllowSelectorSettings ContainsPlaceholder(
string placeholderToMatch)
1115 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Placeholder, StringToMatch = placeholderToMatch };
1116 matchCriteriaEntry.ExactMatch =
false;
1117 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
1121 public IAllowSelectorSettings ContainsSrc(
string srcToMatch)
1123 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Src, StringToMatch = srcToMatch };
1124 matchCriteriaEntry.ExactMatch =
false;
1125 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
1129 public IAllowSelectorSettings ContainsText(
string textToMatch)
1131 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Text, StringToMatch = textToMatch };
1132 matchCriteriaEntry.ExactMatch =
false;
1133 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
1137 public IAllowSelectorSettings ContainsValue(
string valueToMatch)
1139 SelectorSettings.MatchCriteriaEntry matchCriteriaEntry =
new SelectorSettings.MatchCriteriaEntry { MatchType = MatchType.Value, StringToMatch = valueToMatch };
1140 matchCriteriaEntry.ExactMatch =
false;
1141 SelectorSettings.MatchCriteria.Add(matchCriteriaEntry);
Application object that contains the fluent methods to create your Application workflow....
Browser object that contains the fluent methods to create your Browser workflow. Instatiated using G...
GPAL File object instantied with GPAL.File Used to load tokens into a GPALGrid [rows/columns].
Provides comprehensive image capture, conversion, and matching capabilities for automation scenarios....
static Image Base64ToImage(string base64String)
Converts a Base64 string (with or without data URI prefix) to a System.Drawing.Image.
GPAL Selector used to locate Application and Browser elements. Instantiated with GPAL....
IAllowSelectorSettings WithSelectorName(string name)
A common name for this selector. Used in Information and Exception channel messages.
string SelectorPath
Returns the first defined selector path string.
int DeltaX
Drag-and-drop deltaX to move from the elemenet.X + OffsetX.
IAllowSelectorSettings MatchText(string textToMatch)
Found elements must match the text supplied in order to interact with them.
IAllowSelectorSettings WithJavaScript
Interact with this element using javascript injected into the page.
IAllowSelectorSettings MatchValueRegex(string valueToMatch)
Found elements must match the value regular expression supplied in order to interact with them.
IAllowSelectorSettings MatchTextRegex(string textToMatch)
Found elements must match the text regular expression supplied in order to interact with them.
IAllowSelectorSettings MatchAttribute(string attributeToMatch)
Found elements must have attribute supplied in order to interact with them.
Browser.Browser Browser
Browser associated with this selector.
IAllowSelectorSettings WithDeltaY(int deltaY)
Define the y offset from the Top Left corner to perform clicks.
IAllowSelectorSettings WithImage(Image image)
Define an image to match on-screen. The top left corner is used as the start of the element....
bool StopOnNotFound
Stop the workflow if no elements are found for this selector.
void Remove()
Remove this selector from the current Unit of Work.
IAllowSelectorSettings WithText(string text)
Define Text used to find elements by text attribute. Browser only.
IAllowSelectorSettings WithDeltaX(int deltaX)
Define the x offset from the Top Left corner to perform clicks.
IAllowSelectorSettings MatchPlaceholder(string placeholderToMatch)
Found elements must match the placeholder supplied in order to interact with them.
bool SearchForSelector
Search for elements on the page.
IAllowSelectorSettings MatchFunction(Selector.appMatchingFunction matchingFunction)
Your custom application element matching function to examine found elements and return only those you...
IAllowSelectorSettings MatchSrc(string srcToMatch)
Found elements must match the src supplied in order to interact with them.
int DeltaY
Drag-and-drop deltaY to move from the elemenet.Y + OffsetY.
int OffsetY
Y offset fromm the top left corner to interact with the element, image match included....
IAllowSelectorSettings MatchFunction(Selector.webMatchingFunction matchingFunction)
Your custom web element matching function to examine found elements and return only those you 'match'...
IAllowSelectorSettings WithClassName(string className)
Define Name used to find elements by name attribute. Application only.
IAllowSelectorSettings MatchAttributeRegex(string attributeToMatch)
Found elements must have the attribute specified by regular expression in order to interact with them...
int ElementsFoundAndMatchedCount
Count of elements found for this selector (after match).
InteractionType InteractionType
Defined method to interact with this element. NOTE: This can be overridden in the workflow.
IAllowSelectorSettings WithSelenium
Interact with this element using Selenium [default].
IAllowSelectorSettings WithValue(string value)
Define Value used to find elements by value attribute. Browser only.
IAllowSelectorSettings WithName(string name)
Define Name used to find elements by name attribute. Application only.
IAllowSelectorSettings WithOCR(string textToFind)
Use OCR to find the text on-screen.
IAllowSelectorSettings WithXPath(string xPath)
Define XPath used to find elements Both.
IAllowSelectorSettings WithImage(GPALFile imageToFindPath)
Define an image to match on-screen. The top left corner is used as the start of the element....
IAllowSelectorSettings MatchHRefRegex(string hrefToMatch)
Found elements must match the HRef regular expression supplied in order to interact with them.
IAllowSelectorSettings MatchValue(string valueToMatch)
Found elements must match the value supplied in order to interact with them.
delegate CallIfStatus webMatchingFunction(List< IGPALElement > foundElements, out List< IGPALElement > matchedElements, out bool matchedAll, Selector selector, bool ExactMatch=true)
Custom matching function, must return all matched elements and an indicator as to whether all element...
static bool IsLikelyXPath(string s)
Rudimentary but reasonably effective guess: is this string more likely an XPath than a CSS selector?
IAllowSelectorSettings WithSimulateMouse(bool moveWithArc)
Simulate a human moving the mouse when interacting with this element. Implies hardware input.
IAllowSelectorSettings WithAutomationID(string automationID)
Define Name used to find elements by name attribute. Application only.
IAllowSelectorSettings WithOffsetX(int offsetX)
Define the x offset from the Top Left corner to perform clicks.
delegate CallIfStatus appMatchingFunction(List< GPALAutomationElement > foundElements, out List< GPALAutomationElement > matchedElements, out bool matchedAll, Selector selector, bool ExactMatch=true)
Custom matching function, must return all matched elements and an indicator as to whether all element...
IAllowSelectorSettings MatchPlaceholderRegex(string placeholderToMatch)
Found elements must match the placeholder regular expression supplied in order to interact with them.
IAllowSelectorSettings WithImage(string base64String)
Define an image to match on-screen. The top left corner is used as the start of the element....
List< SelectorSettings.SelectorPathEntry > SelectorPaths
The selector paths defined to locate elements.
IAllowSelectorSettings MatchHRef(string hrefToMatch)
Found elements must match the HRef supplied in order to interact with them.
IAllowSelectorSettings WithCSS(string css)
Define CSS used to find elements. Browser only.
bool SimulateMouse
Simulate moving the mouse to interact with this element?
IAllowSelectorSettings WithOffsetY(int offsetY)
Define the y offset from the Top Left corner to perform clicks.
IAllowSelectorSettings WithSearchForSelector(bool searchForElement)
Search for a matching image when using hardware emulation or image matching. Finding an element on p...
IAllowSelectorSettings MatchSrcRegex(string srcToMatch)
Found elements must match the src regular expression supplied in order to interact with them.
string Name
The name you gave this selector, or one assigned by GPAL [selector1, selector2...] Used in Informati...
IAllowSelectorSettings WithStopOnNotFound(bool stopOnNotFound)
Stop the workflow if this selector is not found.
int OffsetX
X offset from the top left corner to interact with the element, image match included....
IAllowSelectorSettings WithHardware
Interact with this element using hardware emulation.
An entry defining a selector locator, a selector path.
Settings for the current selector. Use this only for debugging.