Use data from JSON files in GUI tests

Relevant for: GUI tests

Store test data for your application in a JSON file and have a single test script which can run tests for all the test data in the file.

This topic provides a tutorial on how to use data stored in JSON files in a GUI test.

Overview

When working with GUI tests, you can save input data in a JSON file data source. For example, if you want to test the account registration functionality of your web application, you can save multiple sets of credentials in a JSON file and retrieve the data from the file to your script for execution.

UFT One provides the Parse method to retrieve data from a JSON file. For more information about the method, see the JsonUtil Object in UFT One Automation Object Model Reference.

Back to top

Use a JSON file to test the registration page

Scenario

In this scenario, we'll test the registration page of the AdvantageShopping website.

Procedure

  1. Create a JSON file that contains multiple sets of user credentials, and save the file in your local file system.

    Note:  

    • To test multiple sets of data, make sure to store data in correctly formatted JSON arrays.

    • For the tutorial purpose, the example uses only three sets of data.

    {
    "users":[{
          "username":"remi.jullian",
          "password":"Aa123"
        },
        {
          "username":"remi.jullian",
          "password":"Aa1234"
        },
        {
          "username":"remi.jullian",
          "password":"Aa1235"
        }]
    }
  2. Create your test script.

  3. Add the following test steps to read data from the JSON file and test the account registration function.

    Browser("Advantage Shopping").Page("Advantage Shopping").WebElement("Username").Click
    ' Read data from the JSON file
    Set Dom = jsonutil.Parse("C:\Users\_ft_auto\Desktop\test.json")
    Set users = Dom.GetArray("users")
    ' Iterate through each set of data in the JSON file
    For Each user in users
        username = user.getValue("username")
        password = user.getValue("password")
        Browser("Advantage Shopping").Refresh
        Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("usernameRegisterPage").Set username
        Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("emailRegisterPage").Set "12345@163.com"
        Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("passwordRegisterPage").Set password
        Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("confirm_passwordRegisterPage").Set password
        Browser("Advantage Shopping").Page("Advantage Shopping").WebCheckBox("i_agree").Set "ON" 
        Browser("Advantage Shopping").Page("Advantage Shopping_2").WebButton("register_btnundefined").Check CheckPoint("register_btnundefined")
    Next
  4. Check the test result.

Back to top