Invoking Actions on Elements
Once you have found an element you can then interact with it. Many different types of interactions are supported like clicking, mouse hover over, mouse wheel-related actions, keyboard clicks etc.
- Clicking On an Element - invoke a click on a FrameworkElement.
- Sending Text to a Control - type text content into a FrameworkElement.
- Mouse Actions - invoking different mouse actions on FrameworkElement.
- FrameworkElement Properties and Actions.
- UI Element Actions - understanding wrappers.
- Telerik Testing Framework Specific Properties - commonly used Properties of Telerik's FrameworkElement.
- Telerik Testing Framework Specific Methods - commonly used Methods of Telerik's FrameworkElement.
- Getting and Setting Properties on Elements - working with non-built in Properties.
- Invoking Element Methods - invoke any method attached to an element.
To invoke a UI action, use the User object that is attached to every UI element. For example, to click on an element such as a button, enter code like this:
//In this example app can be a Silverlight application or the MainWindow element of a WPF application
// Click the ShowGuide button.
// Right click the ShowGuide button 5 pixels away from the center of the control
new System.Drawing.Point(5, 5), OffsetReference.AbsoluteCenter);
In this example app can be a Silverlight application or the MainWindow of a WPF application
' Click the ShowGuide button.
' Right click the ShowGuide button 5 pixels away from the center of the control
New System.Drawing.Point(5, 5), _
To send text to the control use one of the following lines of code:
searchText.User.TypeText("Abe Lincoln", 100);
searchText.User.TypeText("Abe Lincoln", 100)
To perform one of the various possible mouse actions use one of the following lines of code:
admin1.User.TurnMouseWheel(5, MouseWheelTurnDirection.Backward, false);
admin1.User.TurnMouseWheel(5, MouseWheelTurnDirection.Backward, False)
All of the Telerik Testing Framework UI controls derive from the FrameworkElement object. Our FrameworkElement object mirrors as closely as possible XAML's FrameworkElement as defined in MSDN here. For customers interested in our extensibility model, the Silverlight Extension does not expose a UIElement and therefore, our FrameworkElement can be viewed as the combination of Silverlight/WPF's UIElement + FrameworkElement. These are just few examples of the many available properties of our FrameworkElement object:
|AutomationId ||Retrieves the Automation ID assigned to this element. |
|Children ||Retrieves a list of elements that are children of this element in the visual tree. |
|Clip ||Gets or sets the Geometry used to define the outline of the contents of a UI Element. |
|Height ||Gets or sets the suggested height of a FrameworkElement. |
|Opacity ||Gets or sets the degree of the object's opacity. |
|OpacityMask ||Gets or sets the brush used to alter the opacity of regions of this object. |
|Width ||Gets or sets the suggested width of a FrameworkElement. |
Each UI element wrapper class contained in Telerik Testing Framework has a set of properties used to access the properties appropriate for that UI element. For example: The Calendar control has a SelectedDate property that you can use to read and/or set the currently selected date of the control. It also has a SelectionMode property you can use to read and/or change the selection mode from a single date to a multi-range date selection. The CheckBox control has an IsChecked property you can use to read/set the checked state of the control and so on.
We have implemented a few special properties in our FrameworkElement object to aid in automation. These include:
|AbsoluteSiblingTagIndex ||Returns the sibling tag index of this FrameworkElement relative to its other siblings within the Visual Tree. |
|AbsoluteTagIndex ||Returns the absolute index of this XAML tag within the entire Visual Tree. |
|Application ||Gets the Application object that owns this element. |
|EnableValidateMouseLocation ||Enables or disables the validation of mouse click locations before performing mouse actions. |
|Find ||Gets the Find object that can be used to search the visual children of this element. |
|TagNameIndex ||Gets the tag name index of the XAML tag name in the visual tree. |
|User ||Gets the UI interaction object which allows you to interact with this framework element directly using real mouse and keyboard interactions. |
|Wait ||Gets a VisualWait object you can use to wait for Visual elements in the Visual Tree. |
|XamlTag ||Gets the XAML tag name of this FrameworkElement. This is used for hierarchy matching and traversal. |
The FrameworkElement class implements a number of methods just for purpose of test automation. Some of the most commonly used include:
|CastAs<T>() ||Returns a FrameworkElement as a strongly-typed control. Does not enforce a tagname to match the type. |
|GetChildren() ||Returns the child elements of the FrameworkElement. |
|GetNextSibling() ||Returns the next sibling of the FrameworkElement. |
|Parent() ||Returns the parent element of the current Framework Element |
|ScrollToVisible() ||Scrolls the browser so that the Framework Element is visible. |
If the property of an element you want to get or set isn't available as a property in the UI element wrapper object, you can use the GetProperty and SetProperty from the FrameworkElement object to get/set it. This is especially useful if you start designing your own custom Silverlight UI controls or use a third party custom control. Let's suppose I have a UI element that represents an airline ticket. One of the many obvious properties such a control would need to have is flight number. To fetch this property from the control I can use code like this:
// Fetch the flight number from the ticket
FrameworkElement ticket = app.FindName("airlineTicket");
AutomationProperty flightNoProperty = new AutomationProperty("flightNo", typeof(string));
string flightNo = (string)ticket.GetProperty(flightNoProperty);
// Update the flight number on the ticket
' Fetch the flight number from the ticket
Dim ticket As FrameworkElement = app.FindName("airlineTicket")
Dim flightNoProperty As New AutomationProperty("flightNo", GetType(String))
Dim flightNo As String = DirectCast(ticket.GetProperty(flightNoProperty), String)
' Update the flight number on the ticket
Another advanced feature of the framework is the ability to invoke any method attached to an element. Here's how to invoke the ScrollToVerticalOffset method of a ScrollViewer element.
ScrollViewer searchScroll = app.FindName("patientSearchScroller").Find.ByType<ScrollViewer>(); searchScroll = app.FindName().Find.ByType<>();
AutomationMethod scrollVert = new AutomationMethod("ScrollToVerticalOffset", null);
Dim searchScroll As ScrollViewer = app.FindName("patientSearchScroller").Find.ByType(Of ScrollViewer)()
Dim scrollVert As New AutomationMethod("ScrollToVerticalOffset", Nothing)