How to use variables and arrays for dynamic comparisons

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:

  1. Create an automated test case with the following steps to navigate to the website and retrieve the value of the footer:
    1. Use Goto_URL command to navigate to https://reference.subject-7.com
    2. 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:

mceclip0.png

 

(Execution)

mceclip1.png

(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. 

 

  1. 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. mceclip0.png
  2. Create a test case with the following steps:
    1. Set variable zip_code=20009 (For Dupont Circle)
    2. 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.   
  3. Set variable whitehouse_weather (For Whitehouse)
  4. Same as step as 2  but the variable is @whitehouse_weather
  5. Compare the values read in step 2 and 4. 

mceclip1.png

mceclip2.png

mceclip3.png

mceclip11.png

 

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:

mceclip4.png
mceclip5.png
(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)mceclip14.png

(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. 

mceclip0.png

(Creating database connection, we use Postgres in this example)

mceclip6.png

(Defining set_list steps)

mceclip7.png

(Defining the loops)

mceclip8.png

(Defining the conditional statement)

mceclip10.png

(Overall Test Case view)

Screen_Shot_2020-01-21_at_3.40.26_AM.png

(Test Case after local execution )

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.