Category Archives: Silverlight

How To : Understanding and Use the Search logic for Silverlight controls in Coded UI Test

Understanding the Search logic for Silverlight controls in Coded UI Test

 

One of the primary objectives during recording in Coded UI Test is to generate a robust search condition for a UI control to be uniquely identifiable during playback. In this post I’ll mention some of the search logic specific to the Silverlight UI Automation support within Coded UI Test introduced in the VS 2010 Feature Pack 2.

Search condition generation during Recording

 

For Silverlight control, Coded UI Test relies primarily on the Automation properties of the control. The sequence of looking for a search property in order descending of priority is

AutomationId,

Name,

LabeledBy,

HelpText,

AccessKey,

AcceleratorKey

Specific controls support additional searchable properties. For instance, Button supports  “DisplayText”, Image supports “Source”, DataGrid Cell supports “ColumnIndex” searchable property and likewise. The various search configurations mentioned here are applicable to Silverlight control search too (except for the SearchConfiguration.VisibleOnly configuration).

microsoft-silverlight[1]

For a Silverlight object hosted in IE, the search hierarchy will consist of an IE search part and Silverlight search part –

 

Top Level Window à {IE Search Hierarchy} à Silverlight Root Visual Element à Parent of Target Element à Target Element.

 

Additional hierarchy can be generated in between the Parent and Silverlight Root Visual element based on the specific control requirement. For example, certain controls such as Datagrid, Tree, TreeItem, Tab, List Item are, at almost all times, included in the search hierarchy if they are found in the ancestor hierarchy of the target element. As an example, the extended search hierarchy of a DataGrid Cell will show up as something like

 

TopLevelWindow à {IE Search Hierarchy} à Root Visual Element (Silverlight) à DataGridTable (Silverlight) à DataGridRow(Silverlight) à DataGridCell (Silverlight)

 

 

Search path during Playback

 

The overall search logic remains identical to what is followed in other UI technologies. It is a breadth first search wherein the top level window is first searched and used as a container for searching the next control in the search condition hierarchy. This is done recursively until the leaf control in the search hierarchy is found.

 

As an example, for a simple button inside a Silverlight page, the search hierarchy will be typically of the format –

 

TopLevelWindow à Document (IE BODY Tag) à Pane (IE DIV Tag) à Custom (IE OBJECT Tag) à Root Visual Element (Silverlight) à Button (Silverlight).

 

For the top-down search till the IE Object Tag, the existing search features and settings in Coded UI Test are applicable. Once the search switches to the Silverlight technology (i.e. Root Visual element of the Silverlight page), there are few limitations to the search.

microsoft-silverlight2-developer-reference[1]

 

What is missing currently in Silverlight control search?

 

  1. PlaybackSettings.ShouldSearchFailFast
  • This setting is not honored currently. However, there is some level of customization that can be done using the PlaybackSettings.SearchTimeout and the Playback.PlaybackSettings.WaitForReadyTimeout settings to tweak the timeout at which the search should abort. The later may not seem obvious, and is hence explained in more detail in a section below.

 

  1. PlaybackSettings.MatchExactHierarchy

Silverlight control search does not currently honor  MatchExactHierarchy = false. So, the search condition specified for the entire Silverlight hierarchy needs to be accurate for the search to succeed. In the above example, it is the Root Visual Element and the Button control.

 

  1. Playback.PlaybackSettings.SmartMatchOptions
  • Control level smart match is not currently supported i.e. Control

Note that Regex match is not yet supported in Coded UI Test. So the only option available is to specify the PropertyExpressionOperator.Contains condition operator in the search properties.

For example, if the button’s name is of format “Submit<SomeDynamicId>”, the search property can be defined as –

uISubmitButton.SearchProperties.Add(“Name”, “Submit”, PropertyExpressionOperator.Contains);

 

  1. There is no concept of FilterProperties as supported in Web Technology in Coded UI Test.

 

 

How to handle search failures because of slow page loading?

 

If the XAP download takes a huge amount of time to load, the search during playback would fail since the Silverlight controls will not have been rendered in the visual tree. The internal search algorithm uses a wait and retry logic to search for a control while checking the visual tree rendering status at each wait interval (this time interval is upped exponentially on each iteration).

I will not be explaining the details here, but the important thing to note is that if there is no rendering happening within a polling interval, the search will return with failure status.

This polling interval is currently set to half of the Playback.PlaybackSettings.WaitForReadyTimeout which has a default value of 60 seconds (i.e. the default polling interval is 30 seconds). So to tackle slow page loading time, you can configure this Playback.PlaybackSettings.WaitForReadyTimeout to a desired value.

Note: Playback.PlaybackSettings.WaitForReadyTimeout does affect the normal search failure time in scenarios where there is some visual rendering happening in the Silverlight page. So you would need to strike an appropriate balance based on the type of application you are testing.

Access SAP Business Data From Silverlight 4 Clients Using WCF RIA Services And LINQ to SAP

The introduction of Microsoft’s WCF RIA Services for Silverlight 4 simplified very much the development process of N-tier business applications using Silverlight and ASP.NET. By using this new technology, we can also easily access and integrate SAP business data in Silverlight clients.

This article shows how to provide a SAP domain service as web service that will be consumed by a Silverlight client. The sample application will allow the user to query customer data. The service uses LINQ to SAP from Theobald Software to connect to a SAP R/3 system.

Project Setup

The first step in setting up a new Silverlight 4 project with WCF RIA Services is to create a solution using the Visual Studio template Silverlight Navigation Application:

Screenshot-01.png - Click to enlarge image

Visual Studio 2010 then asks you to create an additional web application, which hosts the Silverlight application. It’s important to select the checkbox Enable WCF RIA Services (see screenshot below):

SAP2Silverlight/Screenshot-02.png

After clicking the Ok button, Visual Studio generates a solution with two projects, one Silverlight 4 project and one ASP.NET project. In the next section, we will create the SAP data access layer using the LINQ to SAP designer.

LINQ to SAP

The LINQ to SAP provider and its Visual Studio 2010 designer offers a very handy way to design SAP interfaces visually. The designer will generate the code for the SAP data access layer automatically, similar to LINQ to SQL. The LINQ provider is part of the .NET library ERPConnect.net from Theobald Software. The company offers a demo version for download on its homepage.

The next step is to create the needed LINQ to SAP file by opening the Add New Item dialog:

Screenshot-03.png - Click to enlarge image

LINQ to SAP is internally called LINQ to ERP.

Clicking the Add button will create a new ERP file and opens the LINQ designer. Now, drag the Function object from the toolbox and drop it onto the designer surface. If you have not entered the SAP connection data so far, you are now asked to do so:

Screenshot-04.png - Click to enlarge image

Enter the connection data for your SAP R/3 system and then click the Ok button. Next, search for and select the SAP function module named SD_RFC_CUSTOMER_GET. The function module provides a list of customer data.

The RFC Function modules dialog opens and lets you define the necessary parameters:

SAP2Silverlight/Screenshot-05.png

In the above function dialog, change the method name to GetCustomers and mark the Pass checkbox for the NAME1 parameter in the Exports tab. Also set the variable name to namePattern. On the Tables tab, mark the Return checkbox for the table parameter CUSTOMER_T and set the table and structure name to CustomerTable and CustomerRow:

SAP2Silverlight/Screenshot-06.png

After clicking the Ok button and saving the ERP file, the LINQ designer will generate a SAPContext class which contains a method called GetCustomers with an input parameter named namePattern. This method executes a search for SAP customer data allowing the user to enter a wildcard pattern. The method returns a table of customer data:

SAP2Silverlight/Screenshot-07.png

On the LINQ designer level (click on the free part of the LINQ designer surface) property, Create Object Outside Of Context Class must be set to True:

Screenshot-08.png - Click to enlarge image

Now, we finally add a Customer class which we use in our SAP domain service later on. This class and its values will be transmitted to the Silverlight client by the WCF RIA Services. It’s important to set the Key attribute on the identifier fields for WCF RIA Services, otherwise the project will not compile:

Screenshot-09.png - Click to enlarge image

That’s it! We now have our SAP data access layer ready to use and can start adding the domain service in the next section.

SAP Domain Service

The next step is to add the SAP domain service to our web project. A domain service is a specialized WCF service and is one of the core constructs of WCF RIA Services. The service exposes operations that can be called from the client generated code. On the client side, we use the domain context to access the domain service on the server side.

Add a new Domain Service Class and name it SAPService:

Screenshot-10.png - Click to enlarge image

In the upcoming dialog, create an empty domain service class by just clicking the Ok button:

SAP2Silverlight/Screenshot-11.png

Next, we add the service operation GetCustomers to the SAP service with a name pattern parameter. The operation then returns a list of Customer objects. The Query attribute limits the result set to 200 entries.

The operation uses the visually designed SAP data access logic to retrieve the SAP customer data. First of all, an instance of the SAPContext class will be created using a connection string (see sample in code). For more details regarding the SAP connection string, see the ERPConnect.net manual.

The LINQ to SAP context class contains the GetCustomers method which we will call using the given namePattern parameter. Next, the operation creates an instance of the Customer class for each customer record returned by SAP.

The license code for the ERPConnect.net library is set in the constructor of our domain service class.

Screenshot-12.png - Click to enlarge image

That’s all we need on the server side.

In the next section, we will implement the Silverlight client.

Silverlight Client

The implementation of the client side is straightforward. The home view contains a DataGrid control to display the list of customer data as well as a search area with TextBox and Button controls to allow users to enter name search pattern.

The click event handler of the load button, called OnLoadButtonClick, will execute the SAP service. The boilerplate code to access the web service was generated by WCF RIA Services in the subfolder Generated_Code in the Silverlight project.

First of all, an instance of the SAPContext will be created. Then, we load the query GetCustomersQuery and execute the service operation on the server side using WCF RIA Services. If the domain service returns an error, the callback anonymous method will mark the error as handled and display the error message.

If the execution of the service operation succeeded, the result set gets displayed in the DataGrid control.

Screenshot-13.png - Click to enlarge image

The next screenshot shows the final result:

Screenshot-14.png - Click to enlarge image

That’s it.

Summary

<

p>This article has shown how easily SAP customer data can be integrated within Silverlight clients using tools like WCF RIA Services and LINQ to SAP. It is quite simple to extend the SAP service to integrate all kinds of operations.