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 this example, we send user information with dynamic data, including username, password, age, gender, and avatar, to the server to create an account, then receive a user ID as the response. After creating the new user, we find that user by the user ID.
Starting with Katalon Studio 11.7.0, data-driven test execution requires a paid plan. See pricing page.
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:
-
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 a RESTful request.
-
Specify the API endpoint. In this example, the API endpoint is
https://sample-web-service-aut.herokuapp.com/api/users/json.
To learn more about API endpoint management, refer to API testing best practices.
-
In the Variables tab, input the
username,password,gender,age, andavatarvariables.
-
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.
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:
-
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 a RESTful request.
-
Specify the API endpoint. The API endpoint for the GET request is
https://sample-web-service-aut.herokuapp.com/api/users/. -
To pass a variable to the GET API, add the
${<variable_name>}placeholder at the end of the API endpoint. Here we specify theidvariable using the${id}syntax:https://sample-web-service-aut.herokuapp.com/api/users/${id}. -
In the Variables tab, input the
idvariable.
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:
-
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,
('avatar') : avatar
]))
// 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]))
// Avoid printing the response body because it contains the sample password.
// Verify that the returned values match the user information
WS.verifyElementPropertyValue(get_response, 'username', username)
WS.verifyElementPropertyValue(get_response, 'password', password)
WS.verifyElementPropertyValue(get_response, 'avatar', avatar) -
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, andavatarvariables.
-
Run the test case and verify the test message in the Console log.
Bind data in the test suite​
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:
-
To create a test data file, from the main menu select File > New > Test Data. In the displayed dialog, name the test data file and specify the data type.
In this example, we use an Excel file that contains user information.
-
Open a new test suite, click Add, and include the associated test case.
-
To open the Data Binding section, click Show Data Binding.
-
Specify the test data file and configure the variable binding. Follow the steps in Manage Data Binding.
-
Once configured, run the test suite and verify the result in Log Viewer.
See also: