Introduction to Coded step in Test Studio
In our previous post, we discussed how Test Studio can be used for recording test steps. Test Studio has extensive recording properties but sometimes some test scenario are more complex and the recorder or even element/actions menu will not be able to handle them. So, for those complex scenario that requires specialized steps, Test Studio allows you to create coded steps. Test Studio supports C# and Visual Basic, either of which can be used for writing coded steps.
There are two ways to which you can Add Edit Coded Step to a test case.
- Using Coded Step from Step Builder
- By Editing existing step in code
Using Coded Step from Step Builder
A coded step can be added to the existing test by using “Coded Step” option from the Step Builder. When double clicked it will add a coded step to the existing test.
Once Coded Step is double-clicked it will add a coded step to the existing test.
Click on the drop-down present in the newly added coded step.
- <New Coded Function> is the default name provided by the Test Studio.
- searchBing_CodedStep is an existing coded step that is already present in the Test.
The drop allows you to use a coded function in the hierarchy of the existing recorded test steps. So, if I want a coded function to be executed after Step 4 then I will choose the same from the drop down.
You can also change the name of the function to whatever you want, by using rename button beside the test step.
Now, select the <New Coded Function> from the drop down. Once it is selected it will open a new tab with the code for the new function.
Just enter the code for your step inside searchBing_CodedStep1 and you are good to go. Let’s enter a simple code for start.
The test case we have here is from our previous, tutorial find element.
Test Case:
- Open bing.com
- Provide a search string
- Click on search button
- Validate that the first result contains the search
After validation is complete for the search string i.e. all four test step has been executed, let’s say we want to navigate back to the previous page. Let’s reconfigure the test case by adding a 5th step for navigating back.
- Open bing.com
- Provide a search string
- Click on search button
- Validate that the first result contains the search
- Navigate back to the previous screen.
So, write the C# code for the coded step in the selected method.
Run the test and validate that the coded step is working as expected.
Editing existing recorded step in code
If a test step is a little complex then a recorded step will not suffice to cover all the functionality, so sometimes we will need to edit the step by converting it into the code so that it could cater to the required functionality.
To start with, right-click on any step you need to convert into code and edit. The right-click menu will appear with “Edit in Code” option select that option and provide your language choice and the step will be converted into code. Now you will be able to edit it as per your requirement.
So once coded step has been selected, either from Step Builder or by using Edit in Code. A pop-up will appear asking you for your preferred language. You can select C# or Visual Basic from the drop down.
Let’s select C# and click on OK button.
Another tab will open up with the code of the Recorded Test.
The C# code for the recorded step is in following structure.
- Namespace: The namespace in this program is your Test Project Name.
- Class: The class name here is the test name of your Test Studio test
- Method: Method is defined as searchBing_CodedSteps_CodedStep()
The CodedStep attribute that is defined in the coded step is the anchor point for the Test Studio. It allows it to recognize custom coded step in the Test, if the codedStep attribute is removed, Test Studio will not be able to recognize the custom method.
Now let’s get back to the searchBing_CodedSteps tab and see how Test Studio recognizes custom coded test methods.
If you click on the coded step that you have just added, a drop down will appear containing the method defined in the tstest.cs. We don’t need to change function as we are editing an existing step, so the Test Studio will select the function by default.
We have already seen the CodedStep attribute defined in the tstest.cs. This attribute allows Test Studio to recognize the adjacent method as a custom-coded method. Without an attribute definition, Test Studio will ignore a method.
Writing a coded step
Let’s consider the test case we have used.
- Open bing.com
- Provide a search string
- Click on search button
- Validate that the first result contains the search
- Navigate back to the previous screen.
For the Step 2, suppose the textbox in which the string is to be entered, already contains some default data. In that case, we will need to clear the textbox first and after that, we can enter the search string. Let’s write a simple clear step just before the SetText so that the text box is empty before we start putting data inside it.
Here, we are setting the Text in the text box as an empty string. We are recognizing TextBox element by its ID Find.ById(“sb_form_q”). This posts an empty string into the textbox to clear its content and then it will add the Search string to it.
Running the test with the coded Script. The test was executed and pass successfully.