API testing with DevWeb

Your DevWeb scripts can be used for API performance testing. To shorten scripting time, you can use the DevWeb Offline Script Generator to convert Swagger API documentation into JavaScript functions, providing a format that is easy to read and maintain. The generated functions can be called from DevWeb scripts.

Note:  

  • This feature is supported from version 2020 SP2.
  • This feature is provided as tech preview.

About API functions with DevWeb scripts

Swagger is an open specification used to describe and document RESTful APIs.

To enable API testing with DevWeb scripts, LoadRunner Developer provides a method to convert the API calls in Swagger definition files (swagger.json format) into functions that can be called from DevWeb tests.

This functionality is supported with Swagger Specification 2.0.

Using Swagger mode to generate swaggerApi.js

You use the Offline Generator in Swagger mode to generate the required files. When you add the path for a swagger.json file as an argument to the Offline Generator, the resulting output is a JavaScript file, swaggerApi.js, containing auto-generated JavaScript functions.

Each function includes parameters, and represents an API call in the Swagger definition file. The function can connect with the server to execute the operation described by the API call.

After generating the swaggerApi.js, you can import the file to the folder for a DevWeb script. From the script, you can then call the function you want to test, using the required parameters. Implementation is inside the swaggerApi.js. When the function is called, it generates an API call on the application, and returns a web request.

Using Swagger mode with a HAR file

Your swagger.json may be very large, with hundreds of operations, and you may not want to test them all. Instead, you have the option to directly generate a DevWeb script based on a HAR file, containing just the relevant operations. This is the recommended way to create your API test script; it is quicker and easier than editing a script manually to call the functions.

When you add the HAR path (and the swagger.json path) to the arguments for the Offline Generator using Swagger mode, the tool generates a main.js file containing the relevant function calls, in addition to creating the swaggerApi.js file.

The creation of each function call in the main.js is based on the Offline Generator finding a single, unique match between the HAR entry and the generated JavaScript function in the swaggerApi.js. If there is a match, it creates a function call for that function. If a match is not found, or there are multiple matches, it generates a regular DevWeb WebRequest send call.

Back to top

Example

A pet store application is described using the pet_swagger.json file. The JSON contains various different operations for the pet store.

pet_swagger.json file:

The JSON section below defines an API operation for adding a pet to the store, with operationID "addPet". It should be called via a POST request with an argument in its body. The parameter name is "body".

"/pet": {
            "post": {
                "tags": [
                    "pet"
                ],
                "summary": "Add a new pet to the store",
                "description": "",
                "operationId": "addPet",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json",
                    "application/xml"
                ],
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "description": "Pet object that needs to be added to the store",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/Pet"
                        }
                    }
                ],
                "responses": {
                    "405": {
                        "description": "Invalid input"
                    }
                },

 

swaggerApi.js file:

When the Offline Generator generates the swaggerApi.js file, it contains the corresponding generated function for addPet. It has a parameter named body, as used in the JSON file, and the parameter description from the JSON file.

The function creates a WebRequest to call the API, and returns the request. In the body, the function used the parameter.

main.js file:

When the Offline Generator generates the main.js file, it contains a call to the addPet function in the swaggerApi.js, using the value taken from the HAR file. The function uses the returned WebRequest and sends it. The return value of the send is a WebResponse, which is assigned to a variable.

Back to top

Parameters

  • Swagger describes several locations of function parameters: Path, Headers, Query, Form Data, and Body.

    The generated function accepts the parameters defined for the operation in the swagger file, and used in the function body, inside the generated WebRequest. For example, Path parameters are used in the WebRequest URL, Query parameters are used in the WebRequest query.

  • A parameter has a required property in its schema which can be equal to true or false. All parameters are generated, whether required or not. The Offline Generator ignores this property.
  • The functions getSchema and getHost (that return the Schema and Host parameters respectively) are used in the WebRequest URL.

Back to top

Generate files from Swagger definitions

The following steps describe how to generate swaggerApi.js and main.js files using Swagger mode.

To generate files from Swagger:

Prerequisite: A swagger.json file created with Swagger Specification 2.0

  1. If you want to generate a DevWeb script for specific API operations in Swagger, record and create a HAR file (refer to Using Swagger mode with a HAR file, above). Record while invoking the API calls you want to test.

    Use the Proxy Recorder or another recording method - for details, see Record and generate a DevWeb script.

  2. Run the Offline Script Generator from the command line or from your IDE, with the following arguments.

    To generate the swaggerApi.js only:

    OfflineGenerator.exe –mode=swagger <path to swagger.json> <path to result folder>

    To generate swaggerApi.js and main.js (using HAR file generated in step 1 above):

    OfflineGenerator.exe –mode=swagger -level=pages -har=<path to har file> <path to swagger.json> <path to result folder>

    For more information on using the Offline Generator, see DevWeb Offline Script Generator tool.

Back to top

See also: