(This post was written by Evgeny Kanakov from the Micro Focus LeanFT R&D Team)
Creating good identifications for application objects (while automating it for functional testing) is sometimes difficult. The reasons for the difficulty vary:
- Not having testability in mind while developing the application (e.g. not giving IDs for its elements)
- Applications having a very “flexible” / “dynamic” structure
- You may find out that the object you would like to automate doesn’t have “good” properties that you can rely on for creating a unique identification
- An object you would like to automate is too “similar” to other objects in the same page (so you have hard time when trying to create for it a unique identification)
Micro Focus LeanFT provides an identification mechanism that can be very helpful in such cases. The idea behind it is to enable identifying objects while relaying on their visual neighbor objects as anchors. Imagine that you could define your identification to be something like: “The button which is in the same line with the ‘TOTAL’ label, and located below the logo”. Well, with LeanFT’s Visual Relation Identifiers (VRI), you can. Does this capability sound interesting? Continue reading to find out how it works.
Using Visual Relation Identifiers in UFT Pro (LeanFT)
UFT Pro (LeanFT) provides two methods to create VRI identifications: programmatically and via Application Models. VRI can be used in LeanFT for each of the supported application technologies (including web, mobile and desktop applications), in all the operating systems supported by LeanFT (Windows, Mac and Linux), and in all the supported IDEs (Visual Studio, Eclipse and IntelliJ, using the relevant LeanFT SDK).
Problem description in a real application
As an example application, I will use a demo web application that represents a simple online store. Let’s review the application page for the “HP ENVY - 17T” laptop product:
Below the product images, there is a section named “PRODUCT SPECIFICATIONS”. I am going to write a test that will check that the value of the “DISPLAY” field contains the value of the display resolution presented in the “DISPLAY RESOLUTION” field (“1920x1080” in our case).
Let’s take a look at the html code for these two fields:
As you can see, there are no properties that enable to uniquely identify the labels of these fields’ values.
A human trying to describe one of these fields (e.g.: the “DISPLAY” value field), would say: “A label having the ‘DISPLAY’ label” to its left, in the same line”, but how to convert this into identification code used in a script? I will show you how you can do this with VRI.
While you can create VRI identifications programmatically in your code, we’ll see a more convenient way to do it by using Application Models.
Modeling our objects in Application model
In this section we are going to create an application model and model our example’s objects. I assume that you are already familiar with LeanFT Application Models. If not, feel free to read about them here.
1. In your IDE, add an application model item to your testing project
2. Open your browser and navigate to http://advantageonlineshopping.com/#/product/7
3. Open the Object Identification Center (OIC), from the Application Model editor:
4. Start spying the application and click on the “DISPLAY” label:
5. In the OIC, click the “Add to Application Model” button. A test object for the “DISPLAY” label will be added to the application model.
6. Repeat steps 4-5 for the “DISPLAY RESOLUTION” label.
7. Now we will add to our application model test objects for the labels that include the values of these two fields.
Start by spying the value of the DISPLAY field. You should get something like this:
In the OIC un-check the “InnerText” property (Identifying the object based on its value is a bad idea, taking into account that the value presented here is dynamic and may change). Instead, check the “ClassName” property.
If you will now validate the object identification from the OIC, you will see the following, and get in the OIC a message like “Matching objects found: 9”:
In other words, our identification is not unique. Do not be worried. We are going to fix this later using VRI.
From the OIC, add the test object to the application model.
Do exactly the same, for the label holding the value of the “DISPLAY RESOLUTION” field.
8. In the Application Model, we will change the names and code names for the objects we just added, to something more meaningful for us (e.g.: DisplayValue and DisplayResolutionValue).
Our Application Model will look like this:
Finalizing our object identifications with VRI
Our Application Model already includes all the test objects that are needed to write the test. But two of them, DisplayValue and DisplayResolutionValue, cannot be used to uniquely identify the corresponding objects in the application, as we saw above. Let’s add VRI properties for these objects to make our object identifications unique:
1. In the Application Model, choose the DisplayValue object, and in the object details pane, switch the Object Properties view from “Used” to “All”:
2. In that view, under “Featured Properties” check the “VRI” property and click “ Reference Object”
3. The idea is to use the “DISPLAY” object, as a reference (anchor) to the DisplayValue object. Simply select it in the Application Model and press the “Select” button:
The DISPLAY object should now appear under the VRI section of the DisplayValue object. Choose Relation as “Left”, which means that we expect the DISPLAY object to be presented (as we, the users, see it) to the left of the object we are trying to describe:
4. Now let’s check if our VRI property can uniquely identify the object we are looking for: press the VRI Preview button, pointed at in the above image. You will see something like this:
The VRI Preview button visually demonstrates the test objects relations. You can see the green rectangle showing the anchor test object; in our case it is the DISPLAY test object. You can also see that the screen is divided into two areas by a vertical green line that demonstrates the ‘Left’ relation. So the described test objects will only be searched for to the right of this line. The blue rectangles show the candidate test objects that matches the VRI relation that was defined. Notice that the candidate test objects also take into account all other the descriptive properties that were defined (like the “ClassName” property having the ‘value ng-binding’ value that we checked above), so test objects with a different class name won’t be shown as candidates.
It looks like we are still getting too many candidate objects (and don’t have a unique identification). We’ll easily resolve this in the next step.
5. Under the VRI relation (presented in the image in step 3) check the “Inline” check-box and press the VRI Preview button once again. Inline means that we define our anchor to be aligned with our object. This is what you will see:
Now only one matching object was found and our object’s identification is ready to be used.
Repeat the same steps for the DisplayResolutionValue object.
In the next section we’ll see how to use the Application Model in our test.
Writing our test
Finally, let’s write a test that will use our Application Model:
In this simple test we are checking that the value of the “DISPLAY” field contains the value of the “DISPLAY RESOLUTION” field.
We saw how useful LeanFT’s Visual Relation Identifiers can be to overcome situations where creating standard object identification is problematic. VRI provides additional capabilities that are not covered in this blog. You can learn about these capabilities in the VRI page of the LeanFT Help Center.
About UFT Pro (LeanFT)
Micro Focus UFT Pro (LeanFT) is a powerful test automation solution that was built specifically for continuous testing and continuous integration. It enables advanced automation engineers and dev-testers in agile teams to leverage the standard development IDEs for test automation.
You can find more information about LeanFT, in the LeanFT Help Center