If you use the “Web API call” (Figure 1) command, you can run the Web API (REST API) in a test script. As API calls are performed from the user PC, it is simple to call API(s) provided by the in-company server.
The Web API calls support calls to the HTTP GET / POST / PUT / DELETE methods.
Figure 1 “Web API call” command
Table of contents
- GET method
- POST/PUT/DELETE methods
- Specifying Status Code
- If you get an error “Currently the cloud test run does not support webApi command”
GET method
As an example, let us try accessing the RANDOM USER GENERATOR, which enables you to acquire different user information at random in JSON format, as shown in Figure 2.
To simplify the explanation, we have removed a great deal of the information from the actual API response in Figure 2.
{
"results": [
{
"name": {
"first": "levi",
"last": "jones",
}
}
]
}
Figure 2 JSON response of RANDOM USER GENERATOR
First, click the “Edit” button.
The Edit dialog box will be open. Then, specify “GET” for the method and “https://randomuser.me/api?format=json” for the URL.
Furthermore, in the “Result” tab shown in Figure 3, a variable is set to the value of the API response so that it can be used in subsequent processing. In the “Variable” column, specify the variable name, and specify the response value in the “Javascript” column. In the Javascript column, you can reference the response as a JSON object with
- "jsonResponse" for accessing the response body
- "responseHeaders" for accessing the response headers
and each item can be accessed using the Javascript notation, such as “jsonResponse["results"]” and "responseHeaders["Set-Cookie"]".
Figure 3 HTTP GET method call
Values set as variables can be referenced with “$(variable name)” in subsequent commands in the same test case. As shown in Figure 3, we can set the user's last name in variable "LastName" and their first name in variable "FirstName", and this can then be used to set a random user name in the input area, as shown in Figure 4.
Figure 4 Using the variables for the HTTP response
- The saved variables are only valid within the same test case. They cannot be used in other test cases.
- If you want to specify an HTTP request header, specify the “Header” tab in the dialog box in Figure 3.
- The query parameter of the GET method can be specified with the format “?<key 1>=<value 1>&<key 2>=...” after the URL.
POST/PUT/DELETE methods
The method for specifying the method name, URL, header, and response are the same as for the GET method.
Additionally, in the case of the POST / PUT / DELETE methods, you can specify the body of the request on the “Body” tab, as shown in Figure 5. The body value can be either "form-data format" (POST method only) or "raw data format" (text format).
Figure 5 HTTP POST method calls
Specifying Status Code
In the "Result" tab, you can specify the expected HTTP response status code. This allows you to execute tests even when the API call returns an error response.
By default, the Web API call is considered successful if a success status such as 200 is returned. When you select "Specified value", the Web API call is considered successful if the expected status code matches the actual response status.
If you get an error “Currently the cloud test run does not support webApi command”
On Cloud devices or external Clouds, the URLs that can call Web API are restricted, and you may see the error message “Currently the cloud test run does not support webApi command”.
This problem can be resolved, so please contact MagicPod support with the information about the URL domain to which you are trying to connect. As it is necessary to use URLs including Japanese, etc., these can also be treated as query character strings.