Data-driven testing with RESTful Web Service requests

Katalon Studio allows you to perform data-driven testing (DDT) with Web Service Objects.

This tutorial shows you how to create Web Service requests and test cases with variables, then bind data files to the associated request objects and test cases.

You can download the sample project here: Web Service Tests.

In our example, we want to send the user information with dynamic data, including username, password, age, gender, and avatar to the server to create an account, then return a user ID as the response. After creating a new user, we will find that user by the user ID.

Create Web Service request objects with variables

Here we need to create two RESTful requests with variables:

  • A POST request with variables to register a new user.
  • A GET request with variables to get a user by ID.

Create a POST request with variables

We use the POST request to send the user information, including username, password, age, gender, and avatar to the server to create an account, then expect a user ID as the response.

Follow these steps to create and configure the POST request:

  1. Create a Web Service request. From the main menu, select File > New > Web Service Request. In the displayed New dialog, name the request and specify the request type as RESTful request.

    New Web request dialog
  2. Specify the API endpoint. In our example, the API endpoint is https://sample-web-service-aut.herokuapp.com/api/users/json.

    POST request API endpoint

    To learn more about API endpoint management, refer to this document: API testing best practices.

  3. In the Variables tab, input username, password, gender, age, and avatar variables.

    POST Variables tab
  4. To call a variable in a Web Service object, use the ${<variable_name>} syntax as a placeholder in any of the supported locations.

    Here we use the syntax to specify the variables in the HTTP Body tab.

    HTTP Body tab

Create a GET request with variables

Next, we use the GET request to find a user by ID. We expect the server to return the user information, including username, password, gender, age, and avatar in the response.

Follow these steps to configure the GET request:

  1. Create a Web Service request. From the main menu, select File > New > Web Service Request. In the displayed New dialog, name the request and specify the request type as RESTful request.

    New Web request dialog
  2. Specify the API endpoint. Our API endpoint for the GET request is https://sample-web-service-aut.herokuapp.com/api/users/.

  3. To pass a variable to the GET API, add the ${<variable_name>} placeholder at the end of the API endpoint.

    Here we specify the id variable using the ${id} syntax as follows: https://sample-web-service-aut.herokuapp.com/api/users/${id}.

  4. In the Variables tab, input the id variable.

    GET request variables tab

Create a test case with associated Web Service requests

After creating two RESTful Web Service requests with variables, we create a test case to send the POST request to register a new user and extract the user ID from the response. With the returned user ID, we send the GET request to retrieve user information and verify it.

Follow these steps to set up the test case:

  1. Open a new test case. In the Script tab, enter the following code snippet to set up the test case:

    
    // Send a POST request to create a new user
    // The response contains the id of the newly created user
    post_response = WS.sendRequest(findTestObject('POST a new user', [('username') : username, ('password') : password
                , ('gender') : gender, ('age') : age]))
    
    // Extract the id value from the response
    user_id = WS.getElementPropertyValue(post_response, 'id')
    println("ID of user " + username + ": " + user_id.toString())
    
    // Send a GET request to retrieve user information by id
    get_response = WS.sendRequest(findTestObject('GET user by id', ['id' : user_id]))
    println("The response is: " + get_response.getResponseText())
    
    // Verify that the returned values match the user information
    WS.verifyElementPropertyValue(get_response, 'username', username)
    WS.verifyElementPropertyValue(get_response, 'password', password)
    
    
  2. To input variables into the test case, switch to the Variables tab and pass the variables defined in the POST request object.

    Here, we specify the username, password, gender, age, and avatar variables.

    Test case variables
  3. Run the test case and verify the test message in the Console log:

    Test case run result

Bind data files to the associated Test Case

After configuring the test case with associated Web Service requests, we bind the test case to the test data in a test suite.

Follow these steps:

  1. To create a test data file, from the main menu, select New > File > Test Data. In the displayed dialog, name the test data file and specify the data type.

    New Test Data dialog

    In our example, we use an Excel file that contains user information.

    Test Data table of user information
  2. Open a new test suite, click on the Add button and include the associated test case.

    Test Data table of user information
  3. To open the Data Binding section, click on the Show Data Binding button.

    Test Data table of user information
  4. You need to specify the test data file and configure the variable binding. Follow the steps in this guide to configure the binding: Manage Data Binding.

    Test Data table of user information
  5. Once configured, run the test suite and verify the result in the Log Viewer.

    Test Data table of user information

Notes:

For real-time monitoring and better reporting capabilities, consider integrating your project with Katalon TestOps. Learn more about test result reports here: Upload Test Results to Katalon TestOps from Katalon Studio.

See also:

Feedback