Extracting an element text into variables and using it into your test. Sometime we need to extract a value from an object in the application and use it in some other step. There may be a scenario where you need to extract or copy some text from the application and verify it with some other text in a different module or a different page. Or there can be a scenario where you need to extract an element and perform some operation on it and then validate it with test data or element present in the different page.
There can be following scenarios, we will have a look on all:
- Using extracted text from one page to validate text in different page
- Extracting text from one page and entering extracted text into next page
- Extracting a text from one page and performing some operations on it. Then using it for validation or perform any other operation.
Using extracted text from one page and use it to validate text in different page
Let’s assume a scenario where you want to use an extracted text from one page and validate some text present in another page.
So, let’s have a look at the test case:
- Open following URL: http://store.demoqa.com/
- Note the name of the first product
- Navigate to Product Category menu
- Validate that the product is present
Let’s first create a test in the Telerik Test Studio. I am creating a test with name extractProduct_Validate you can use any name. After creating the test start the recording.
In the next pop-up enter the URL and start recording. The URL will open now, click on the Highlight element button in the recorder and highlight the Product name you want to extract. Keep the highlighter over the name for a few seconds, a mouse menu will open. Scroll down to extract text.
So, once we have clicked on the Extract text contains, we will have a step added to our test.
Now, we go to the element and select application object identifiers for this element. (For knowing more about elements and object identifiers please go through the following article :Right click on the element and select Edit in Live.
In the live, we will select element in such a way that it can extract any element (product name) from the product slider.
Here we are using only Tag Name and not using TextContent as Slider can have any product on their list, so using “tagname” will allow us to extract any product name from the slider.
Now, we will change the data bind variable name. In the right Properties panel, you will find a DataBindVariableName. This is actually a variable that will contain the extracted text. Telerik auto generates data bind variable names, but you can also change it as per your convenience.
We will change variable name to productName from IPodNanoBlueH2Tag.
Now, let’s continue with the test step.
Navigate to Product Category page and verify the product is present. Go to the first product and highlight it. Once the menu opens, go to Quick Step and verify the text contains.
A verification step will be recorded. Now, we will have to configure the object identifier for this element in such a way that it can verify all products.
Update the product element object identifier by “Edit in Live”. Use the object identifier that is common for all product names.
Now, let’s verify that if the extracted product name is available on this page or not. For doing that we will use an extracted string as an object identifier for the product name.
Go to the property of the product name element. This is the Find Element screen for the Product name element. The properties currently selected/used are common for all the product name, but Test Studio will pick-up the first product name for validation so, if something other than “iPhone 5” appears then this test will fail.
To make sure that the test is able to search all the available product name, let’s add a new object identifier, “TextContent” click on the “TextContent” from the left panel and it will be added.
But, it says “iPhone 5”, let’s change this to our data bind variable name. Remove iPhone 5 and click on the arrow beside the text value. A drop down menu will open, scroll to down, there will be a textbox with “{}” brackets in the left, write down your variable in the text box. This will allow Test Studio to fetch the object based on the text content in the variable.
We will enter productName in the text box. Click on the set button to save it.
Once complete lets run the test.
Extracting text from one page and entering extracted text into next page
In this scenario, we will be using text extracted from the page for entering a value into a textbox in another page. This scenario is useful when your application generates random/different text and you need to use this text at some other place within the application.
Let’s consider following test case for this:
- Open following URL: http://store.demoqa.com/
- Note the name of the first product
- Navigate to Product Category menu
- Type the name of the first product in the search field and search
The scenario is quite similar to the one we have used in the previous post “Using extracted text from one page and use it to validate text in different page” except for the last step where we are using the extracted product for a search. We will follow the same step as we followed in the previous post for step 1 to 3.
Let’s have a look at our step 4.Lets run the test till step 3, once it has run, recording panel will open in the browser. Type any text in the “Search” text box, Telerik Text Studio will record the text. We have typed “iphone 5” you can type anything and press enter button. Both the steps should be recorded.
Both steps have been recorded in the Telerik Test Studio.
- Typing Product name
- Pressing “Enter”
Now, click on the type product name step. Then click on the “Bindings” from the Properties panel at right hand side.
Now, click on the drop down Text,
You will find the data bind variable that we used in the previous step to bind the extracted variable is displayed here i.e. “productName”. Select it and click on set.
The step will now be bind with the variable and will be dataDriven, which means it will type the text that has been extracted in the data bind variable in the previous step.Let’s run the test, and see the result.
Extracting a text from one page and performing some operations on it. It then using it for validation or perform any other operation.
Let’s assume a scenario where you want to extract a text from one page and then performing some operations on it and then using it in some other page.
So, let’s have a look at the test case:
- Open following URL: http://numbergenerator.org/randomnumberbetween1and10
- Click on the Go button
- Extract the generated number
- Now, let’s validate that the square of the generated number is always between 1 and 100
Let us create a new web test and open the given URL. Let’s record and add steps up to 3 in the Test Studio. Add sufficient wait and change the object properties.
Here, we have created a new test “randomNumber”. We have added the step to open URL, generate a number and then extract the number.
Now, let’s extract this generated number into some variable. We have bound the extracted number into a data variable randomGeneratedNmbr
Now, let’s add a coded step to add number “10” into the randomly generated number.
To do that lets add a coded step to our current test.
To know more about how to coded step, please follow our previous post: http://original.toolsqa.com/telerik-test-studio/add-edit-coded-step/
At first, we will assign the extracted number to a variable. We will also define the data type for the extracted number/text. Here we have extracted a number so we are defining a data type as int but if you are performing an operation on strings, you can provide the data type as a string.
Below is the sample code for assigning a variable to an extracted value.
Now, lets use the above code in our coded step. In the below code snippet, we have added randomGeneratedNmbr as the data bind variable.
Once this code runs, it will assign the extracted value to variable nmbrExtract.
Let’s write a simple code to find the square of the extracted number then assert that the value of the square is always between 1 and 100.
squareNmbr will contain the square of the number.I have also added another code, to log the square in the test log.
Now, let’s run the test.
Let’s view the logIn the Log, we have the Log and the Square of the number that was extracted from the page.