Finding Page Elements
Telerik Testing Framework provides one of the richest markup identification infrastructures currently available on the market. It builds on top of commonly known element identification methods like 'getElementById', 'getElementByName' or 'XPath' and extends them to provide identification routines that cater more to application automation scenarios. In addition to maintaining a simple and easy to use set of APIs, Telerik Testing Framework introduces a consistent and extensible way to build identification and persist it using 'FindParam' objects.
All the Find.Byxxx methods now support LINQ queries.
It is important to understand how Telerik's identification method works because that understanding will allow you to exploit the power of these identification methods to build robust automation quicker.
Element Identification Overview
The following identification methods are supported:
Methods | Description | Example |
---|---|---|
Find.ById() | Searches for an element contained in a markup document using its set 'id' attribute. When the desired 'id' matches an element's id, the element is returned - identical to getElementById |
|
Find.ByName() | Searches for an element contained in a markup document using its set 'name' attribute. When the desired 'name' matches an element's name, the element is returned |
|
Find.ByImage() Find.AllByImage() |
Searches for an element or 'All' elements that look like a predefined image |
|
Find.ByTagIndex() | Searches for an element using its tag name occurrence index. Finds the element at the specified occurrence index and returns it. This method uses zero based indexing. |
|
Find.ByAttributes() Find.AllByAttributes() |
Searches for an element or 'All' elements using an 'exact' or 'partial' list of attribute values (You can specify 1-N attribute/value pairs). When all attribute values match, the element or collection of elements is returned. |
|
Find.ByContent() Find.AllByContent() |
Searches for an element or 'All' elements using 'exact', 'partial' or 'regex' of the element content. The element content can be: InnerText, InnerMarkup, OuterMarkup, TextContent (default), StartTagContent. |
|
Find.ByExpression() Find.AllByExpression() |
Searches for an element or 'All' elements matching an HtmlFindExpression. The HtmlFindExpression cannot use any type of hierarchical expressions including tag index path expressions, HTML path expressions, and XPath expressions. |
|
Find.ByXPath() Find.AllByXPath() |
Searches for an element or 'All' elements using an XPath expression. WebAii supports the .NET Framework XPath implementation. |
|
Find.ByCssSelector() Find.AllByCssSelector() |
Searches for an element or 'All' elements using a css selector query. |
|
Find.AllByTagName() | Searches for 'All' elements with the specified tag name and returns it as a list of elements. |
|
Find.ByNodeIndexPath() | Searches for an element using dom tree node index paths. This identification is done using an xpath like approach that simply describes the hierarchy path to a specific element using the node index within the hierarchy without having to specify the tag name at each level. This identification method can be chosen in cases where a segment of the DomTree hierarchy at a specific location is consistent but element type changes. For example, if an element you are trying to target is the direct child of another element that fluctuates between a span and div, you can choose this identification method to provide a consistent way to identify that element. |
|
Find.FromCollection() | Finds all elements with the FindParams in the passed in collection. |
|
Find.ByCustom() Find.AllByCustom() |
Searches for an element or 'All' elements using a custom predicate. |
|
Identification Methods Usage
Telerik Testing Framework identification methods are accessible using the Find object that is exposed as a property off of the 'Browser' object : Manager.ActiveBrowser.Find.Byxx(...) and also as a property off of each TestRegion object : TestRegion.Find.Byxx(...).
The difference between the Find object off of the Browser class (Root Base Identification - Identification Sample) and the Find object off of the TestRegion class (ReGion Base Identification - RGBI), is that the one off of the Browser performs all searches starting from the root document element whereas the Find object off of the TestRegion's searches only the elements contained in that specific test region and uses the TestRegion's open tag (i.e. <!--testregion...->) as the root element for reference based identification like tag name occurrence index and XPath identification.
With TestRegions, depending on the areas of the application that each automated test is targeting, you can use different Find objects to give each test a greater level of independence and shield it from product changes outside its target area. This topic is discussed in greater details in Introduction to TestRegions.
Matching Syntax Used in Parameters
The Byxxx functions that take a nameValuePairs parameter recognizes the following matching syntax:
class=myclass - Matches when the 'class' attribute is equal to 'myclass'
class=~myclass - Matches when the 'class' attribute contains the string 'myclass'. This is called the 'partial match' syntax.
class=!myclass - Matches when the 'class' attribute does not contain the string 'myclass'.
You can mix the above syntax into a single Byxxx function. (e.g. Find.ByAttributes("class=~my", "class=~class", "class=!other") will match the string "class=myclass" but will not match "class=myotherclass".)
The Byxxx functions that take a content string parameter recognizes the following matching syntax:
Prefix the string parameter with 'l:' for exact match strings. e.g. 'l:Vacation homes' will match 'Vacation homes' exactly. This is also the default used if no prefix is used.
Prefix the string parameter with 'p:' for partial strings matching. e.g. 'p:Arizona' will match any content containing the string 'Arizona'.
Prefix the string parameter with 'x:' for regular expression matching. e.g. 'x:[foo]' will match any content containing the string 'foo' in the middle of it. For more information about regular expressions see this MSDN article.
Identification Sample
To help illustrate the above identification methods, let's use the following sample application. Note that this application uses TestRegions excessively to allow it to demonstrate the different identification methods that can be performed using both RBI and RGBI:
The code below shows the different methods you can use in your test code to identify elements. Note in the sample below, we are using Visual Studio's Assert class to demonstrate how different objects can be identified differently and to illustrate how to scope the identification with TestRegions using RGBI and across the entire document DomTree using RBI.