New to Telerik Test StudioStart a free 30-day trial

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.

  1. Clicking On an Element - invoke a click on a FrameworkElement

  2. Sending Text to a Control - type text content into a FrameworkElement

  3. Mouse Actions - invoking different mouse actions on FrameworkElement

  4. Framework Element Properties and Actions

  5. Invoking Element Methods - invoke any method attached to an element.

Clicking On 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:

C#
//In this example app can be the MainWindow element of a WPF application 
// Click the ShowGuide button.
app.Find.ByName("guideButton").User.Click();
 
// Right click the ShowGuide button 5 pixels away from the center of the control
app.Find.ByName("guideButton").User.Click(MouseClickType.RightClick,
     new System.Drawing.Point(5, 5), OffsetReference.AbsoluteCenter);

Sending Text to a Control

To send text to the control use one of the following lines of code:

C#
searchText.User.TypeText("Abe Lincoln", 100);
 
searchText.User.KeyPress(System.Windows.Forms.Keys.L, 100);
 
searchText.User.KeyDown(System.Windows.Forms.Keys.L);
searchText.User.KeyUp(System.Windows.Forms.Keys.L);

Mouse Actions

To perform one of the various possible mouse actions use one of the following lines of code:

C#
admin2.Find.ByType("Thumb").User.DragTo(admin3.Find.ByType("Thumb"));
admin1.User.HoverOver();
admin1.User.MouseEnter(OffsetReference.LeftCenter);
admin1.User.MouseLeave(OffsetReference.RightCenter);
admin1.User.TurnMouseWheel(5, MouseWheelTurnDirection.Backward, false);

Framework Element Properties and Actions

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. These are just few examples of the many available properties of our FrameworkElement object:

Retrieves a list of elements that are children of this element in the visual tree.
PropertyDescription
AutomationIdRetrieves the Automation ID assigned to this element.
Children
ClipGets or sets the Geometry used to define the outline of the contents of a UI Element.
HeightGets or sets the suggested height of a FrameworkElement.
OpacityGets or sets the degree of the object's opacity.
OpacityMaskGets or sets the brush used to alter the opacity of regions of this object.
WidthGets or sets the suggested width of a FrameworkElement.

UI Element Action

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.

Telerik Testing Framework Specific Properties

We have implemented a few special properties in our FrameworkElement object to aid in automation. These include:

PropertyDescription
AbsoluteSiblingTagIndexReturns the sibling tag index of this FrameworkElement relative to its other siblings within the Visual Tree.
AbsoluteTagIndexReturns the absolute index of this XAML tag within the entire Visual Tree.
ApplicationGets the Application object that owns this element.
EnableValidateMouseLocationEnables or disables the validation of mouse click locations before performing mouse actions.
FindGets the Find object that can be used to search the visual children of this element.
TagNameIndexGets the tag name index of the XAML tag name in the visual tree.
UserGets the UI interaction object which allows you to interact with this framework element directly using real mouse and keyboard interactions.
WaitGets a VisualWait object you can use to wait for Visual elements in the Visual Tree.
XamlTagGets the XAML tag name of this FrameworkElement. This is used for hierarchy matching and traversal.

Telerik Testing Framework Specific Methods

The FrameworkElement class implements a number of methods just for purpose of test automation. Some of the most commonly used include:

Returns the child elements of the FrameworkElement.
PropertyDescription
CastAs<T>()Returns a FrameworkElement as a strongly-typed control. Does not enforce a tagname to match the type.
GetChildren()
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.

Getting and Setting Properties on Elements

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 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:

C#
// 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
ticket.SetProperty(flightNoProperty, "HX-1572");

Invoking Element Methods

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.

C#
ScrollViewer searchScroll = app.FindName("patientSearchScroller").Find.ByType<ScrollViewer>();
searchScroll = app.FindName().Find.ByType<>();
 
AutomationMethod scrollVert = new AutomationMethod("ScrollToVerticalOffset", null);
searchScroll.InvokeMethod(scrollVert, 2000);