By using the Web API call command, you can run the Web API (REST API) in your test script. The Web API calls support calls to the HTTP GET / POST / PUT / DELETE methods.
Table of contents
- GET method
- POST/PUT/DELETE methods
- Specifying Status Code
- Note on setting “Content-Type” in the Header key
- 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 below.
To simplify the explanation, we have removed a great deal of the information from the actual API response.
{
"results": [
{
"name": {
"first": "levi",
"last": "jones",
}
}
]
}
First, click the Edit button.
In the dialog, specify GET for Method and https://randomuser.me/api?format=json for API URL.
Furthermore, in the Result tab, 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"]".
Values set as variables can be referenced with $(variable name) in subsequent commands in the same test case. 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 as below.
- The saved variables are only valid within the same test case. They cannot be used in other test cases.
- When you want to specify an HTTP request header, specify the Header tab in the dialog box.
- 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 below. The body value can be either form-data format (POST method only) or raw data format (text format).
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.
Note on setting “Content-Type” in the Header key
If the first letter of Content and Type are in lower case, it may fail with a 400 BadRequest error; please capitalize the first letter of Content and Type.
OK: Content-Type
NG: content-type, Content-type, content-Type
In the case of 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 easily resolved, so please contact MagicPod support with information about the URL domain to which you are trying to connect to. When it is necessary to use URLs including unicode characters, etc., these can also be treated as query character strings.