Handle Dynamic Id with ease in Test Studio

Learn more about Test Studio here

Download 30 days Free Trial Here

Okay, so I will start this post saying there is not a single golden rule to handle dynamic id while automating Test. It depends, I repeat it depends. In this post I will focus on various ways you can handle dynamic id in Test Studio.

clip_image002

In this post we will learn to deal with dynamic id in three different ways,

  1. Handling with changing setting at Test Project level
  2. Handling with changing setting at Element level
  3. Handling with Coded Step

Scenario # 1 : Test Project Level

Let us say you are sure that id is going to be dynamic throughout the application. And you cannot rely on id to identify element. In this scenario Test Studio allows you to configure Identification Logic at the Test Project level. You can change that in Project Settings.

By default Test Studio finds and element on following identification logic. It search on the id AND tagname. This is set for the Test Project level.

clip_image002

Now we know that id for application in test is going to be dynamic and we want to change the FindLogic to some other tag. In other words we want to change default Find Logic for whole Test Project. To do this navigate to Project Settings.

You can browse to FindLogic setting of Test project by clicking on

Project -> Show Setting

clip_image004

In Project Setting dialog box click on identification logic. Here you can find identification logic and change it as per requirement to your project.

clip_image006

As you can clearly see that by default Test Studio find an element on its Id. If you consider that Name of the elements are not expected to change frequently then you can consider to change project setting to find an element on its Name attribute.

To change FindLogic select any attribute then click on up/down arrow to change its position. For example Name is at second from top. To make it first attribute on which Test Studio will find element select Name and then click on up arrow button to bring it at the top in the list.

clip_image008

In this way you can configure FindLogic. You may notice that if required you can add new Tag to list as well. After reconfiguring Identification Logic you can find that now FindLogic for whole Test project (recording which will happen after changing logic) has been set to name and tagname.

clip_image010

Scenario # 2 : Element Level

Second scenario could be you need to change identification logic only for a particular element. You do not want to change identification logic for whole Test project rather for a particular element. Test Studio allows you to do that as well. To do this select element from Element Repository and right click. From context menu select Edit Element.

clip_image001

When click on Edit Element, next you will get various options to Find Element.

clip_image003

Let us Find Element in Live Version. To find element in Live Version make sure that application is already open in browser. Since already application is opened here so select open Current Page and select application from drop down.

clip_image005

After navigating to application you will find Elements current FindLogic.

clip_image007

To delete current logic move mouse and you get option like below to delete current filter.

clip_image009

After deleting both filter let us use XPath to find element.

clip_image011

This is what all you need to change FindLogic for a particular element. You can see in Element Repository that FindLogic has been changed to XPath.

clip_image013

Scenario # 3 : Code Step

So far in above two scenario we saw that if Id is dynamic we can change it to some other attribute of element like Name, XPath etc. Now there could be scenario that you cannot rely on any attributes.

XPath gives you positional based FindLogic. Consider scenario of a Grid. In Grid you need to automate test to find an Employee ‘e2’. Say e2 is second employee in database and is listed in the second rows of Grid. You can put verification step as below,

clip_image001[6]

Now above test will fail if order of elements in Grid is changed. So in this kind of scenario you cannot rely on either id or XPath of element.

There could be many ways you can fix and automate test to find whether employee e2 exists in db or not. Test Studio allows you to write your custom logic using Coded Step. So we can write a coded step as below.

clip_image002[6]

In above coded step, we are finding reference of Grid then searching Employee ‘e2’ in whole grid and then asserting if count is equal to 1 to verify existence of Employee e2.

So in this scenario we are not finding element on any of the attribute and writing our own logic in script to find element.

Conclusion

We learnt in this post that there are various ways to deal with dynamic id. Again there is not a single bullet proof method to deal with dynamic id. It depends on the scenario that how dynamic id cane be handled while automating test.

Learn more about Test Studio here

Download 30 days Free Trial Here

How to debug a Coded Step in Test Studio?

Learn more about Test Studio here

Recently in one of the customer interaction I came across a question that how a Coded Step in Test Studio can be debugged? This is an important query and can be very useful while writing complex Coded Step.

So let us begin with following recorded step,

image

As we see step 6 is a Coded Step. We need to debug this step. To being with we cannot debug a particular line of coded step in Test Studio desktop version. In Test Studio desktop version we can put breakpoint to debug only at Test Step level. So we can put a breakpoint on Test Step as given below. Test Studio will wait for user at step 3, 4 and 6.

image

Putting breakpoint at Test Step level is simple. However our requirement is to debug particular line of code in Coded Step. We cannot do this in Test Studio Desktop version. But this can be done in Test Studio plug-in of Visual Studio. So we need to follow below steps to debug coded step.

Very first open Test in Visual Studio and put a breakpoint in line of the code we need to debug. This can be done as follows,

image

After this open Test Explorer. To open it from menu select Test and then Windows and then Test Explorer. See the image below for reference.

image

Once Test Explorer is open select test (of which coded step you want to debug). In this case that is Test3 and right click on that and from context menu select Debug Selected Tests as given in below image,

image

Now on debugging of that particular test you will find breakpoint is being hit.

image

We learnt it is super easy to debug a codes step in Test Studio. I hope you find this post useful. Thanks for reading.

Learn more about Test Studio here

Testing with LINQ in Test Studio

Recently I was giving demo on Test Studio to one of  the customer. In demo one of the participants raised a question that, “How could I use LINQ (Language Integrated Query) to insert some data back to database after test execution gets completed “

I demoed to team  that how they could use LINQ in test. In this post, I will walk you through steps required to test with LINQ in Test Studio. We will insert data back to database as last step of the test.

Essentially we can achieve in these three steps

  1. Write LINQ and business logic in different library
  2. Add library as reference in test project
  3. Write script of coded step calling library functions.

To start testing with LINQ, add a coded step in your test. To add coded step select Test from menu and then Script Step from ribbon as given in below image.

image

Once Code step is added export test project to Visual Studio. To export to visual studio from menu click on Export to Visual Studio as shown in image below,

clip_image001

Creating Library to work with LINQ

Now in visual studio you need to add operations related to LINQ in different class library. Idea is that we will create separate library for LINQ operation and later we will add that library to Test Studio.

clip_image003

Assume that we have created a library with name DataLibrary. This Library got

  1. LINQ data context classes
  2. We added a static class in which operation is being performed to insert data back to database.

So DataHelper class contains following code to insert data to database using LINQ.


public static class DataHelper
 {

public static void InsertItem()
 {
 DataClasses1DataContext context = new DataClasses1DataContext();
 Product p = new Product
 {
 Discontinued = false,
 ProductID = 99,
 CategoryID = 2,
 ProductName = "Test Studio 9",
 UnitPrice = 200
 };

context.Products.InsertOnSubmit(p);
 context.SubmitChanges();

 }

}

Adding Reference in Test Project

There are in two ways you can work on Test Studio. Either on Visual Studio plugin or on Test Studio standalone version.

Adding reference in Visual Studio is simpler. Right click on test project and from context menu select Add Reference. Browse to DataHelper.dll library and add reference in test project.

Even though you have added reference in Visual Studio make sure that you have added reference in test project opened in standalone version else you will get exception.

image

To add DataHelper.dll assembly in Test Studio standalone version you need to follow following steps,

Select Project option in top menu

clip_image001[6]

Click on Show Settings in project tab

clip_image002

On clicking on Show Settings you will get Project Settings dialog box. In dialog box click on Script Options. To add assembly reference click on Add Reference.

image

When you clock on Add Reference Test Studio will open File Chooser dialog box. In that browse DataHelper.dll assembly reference location in file system and select that to add as reference in current Test project.

Writing Coded Step

Once reference of DataHelpe.dll is added in test project you need to write script for coded step. We can insert a row in database by calling static InsertItem method. So coded step will be as following


[CodedStep(@"New Coded Step")]
 public void LINQ_Test_CodedStep()
 {

DataHelper.InsertItem();

 }

In this way we can test with LINQ in Test Studio. To summarize

  1. Write LINQ and business logic in different library
  2. Add library as reference in test project
  3. Write script of coded step calling library functions.

I hope you find this post useful. To speedup automated testing do not forget to download trail of Test Studio from here

image