ClassFind
Class provides quick search routines for finding an element(s) within a document.
Definition
Namespace:ArtOfTest.WebAii.Core
Assembly:ArtOfTest.WebAii.dll
Syntax:
public class Find
Inheritance: objectFind
Derived Classes:
Properties
AssociatedBrowser
Gets the browser object that is associated with this find object. If this property is null, then the Find object is associated with a search region.
Declaration
public Browser AssociatedBrowser { get; }
Property Value
Remarks
Either Find.SearchRegion or Find.AssociatedBrowser is null. Both can't be null and both can't be set.
FindReferenceType
Gets the reference type that this Find object is using. (i.e. browser root, testregion ... etc)
Declaration
public FindReferenceType FindReferenceType { get; }
Property Value
IgnoreFindAllControlMismatch
Gets/Sets whether to ignore Find.Allxx<TControl>(...) elements that don't match the TControl specified. (i.e. the TControl type validation fails for that element). If false, the Find.Allxx method will throw an exception.
Declaration
public bool IgnoreFindAllControlMismatch { get; set; }
Property Value
LastFindParam
Gets the last FindParam used to search.
LastSearchLog
Gets the search log for the last find attempt. This log can help diagnose when find fails to locate an element.
SearchRegion
Gets the search region for this Find object. When this is set to a specific region, all search is done locale to that region. All indexes are calculated starting at zero from this region's element. If this property is null, then the Find object will perform all searches from the document root element.
SearchRootElement
Gets/Sets the root element that all searches will be performed under. This element depended on the reference type. [Browser/TestRegion/Element].
ThrowIfNullOrEmpty
Gets/Sets whether to throw an exception if any of the find methods returns a null element or any of the find all methods return empty list of elements.
Methods
AllByAttributes(params string[])
Returns a list of elements that have attributes that match the NameValuePairs passed it fully or partially.
Declaration
public ReadOnlyCollection<Element> AllByAttributes(params string[] nameValuePairs)
Parameters
nameValuePairs
string[]
A list of name value pairs to match. For example:
"bar=foo","class=myclass","src=foo.gif","src=!bar". Use = for partial values or =! to exclude values.
Returns
The list of found elements.
AllByAttributes<TControl>(params string[])
Returns a list of elements that have attributes that match the NameValuePairs passed it fully or partially.
Declaration
public ReadOnlyCollection<TControl> AllByAttributes<TControl>(params string[] nameValuePairs) where TControl : Control, new()
Parameters
nameValuePairs
string[]
A list of name value pairs to match. For example:
AllByAttributes("bar=foo","class=myclass","src=foo.gif"). Use = for partial values.
Returns
ReadOnlyCollection<TControl>
The list of found elements.
Remarks
If
AllByContent(string)
Returns a list of elements that have TextContent that match the specified content The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public ReadOnlyCollection<Element> AllByContent(string textContent)
Parameters
textContent
The TextContent to match
Returns
The list of found elements.
AllByContent(string, FindContentType)
Returns a list of elements that match the specified content. The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public ReadOnlyCollection<Element> AllByContent(string content, FindContentType contentType)
Parameters
content
The content to match
contentType
The content type to match.
Returns
The list of found elements.
AllByContent<TControl>(string)
Returns a list of elements that have TextContent that match the specified content The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public ReadOnlyCollection<TControl> AllByContent<TControl>(string textContent) where TControl : Control, new()
Parameters
textContent
The TextContent to match
Returns
ReadOnlyCollection<TControl>
The list of found controls (htmlControl for example).
AllByContent<TControl>(string, FindContentType)
Returns a list of elements that match the specified content. The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public ReadOnlyCollection<TControl> AllByContent<TControl>(string content, FindContentType contentType) where TControl : Control, new()
Parameters
content
The content to match
contentType
The content type to match.
Returns
ReadOnlyCollection<TControl>
The list of found controls (htmlControl for example).
AllByCssSelector(string)
Find all by css selector.
Declaration
public ReadOnlyCollection<Element> AllByCssSelector(string selector)
Parameters
selector
The css selector.
Returns
AllByCssSelector<TControl>(string)
Find all by css selector.
Declaration
public ReadOnlyCollection<TControl> AllByCssSelector<TControl>(string selector) where TControl : Control, new()
Parameters
selector
The css selector.
Returns
ReadOnlyCollection<TControl>
AllByCustom(Predicate<Element>)
Return all elements that match the predicate
Declaration
public ReadOnlyCollection<Element> AllByCustom(Predicate<Element> predicate)
Parameters
predicate
The custom predicate that defines the element search
Returns
The list of elements that match the predicate, else an empty collection or an exception (depending on ThrowIfNullOrEmpty)
AllByCustom<TControl>(Predicate<TControl>)
Returns all controls that match the predicate and are convertable to TControl.
Declaration
public ReadOnlyCollection<TControl> AllByCustom<TControl>(Predicate<TControl> predicate) where TControl : Control, new()
Parameters
predicate
Predicate<TControl>
The predicate to use during the search.
Returns
ReadOnlyCollection<TControl>
A read only collection of elements matching the predicate and control type.
AllByExpression(HtmlFindExpression)
Find all elements by HtmlFindExpression.
Declaration
public ReadOnlyCollection<Element> AllByExpression(HtmlFindExpression expression)
Parameters
expression
The HtmlFindExpression to use during the find.
Returns
A ReadOnlyCollection<T> of Element
AllByExpression(params string[])
Find all elements by HTML find expression strings.
Declaration
public ReadOnlyCollection<Element> AllByExpression(params string[] expression)
Parameters
expression
string[]
Array of HTML find expression strings.
Returns
A ReadOnlyCollection<T> of elements.
AllByExpression<TControl>(HtmlFindExpression)
Find all elements by HtmlFindExpression of type TControl.
Declaration
public ReadOnlyCollection<TControl> AllByExpression<TControl>(HtmlFindExpression expression) where TControl : Control, new()
Parameters
expression
The HtmlFindExpression to use during the find.
Returns
ReadOnlyCollection<TControl>
A ReadOnlyCollection<T> of TControl elements.
AllByExpression<TControl>(params string[])
Find all elements by HTML find expression strings of type TControl.
Declaration
public ReadOnlyCollection<TControl> AllByExpression<TControl>(params string[] expression) where TControl : Control, new()
Parameters
expression
string[]
Array of HTML find expression strings.
Returns
ReadOnlyCollection<TControl>
A ReadOnlyCollection<T> of TControl elements.
AllByImage(Image, double, bool, Point, int, float, int)
Return all elements that look like specific image.
Declaration
public ReadOnlyCollection<Element> AllByImage(Image image, double score, bool autoScroll = true, Point probeOffset = default, int timeout = 2147483647, float imageScale = 1, int searchDelay = 100)
Parameters
image
The image to search for.
score
The minimum matching score. Value must be between 0 and 100.
autoScroll
If set to true will scroll the page under test. If set to false will search only the visible area.
probeOffset
Probing offset. Offset from center of the image rectagle. If the element you are looking for is not in the center of provided image use this argument to specify where its center is (related to image center).
timeout
Timeout in milliseconds.
imageScale
The original screen scaling applied when the image was taken.
searchDelay
Number of ms to wait before searching for element image.
Returns
All elements that match provided image. If an element is part of iframe will return iframe element.
AllByTagName(string)
Returns a list of elements with a specific tag name.
Declaration
public ReadOnlyCollection<Element> AllByTagName(string tagName)
Parameters
tagName
The tag name to search for.
Returns
The list of found elements.
AllByTagName<TControl>(string)
Returns a list of elements with a specific tag name as a control collection.
Declaration
public ReadOnlyCollection<TControl> AllByTagName<TControl>(string tagName) where TControl : Control, new()
Parameters
tagName
The tag name.
Returns
ReadOnlyCollection<TControl>
The list of found controls.
AllByXPath(string)
Returns a list of elements that match the specified XPath. This function supports the System.Xml XPath syntax.
Declaration
public ReadOnlyCollection<Element> AllByXPath(string xpath)
Parameters
xpath
The XPath string. XPath is case-sensitive.
Returns
The list of found elements.
AllByXPath<TControl>(string)
Returns a list of elements that match the specified XPath. This function supports the System.Xml XPath syntax.
Declaration
public ReadOnlyCollection<TControl> AllByXPath<TControl>(string xpath) where TControl : Control, new()
Parameters
xpath
The XPath string. XPath is case-sensitive.
Returns
ReadOnlyCollection<TControl>
The list of found elements.
AllControls<TControl>()
Gets an IEnmerable for TControl to be used for LINQ queries. This will return only elements that are convertable to TControl
Declaration
public IEnumerable<TControl> AllControls<TControl>() where TControl : Control, new()
Returns
IEnumerable<TControl>
IEnumberable
AllElements()
Gets a IEnumerable for all elements to be used for LINQ queries
Declaration
public IEnumerable<Element> AllElements()
Returns
IEnumberable
BuildBestFitFindExpression(Element)
Generates a HtmlFindExpression to be used to find the specified element. The function uses an algorithm optimized for most reliable search methods and will revert back to using the tagname index as a last resort
Declaration
public static HtmlFindExpression BuildBestFitFindExpression(Element element)
Parameters
element
The element to generate the find expression for.
Returns
The find expression to use to find the element. Can contain nested find expressions.
ByAttributes(params string[])
Returns a an element that has attributes that match the NameValuePairs passed it. All matching is case-insensitive.
Declaration
public Element ByAttributes(params string[] nameValuePairs)
Parameters
nameValuePairs
string[]
A list of name value pairs to match. For example:
"bar=foo","class=myclass","src=foo.gif","src=!bar". Use = for partial values or =! to exclude values.
Returns
The found element. The function returns null if no element is found.
ByAttributes<TControl>(params string[])
Same as ByAttributes(). Returns a control type instead of element.
Declaration
public TControl ByAttributes<TControl>(params string[] nameValuePairs) where TControl : Control, new()
Parameters
nameValuePairs
string[]
A list of name value pairs to match. For example:
"bar=foo","class=myclass","src=foo.gif","src=!bar". Use = for partial values or =! to exclude values.
Returns
TControl
The control object.
ByContent(string)
Returns an element by searching its TextContent. The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
ByContent(string, FindContentType)
Returns an element by searching its content with the options to pick the content type. (InnerText, InnerMarkup or OuterMarkup) The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public Element ByContent(string content, FindContentType contentType)
Parameters
content
The content to match.
contentType
The content type to match.
Returns
The found element. The function returns null if no element is found
ByContent(string, FindContentType, params string[])
Returns an element by searching its content with the options to pick the content type. (InnerText, InnerMarkup or OuterMarkup) in addition to a list of attributes. The content must start with: 'l:' for exact match strings, 'p:' for partial strings or 'x:' for regular expression matches. If none are provided, the string will be matched using an exact match 'l:'. All searches are case-insensitive.
Example:
InnerText for div1 : Text1Text2 {recursive} TextContent of div1 : Text1 {non-recursive}
Default for ByContent is TextContent which is the most common usage.
Declaration
public Element ByContent(string content, FindContentType contentType, params string[] nameValuePairs)
Parameters
content
The content to match.
contentType
The content type to match.
nameValuePairs
string[]
The attribute name/value pairs to apply.
Returns
The found element. The function returns null if no element is found
ByContent<TControl>(string)
Same as ByContent(). Returns a control type instead of element.
Declaration
public TControl ByContent<TControl>(string content) where TControl : Control, new()
Parameters
content
The content to match
Returns
TControl
The control object.
ByContent<TControl>(string, FindContentType)
Same as ByContent(). Returns a control type instead of element.
Declaration
public TControl ByContent<TControl>(string content, FindContentType contentType) where TControl : Control, new()
Parameters
content
The content to match.
contentType
The content type to match.
Returns
TControl
The control object.
ByContent<TControl>(string, FindContentType, params string[])
Same as ByContent(). Returns a control type instead of element.
Declaration
public TControl ByContent<TControl>(string content, FindContentType contentType, params string[] nameValuePairs) where TControl : Control, new()
Parameters
content
The content to match.
contentType
The content type to match.
nameValuePairs
string[]
The attribute name/value pairs to apply.
Returns
TControl
The control object.
ByCssSelector(string)
Returns an element by searching using a css selector.
Declaration
public Element ByCssSelector(string selector)
Parameters
selector
The css selector.
Returns
The found element.
Exceptions
Thrown if element is not found and ThrowIfNullOrEmpty is true.
ByCssSelector<TControl>(string)
Returns a control by searching using a css selector.
Declaration
public TControl ByCssSelector<TControl>(string selector) where TControl : Control, new()
Parameters
selector
The css selector.
Returns
TControl
The found control.
ByCustom(Predicate<Element>)
Return the element that matches the defined predicate.
Declaration
public Element ByCustom(Predicate<Element> predicate)
Parameters
predicate
The custom predicate that defines the element search.
Returns
The element, else null.
Exceptions
Thrown if the element was not found and ThrowIfNullOrEmpty is set.
ByCustom<TControl>(Predicate<TControl>)
Get a control by using custom predicate. This function will try to eliminate any elements that are not convertable to TControl type.
Declaration
public TControl ByCustom<TControl>(Predicate<TControl> predicate) where TControl : Control, new()
Parameters
predicate
Predicate<TControl>
The predicate.
Returns
TControl
The first found control.
ByExpression(HtmlFindExpression)
Find element by HtmlFindExpression.
Declaration
public Element ByExpression(HtmlFindExpression expression)
Parameters
expression
The HtmlFindExpression to use during the find.
Returns
An Element if found or null if not found.
ByExpression(HtmlFindExpression, bool)
Find Element by HtmlFindExpression.
Declaration
public Element ByExpression(HtmlFindExpression expression, bool includeRoot)
Parameters
expression
The HtmlFindExpression to use during the find.
includeRoot
Whether or not to include the root element in the search.
Returns
An Element if found or null if not found.
ByExpression(params string[])
Find element by HTML find expression strings.
ByExpression<TControl>(HtmlFindExpression)
Find element by HtmlFindExpression of type TControl.
Declaration
public TControl ByExpression<TControl>(HtmlFindExpression expression) where TControl : Control, new()
Parameters
expression
The HtmlFindExpression to use during the find.
Returns
TControl
A TControl element if found or null if not found.
ByExpression<TControl>(params string[])
Find element by HTML find expression strings of type TControl.
Declaration
public TControl ByExpression<TControl>(params string[] expression) where TControl : Control, new()
Parameters
expression
string[]
Array of HTML find expression strings.
Returns
TControl
A TControl element if found or null if not found.
ById(string)
Returns an element by looking for its 'id' attribute value.
ById<TControl>(string)
Same as ById(). Returns a control type instead of element.
Declaration
public TControl ById<TControl>(string id) where TControl : Control, new()
Parameters
id
The id value for the element to find. All searches are case-insensitive
Returns
TControl
The control object.
ByImage(Image, double, int, bool, Point, int, float, int)
Return an element that looks like specific image.
Declaration
public Element ByImage(Image image, double score, int index = 0, bool autoScroll = true, Point probeOffset = default, int timeout = 2147483647, float imageScale = 1, int searchDelay = 100)
Parameters
image
The image to search for.
score
The minimum matching score. Value must be between 0 and 100.
index
The image index. This is a zero-base index.
autoScroll
If set to true will scroll the page under test. If set to false will search only the visible area.
probeOffset
Probing offset. Offset from center of the image rectagle. If the element you are looking for is not in the center of provided image use this argument to specify where its center is (related to image center).
timeout
Timeout in milliseconds.
imageScale
The original screen scaling applied when the image was taken.
searchDelay
Number of ms to wait before searching for element image.
Returns
The element that matches provided image. If the element is part of iframe will return iframe element.
ByName(string)
Returns an element by searching for its 'name' attribute value
ByName<TControl>(string)
Same as ByName(). Returns a control type instead of element.
Declaration
public TControl ByName<TControl>(string name) where TControl : Control, new()
Parameters
name
The name value for the element to find. All searches as case-insensitive
Returns
TControl
The control object.
ByNodeIndexPath(string)
Returns an element by searching for it using a node index path.
For example:
This call: ByNodeIndexPath("0/2/0/1") describes the <target></target> element in the below hierarchy:
<referenceElement> (0)<foo> <bar> </bar> <car> </car> (2)<bus> (0)<driver> <cap> </cap> (1)<target> </target> </driver> </bus> </foo> </referenceElement>
Declaration
public Element ByNodeIndexPath(string nodeIndexPath)
Parameters
nodeIndexPath
The node index path. This is a forward slash delimited list of tree node indexes that describe a specific path to a target element while ignoring the actual element tags and simply describing the hierarchal relationship to that element.
Returns
The found element. The function returns null if no element is found.
ByNodeIndexPath<TControl>(string)
Same as ByNodexIndexPath(). Returns a control type instead of element.
Declaration
public TControl ByNodeIndexPath<TControl>(string nodeIndexPath) where TControl : Control, new()
Parameters
nodeIndexPath
Node index path identifying the target element to wrap.
Returns
TControl
A control of the specified type.
Exceptions
Thrown no element at the node index path.
ByTagIndex(string, int)
Returns an element by its tag name occrrence.
ByTagIndex<TControl>(string, int)
Same as ByTagIndex(). Returns a control type instead of element.
ByXPath(string)
Returns an element using an XPath. This function supports the System.Xml XPath syntax.
ByXPath<TControl>(string)
Same as ByXPath(). Returns a control type instead of element.
Declaration
public TControl ByXPath<TControl>(string xpath) where TControl : Control, new()
Parameters
xpath
The XPath string. XPath is case-sensitive.
Returns
TControl
The control object.
FromCollection(FindExpressionCollection<HtmlFindExpression>)
Finds all elements with the HtmlFindExpressions in the collection.
Declaration
public IDictionary<string, Element> FromCollection(FindExpressionCollection<HtmlFindExpression> collection)
Parameters
collection
FindExpressionCollection<HtmlFindExpression>
The HtmlFindExpressions
Returns
The elements found
Remarks
If ThrowIfNullOrEmpty is true, this function will throw if an element is not found. If ThrowIfNullOrEmpty is false, this function will not add and element that we not found.
FromExpressionsFile(string)
Finds all elements with FindExpressions defined in a file.
Declaration
public IDictionary<string, Element> FromExpressionsFile(string fileName)
Parameters
fileName
The filename where all the FindParams are defined.
Returns
The collection of found elements in a dictionary with the key being the key defined in the file
FromExpressionsXml(string)
Finds all elements with FindExpressions defined in the serialized xml string.
Declaration
public IDictionary<string, Element> FromExpressionsXml(string xml)
Parameters
xml
The xml with the expressions defined.
Returns
The collection of found elements in a dictionary with the key being the key defined in the xml
ImageMatch(Image, double, int, int, float, int)
Find area within the browser view port that looks like specified image.
Declaration
public Rectangle ImageMatch(Image image, double score, int index = 0, int timeout = 2147483647, float imageScale = 1, int searchDelay = 100)
Parameters
image
The image to search for.
score
The minimum matching score. Value must be between 0 and 100.
index
The image index. This is a zero-base index.
timeout
Timeout in milliseconds.
imageScale
The original screen scaling applied when the image was taken.
searchDelay
Number of ms to wait before searching for element image.
Returns
Area (in screen coordinates) that looks like specified image. Default value (default(Rectangle)) means no area is found.
ImageMatches(Image, double, int, float, int)
Find areas within the browser view port that looks like specified image.
Declaration
public ReadOnlyCollection<Rectangle> ImageMatches(Image image, double score, int timeout = 2147483647, float imageScale = 1, int searchDelay = 100)
Parameters
image
The image to search for.
score
The minimum matching score. Value must be between 0 and 100.
timeout
Timeout in milliseconds.
imageScale
The original screen scaling applied when the image was taken.
searchDelay
Number of ms to wait before searching for element image.
Returns
Collection of screen areas that look like specified image.
Events
ReturnedNullOrEmpty
Invoked when one of the find methods returns null or an empty list of elements.
Declaration
public event EventHandler<ReturnedNullOrEmptyEventArgs> ReturnedNullOrEmpty
Event Value