Recording and Play Back has its own restrictions and sometime it may not be able to automate complex scenarios, in such cases coded steps are needed. Telerik supports C# and VB script, so it is advisable to have basic knowledge of these languages before jumping into coded tests. In this post, we will be using C# for automating test cases.
Before we begin writing our first coded test case, please go through following posts to understand the basics of coded test in Test Studio:
Create a new test case and add a coded step.
Double click on the coded step to open it.
Inside the [CodedStep(@”New Coded Step“)] test method we can write our test script.
Let’s first select a test case that we want to automate. For now, we are considering the same test case that we used for our first test recording.
Let’s list down the steps for the test scenario:
- Open Browser
- Open http://demoqa.com/
- Click on Contact Tab
- Enter your Name
- Enter Your EmailEnter Subject
- Enter Your message
- Click on Send
- Verify that the message was sent
Steps to write coded Test case in Telerik Test Studio
1. The first step is to open a browser.
For opening a browser we don’t have to write any specific code, during the test run Telerik asks the user to select a browser for executing the test. So, any web test in Test Studio automatically opens up the desired browser before starting execution.
So, lets move on to the next step.
2. Open http://demoqa.com/
This step requires us to enter the URL in the browser and open demoqa.com website.
In the Test method, Type ActiveBrowser following by “ . “ – the intellisense will appear displaying a list of options.
Select NavigateTo with opening bracket, the Telerik will now provide you with overloads for NavigateTo.
Using the first overload, pass the URL of the website as a string.
Once the test step is complete, compile the code.
So, we have a successful build. Let’s run the test.
So, our first test step worked and it opened the website as we wanted. So, let’s move to the next step.
3. Click on Contact Tab
The next step requires us to click on the contacts tab. When we want to perform an operation on any object present in the webpage, we first need to identify the location of the object.
Let’s first locate properties of the “Contact” object.
By using “F12” and extracting properties, we will be able to retrieve properties of the object.
We will first define an Element by the unique identification property. Here we are locating the element by using its Xpath.
Once we have defined the element location, we will perform “Click” action.
4. Enter your Name
The fourth step requires us to enter our name in the “Your Name” text box. Here, we will first retrieve the location or properties of the textbox then we will enter our name.
We have a unique name value for the textbox, we will use that for defining our element. (name = “your-name”)
But before we define the element, provide some Wait time to allow the page to load properly. We will provide a wait of 1 sec.
As you can see in the below image, Sleep() accepts values in 32 bit Integer format and the values are in a millisecond. So, we have set a value of 1,000 milliseconds.
So, once we have provided ample time for the browser to load page, lets again move back to our test step.
First, we will define the object by Name and then we will perform a Set Text operation.
Enter the arguments for the set text
5. Enter Your Email
6. Enter Subject
7. Enter Your message
For the test steps 5, 6 and 7 we will follow the same process as test step 4.
Here, we have defined all the elements using Name property but as applicable, you can use ids, Name, tagIndex, xpath etc. to define your element.
8. Click on Send
So, the next step in our test case is to click on send button. Lets again use the Xpath for finding Send button.
9. Verify that the message was sent
Let’s give some wait time for the browser and then we can proceed with the validation.
When message is sent successfully, following message is displayed in the browser.
So, we will verify if this element is present or not. To validate the element presence we will perform a simple Assert.
I have also added a message to the Log for displaying “element is present” message. So, if the all the test step passes the message will be printed in the log if any of the test steps fails then the message will be not be logged as Telerik will stop the execution at the failure point itself.
Let’s run the test case:
Let’s check the log by clicking on “View Log” link.