POSTMAN PRACTICE-2

Simge Ş.
4 min readOct 16, 2022

--

This story will be another step for Postman practices.
Have you ever heard “Chai”? Maybe in a name of tea ☕ Or a library?
Get your cup of tea and follow the path together as we did previous story 🎉

  1. If you haven’t checked Postman Practice-1 , it is critical to take look at it before reading this story.
  2. Now, let’s direct to “Create Board” call’s Test section.
  3. Initialize String variables with “let”.
let name = "yourname";
let envName = "yourname";
let surname = "yourSurname";

4. In first story , we practiced how basic test is applied to API call. Now we are gonna deal with some other basic tests. Let’s add below codes after initialized variables.

pm.test("name equals to envName",function(){pm.expect(name).to.eql(envName);})pm.test("name equals to surname",function(){pm.expect(name).to.eql(surname);})pm.test("name is not equal to surname",function(){pm.expect(name).is.not.to.eql(surname);})

5. Now you are ready to save it and press Send Button.

Figure 1. Test Result of First Case

If you see same results as in Figure 1, you completed first case of writing tests 🤩 . If not , please check steps again.

I’ve added informations next to the tests to explain how the functions work.Well , it s not that hard to understand when we look at the code in functions. The below test code has syntax like English and we can say that it tests name equals to envName :

pm.expect(name).to.eql(envName);

Chai is an assertion library that provides functions and methods that help you to compare the output of a certain test with its expected value. Chai provides clean syntax that almost reads like English. Therefore we can write our tests easily, right ? 🧑‍💻

6. Okay after encouraging you 👌 , let’s check some other test functions.

Figure 2. Json Parse and Test Case
let response = pm.response.json(); // initialize response variable
console.log(response); // show the response in console

Here the response is JSON object which is parsed from response. “pm.response.json()” returns the response body of API request as an object in JSON format. In this case, we can use variables which request body has.

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program.

Hmmm 🧐 , now we know that response variables are reachable. In previous story, we learned variables can be set in environment. Yeah , you think what I think.

👉 Here it is : we can set our variables in environment according to parameters before we send request , then we can test the response.

Figure 3. Pre-request Script sample

7. As you see in Figure 3, I set environment boardName variable as a dynamic random name. In this case , I do not need to change name of board manually. The only thing I should do is changing name of parameter :

Figure 4. Change name value as Environment variable name

8. 🏃‍♀️ Save and run. You see the new environment variable in your environment section.

Pre-request script execute JavaScript before a request runs.

Therefore, before request it is known what the boardName is and it will be saved in environment.

Figure 5. boardName is set in environment

9. Time to test setting variable ⚡ In test section there was a test which we control our board name but it was not dynamic. We need to get our dynamic name from environment by pm.environment.get() code.

Figure 6. Test with Getting Environment Variables

In test results, first test failed and second one passed because our boardName is not MediumTrello anymore, it set as dynamic value. And it generates random names when we request this api call.

By the way, if you get this

“message”: “Board must be in a team — specify an idOrganization”

you should know you have created limited number of boards, you need to delete some of them.

Let’s review what we’ve practiced in this story :

Chai assertion library

Pm Object - test

Tests with pm.response.json() , console.log()

✅ Set environment variable in pre-request script

✅ Tests response data by getting environment variable

☕ Alright , I linked some words for you to check resources. Another link is for recipe of Chai Tea☕.

✨I hope you enjoyed while reading this story and drinking your cup of tea. Next, there will be story about another step of postman.✨

Previous story

--

--

Simge Ş.

Software Automation Test Engineer - ISTQB Certificated