Abstract
Oftentimes, testers would like to read a value off the screen, off of a REST call or maybe from a database table and do comparisons. This article describes variables and templates as well as commands to use. Please note the hyperlinks in this tutorial point to reference documentation as well as download material.
Variables & Data Templates
Static Variables (Data Templates)
If prior to testing execution we know what value will be used for a given field, then we refer to it as static. For example, suppose you have a registration form that you want to automate, you don't want to hardcode the data (fname, lname, username, etc). Then you will create a data template sat registration_form and define fields fname, lname, and username. These fields are placeholders for the values that are passed as data_sets to the test prior to the execution. Once the template is defined, you can use it throughout any command that takes a value like Fill, Goto_Url, Wait, VerifyText, etc. As well as use it in any of the artifacts. For example, in the XPath of a given locator or in a Rest Connection's URL, Password, etc. To access a data_template field use ${template_name.field_name} so in our example ${registration_form.username}
To summarize, generally for data-driven testing, or if you know the values that are going to be used in a test case prior to the execution ( or you want to avoid hard-coding) use data templates.
Dynamic Variables
Sometimes the value that you are dealing with is dynamic or not known prior to the execution. For example, a value read from a table on a website or a value return by being filled in a JSON response. These variables are referenced with "@" so, for example, @var can be used in any commands that takes a value or any artifact field.
Compare vs Verify
In Subject7, there are a few ways you can do verification. The main two are commands like Verify_Rest, Verify_DB or you use variables to read what you need and then use the command Compare. Verify commands are one-liners and usually do just a quick check. However, Compare is a very flexible command where you can compare values from various sources. You can read from a database table and compare values to a JSON string or values read from a web page.
Examples
In this section, we will illustrate a few useful examples. In short, we will be reading values from various sources (Web, DataBase, Rest Response) and Compare them with different values (static, templates, other variables, etc). We will be using commands like get_web_value, get_db_value, get_rest_value, etc. Then we will be using the compare command to make verifications.
Reading a Value from Subject7 Reference Footer
Suppose you want to get the value from the footer on Reference site (https://reference.subject-7.com) which shows the version and last update. You can download the example here and import it into your own account. Let’s do this in Subject7:
- Create an automated test case with the following steps to navigate to the website and retrieve the value of the footer:
- Use Goto_URL command to navigate to https://reference.subject-7.com
- Use Get_Web_Value to find the footer using its XPath below and retrieve its value (using HTML tag with attribute = TEXT) where the value is stored in @footer.
XPath: //p[contains(text(),'Social Network')]
Here are some screenshots that show your test case as well as execution:
(Execution)
(Execution which shows that the XPath was used to locate element and value has been retrieved and saved under @footer).
Reading Rest JSON values Example
Suppose you want to check if the weather forecast of the close-by Zip codes is within range of each other. For example, you want to see if Dupont Circle (Zip Code 20009) and The White House (Zip code 20500) both located in Washington DC, have the forecast. There is a public Rest API that can be used as follows:
https://samples.openweathermap.org/data/2.5/weather?zip=20009,us&appid=1
The response for Dupont Circle Zip Code 20009:
{
"coord": {
"lon": -122.09,
"lat": 37.39
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],.....
Rest Get method for The White House Zip Code 20500:
https://samples.openweathermap.org/data/2.5/weather?zip=20500,us&appid=1
Response:
{
"coord": {
"lon": -122.09,
"lat": 37.39
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
As we can see both responses have "Rain" in their main weather attribute. You can download the example here and import it into your own account. Now let's do this in Subject7.
- Create a Rest Connection under artifacts and use a dynamic variable @zip_code. Now every time you want to use this connection, you need to set the variable before.
- Create a test case with the following steps:
- Set variable zip_code=20009 (For Dupont Circle)
- Use Get_Rest_Value command to read the value for the JSON field main inside the weather filled into @dupont_circle_variable. The JSON path is weather.main.
- Set variable whitehouse_weather (For Whitehouse)
- Same as step as 2 but the variable is @whitehouse_weather
- Compare the values read in step 2 and 4.
Reading REST Headers Values example
Suppose you want to record the time in which the weather was pulled for both Zip codes (in the example above) which appears on the response headers of the REST call. You can download the example here and import it into your own account.
Add a step to the example used above which retrieves the date value from the response header:
Use Get_Rest_Value command to read the value from the response header field ‘date’ inside to be filled into @date_and_time. The header path is date. Add a step after each zip code which retrieves the date and time. Here are some screenshots of the step and execution:
(Defining two steps to get value from ‘date’ header after each step that retrieves weather so that each Zip code has its own retrieval date and time)
(Executed steps which show date and time for each Zip code)
Reading Database Values example
Suppose you have a table called users:
id | username | firstname | lastname | enabled
----+----------+-----------+----------+---------
1 | jsmith | James | Smith | t
2 | jbond | James | Bond | t
3 | mangelo | Michael | Angelo | t
4 | bpitt | Brad | Pitt | t
(4 rows)
Let's assume you have the same data in a CSV. You can upload it to a data template and name it users_reference_list. Now the idea is to loop through both lists and see if there are any discrepancies. You can download the example here and import it into your own account.
First, we use set_list to read both sources (database and reference list) in a variable that is essentially a two-dimensional array. We need to define the database connection and a sample query Then we have 2 loops and we do comparisons. Here are some screenshots of the test case and its steps.
(Creating database connection, we use Postgres in this example)
(Defining set_list steps)
(Defining the loops)
(Defining the conditional statement)
(Overall Test Case view)
(Test Case after local execution )
Comments
Please sign in to leave a comment.