Migrating Postman Test Scripts To Thunder Client


In my last post I reviewed a few different REST client alternatives, that are not Postman. As part of my migration in tooling, I wanted to demonstrate the changes you may need to make to your test and pre-request scripts.

Thunder Client has a UI to do more declarative tests. I won't be delving into those, and I think they are pretty self-explanatory based on what selections you can make.

One point to make - when you import your Postman collection, Thunder Client will attempt to apply a transformation of your code to suit its environment, however in my experience I found it didn't catch everything - so more than likely you will need to do a manual review.

How does a Postman test script look?

One of my number one scenarios is using OAuth request flow, and putting the access token into my environment so I can do authorized requests without having to manually copy values around.

In Postman, a typical test script looks like this:

const oauthResponse = JSON.parse(responseBody);

pm.test("access_token returned", function() {
    pm.expect(oauthResponse.access_token).to.be.ok;

    // We got an access token, so now push it onto our environment.
    pm.environment.set("access_token", oauthResponse.access_token);
});

This essentially does two actions.

  1. Sets up a test to see if there was an "access_token" field in the response
  2. Updates the environment with its value

And an equivalent test script in Thunder Client

You may not be surprised, but while postman has a global `pm` object, Thunder Client has a global `tc` object.

Key differences you need to make to your code:

  • Use tc instead of pm
  • There is no global variable responseBody. Instead you use tc.response.json (if you want the raw text value, you can refer to tc.response.text.
  • Instead of pm.expect, you can just use the global expect. Another option for tests is for the anonymous function to return a boolean value.
  • Instead of pm.environment.set, you use tc.setVar.

With those highlights in mind, our new script becomes:

const oauthResponse = tc.response.json;

tc.test("access_token returned", function() {
    expect(oauthResponse.access_token).to.be.ok;

    // We got an access token, so now push it onto our environment.
    tc.setVar("access_token", oauthResponse.access_token);
}); 

After you execute your request, the right pane has a Results tab which indicates the success or failure of any test you added to your request.

For more information on these, you can refer to the Thunder Client support GitHub repo: https://github.com/rangav/thunder-client-support

Particularly,

Pre-Request Script in Postman

In the past, I've used a pre-request script to randomly populate some data for an API I was developing so each time a make the request I do not need to worry about the input data.

To accomplish this I had something like the following script:

const requestBody = JSON.parse(pm.request.body.raw);

const emailUserList = [
    "arun.padilla",
    "rajeev.gabler"
];

const emailPrefix = emailUserList[Math.floor(Math.random() * emailUserList.length)]

requestBody.user = `${emailPrefix}@${requestBody.customer}.com`;

pm.request.body = {
    mode: "raw",
    raw: JSON.stringify(requestBody)
};

Can we do the same in Thunder Client?

From my testing, you cannot modify the request body in this way. The workaround however is that you can reference variables in your request body - so your pre request script would instead just set variables rather than modifying the request body directly. This is probably a cleaner approach in any case.

With this new approach in mind, the script will looks like this:

const emailUserList = [
    "arun.padilla",
    "rajeev.gabler"];

const emailPrefix = emailUserList[Math.floor(Math.random() * emailUserList.length)]

tc.setVar("user", `${emailPrefix}@example.com`, "local"); 

I decided to use the "local" space for this data so as not to clutter up my environment with temporary data. 

The corresponding request body example is:

{
    "user": "{{user}}"
}

In Thunder Client, pre-request scripts are configured under the tab "Pre Run". 

Credits

Opening Splash Photo by MichaƂ Mancewicz on Unsplash

Popular posts from this blog

Report row buttons firing a dynamic action

Accessing the last request value from a page submission

Installing Oracle Instant Client on Ubuntu