API Testing in DevWeb using Swagger

Last month, Micro Focus had released its newer version of LoadRunner products. You can check it out my last article about LoadRunner Professional Service Pack 3. This blog post covers about one of the new feature in LoadRunner 2020 SP 3 – API Testing in DevWeb using Swagger.

What is DevWeb?

DevWeb (tech preview version was TruWeb) is a new protocol offering from Micro Focus to performance test applications at HTTP transport level and WebSocket.

The standalone offering is known as LoadRunner Developer which is a cross platform across Windows, Linux and macOS, uses lightweight JavaScript SDK and scalable.

If the above link didn’t work, please click here to checkout with the discount code.

What is Swagger?

Here is the definition from Wiki:

Swagger is an Interface Description Language for describing RESTful APIs expressed using JSON. Swagger is used together with a set of open-source software tools to design, build, document, and use RESTful web services. Swagger includes automated documentation, code generation, and test-case generation.

Swagger was developed by SmartBear Software.

API Testing in DevWeb using Swagger

LoadRunner Professional 2020 SP 3 supports API testing in DevWeb using Swagger Specification 2.0

All you need is to import the swagger json file in DevWeb, which generates swaggerApi.js file under Extra Files as shown below.

API Testing in DevWeb using Swagger
API Testing in DevWeb using Swagger

swaggerApi.js contains all the JSON definitions which will get called from Main. Below is the getPetById definition from this Pet Store Swagger.

/**
* Find pet by ID
* @param {petId} ID of pet to return
**/
function getPetById(petId){
    const webRequest0 = new load.WebRequest({
        url: `${this.$.getSchema()}://${this.$.getHost()}/${this.$.getBasePath()}/pet/${petId}`,
        method: "GET",
        headers: {
            "accept": "application/json,application/xml"
        },
    })
    return webRequest0;
}

How to import the swagger file in DevWeb?

Create a new DevWeb script, go to Record > Import API File as shown below.

Import API File - Step 1
Import API File – Step 1

Click on Browse and browse the downloaded json file as shown below and then click in Import. This will import all the definitions into swaggerApi.js file.

Import API File - Step 2
Import API File – Step 2

The best way to generate DevWeb script is using HAR file.

How to generate script from swagger definitions?

There are two ways you can generate the script:

  1. Using HAR file
  2. By recording
  3. Using OfflineGenerator.exe

Using HAR File

As we are using Pet Store swagger, let us capture the get pet id request and save it as HAR.

Launch https://petstore.swagger.io/#/pet/getPetById in your favorite browser and bring up the Network tool. I am using Firefox to save the request as HAR.

Save as HAR
Save as HAR

Now back to VuGen, click on Record button and browse the saved HAR file using the below settings and then click on Start Recording.

Start Recording
Recording using HAR

swaggerApi.js will have all the operations from json. Generate DevWeb script either using HAR or Recording or Offline Generator.

This will analyze the traffic and generate respective Main file for getPetById as shown below.

// This script was generated and reflects raw data. It is recommended to change this code to your required logic

const swaggerApi = require('./swaggerApi.js')(load);


load.initialize("Initialize", async function () {
});

load.action("Action", async function () {
    load.WebRequest.defaults.returnBody = false;
    load.WebRequest.defaults.headers = {
        "Accept": "application/json",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.5",
        "Connection": "keep-alive",
        "DNT": "1",
        "Referer": "https://petstore.swagger.io/",
        "TE": "Trailers",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"
    };

    let webRequest1 = swaggerApi.getPetById("12");
    const webResponse1 = await webRequest1.send();


});

load.finalize("Finalize", async function () {
});

If all goes well, the replay will be successful as shown below.

Successful Replay
Successful Replay

By recording

Now let us generate the getPetById request by recording. Click on Record button and configure as shown below and then click on Start Recording.

Start Recording
Start Recording

In the browser, just perform the getPetById request, once the recording is complete, go to Main to see the captured call.

// This script was generated and reflects raw data. It is recommended to change this code to your required logic

const swaggerApi = require('./swaggerApi.js')(load);


load.initialize("Initialize", async function () {
});

load.action("Action", async function () {
    load.WebRequest.defaults.returnBody = false;
    load.WebRequest.defaults.headers = {
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "en-US,en;q=0.9",
        "dnt": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
    };

    const webResponse1 = new load.WebRequest({
        id: 1,
        url: "https://petstore.swagger.io/",
        method: "GET",
        headers: {
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "cache-control": "max-age=0",
            "sec-fetch-dest": "document",
            "sec-fetch-mode": "navigate",
            "sec-fetch-site": "none",
            "sec-fetch-user": "?1",
            "upgrade-insecure-requests": "1"
        },
        resources: [
            "https://petstore.swagger.io/swagger-ui.css",
            "https://petstore.swagger.io/swagger-ui-bundle.js",
            "https://petstore.swagger.io/swagger-ui-standalone-preset.js",
            "https://petstore.swagger.io/favicon-32x32.png",
        ],
    }).sendSync();

    const webResponse2 = new load.WebRequest({
        id: 2,
        url: "https://petstore.swagger.io/v2/swagger.json",
        method: "GET",
        headers: {
            "accept": "application/json,*/*",
            "referer": "https://petstore.swagger.io/",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin"
        },
    }).sendSync();

    let webRequest3 = swaggerApi.getPetById("14");
    const webResponse3 = await webRequest3.send();


});

load.finalize("Finalize", async function () {
});

Using OfflineGenerator.exe

Navigate to the LoadRunner installation location typically under C:\Program Files (x86)\Micro Focus\Virtual User Generator\bin\DevWeb and issue the below command to generate the script using the OfflineGenerator.exe program.

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

E.g.

OfflineGenerator.exe -mode=swagger -level=pages -har="C:\Users\Downloads\petstore.swagger.io_Archive [20-11-05 02-36-37].har" C:\Users\Downloads\petstore.json C:\Users\Documents\VuGen\Scripts\Swagger-Demo\

Conclusion:

DevWeb makes easy to test the API performance by leveraging Swagger specification. It expedites the process of creating scripts and helps in shift-left. Great power comes with great responsibility. After creating scripts, it is our responsibility to parameterize, handle errors, implement logic etc.

API Testing in DevWeb using Swagger

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn Kubernetes and Get Certified in CKAD.SHOW ME HOW
+ +
Share via
Copy link