Skip to content

amirh3sam/PostManAutomation_LocalHost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 

Repository files navigation

image

✔️ PostMan Automation

To set up a free server effortlessly, you can generate the server by executing a few lines of code in the command prompt. To learn how to create your local server for testing HTTP API requests, visit this GitHub page The GitHub Page or you can download it here Download zip file HERE , after you download the zip file extract it to desktop after open the folder rightclick somewhere and choose open in Terminal then type npm i Enter Then Type npm run dev Enter wait to see on Green that Running mean server is ready :)

image

With this server, you can conduct tests involving GET, POST, PUT, PATCH, and DELETE methods on data name, gender, age. Begin by establishing a database on your local host to assess GET and POST operations for data creation. You can also utilize this server to DELETE or UPDATE your database as needed. Once the server is up and running, you can proceed to Postman for automated testing.

after you create the server now we can do some testing and verification on responses ➡️ http://localhost:3000

if for a reason your 3000 PORT was used by another project, you can easily change that PORT to 3001 or any open port, to do that you need to open the Project and on package.json there is a dev object change that PORT=3001 and save and run again!

image


📌 STEP 1: Establish a workspace and assign it any name you prefer, such as "MyAutomationAPI."

➡️ now need to create a Collection and give a name like APIPractice )

after you click on APIPractice you can see the Tab called "Variables" and click on it.

📌 STEP 2: Under VARIABLE column type → heroUrl

Then under CURRENT VALUE – > http://localhost:3000

INITIAL VALUE → is the URL that you used to share it with others

see other type of variable :

  • Global variable

  • Collection variable

  • Environment variable

  • Data variable

  • Local variable


📌 STEP 3: image

our first request: GET → save then run it

📌 STEP 4: Verification: Click on the Test tab → then on the right side you have some methods that you can pick Inside Test you need to type JS code

Click on image

→ to verify status code.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

For checking other status codes: Just need to change that 200 inside the () to any other status code

after clicking on "Send" gonna see under Test Results that shows a pass

image

📌 STEP 5: → to verify headers: image

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

→ if you want to verify for example Date is present in the header just need to copy and paste same code and change instead “Content-type” chenge to “Date” then click Send

pm.test("Date is present", function () {
    pm.response.to.have.header("Date");
});

Is not case sensitive instead Date you can type the date

Now want to verify content type is application/json now in the second line after “Content-Type” add a comma and type “application/json” image

pm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type","application/json; charset=utf-8");
});

Another thing wants to check header Transfer-Encoding is chunked:

pm.test("Transfer-Encoding is chunked", function () {
    pm.response.to.have.header("Transfer-Encoding", "chunked");
});

Now you can see we have 5 Tests that all Pass : image

📌 STEP 6: For POST HTTP Request : POST: http://localhost:3000/addHero on the body tab Choose Jason Then add value then after sending you're going to get a 201 Status code! as you see in this example we added one json file to our database

image

Now go to the Test tab

Check status code

image

Now from the right side click on Respond body: Json value check

image

Now need to change in third line value to msg and 100 to "Your hero successfully added to the database." image

##Create Json object:

var jsonData = pm.response.json();

To print the name on the console:

console.log("Hero name: " +jsonData.data.name);

Also age and gender of specific ID:

console.log("Hero age: " +jsonData.data.age);
console.log("Hero gender: " +jsonData.data.gender);

image

on the console, we can see the result : image

📌 STEP 7: Click on the Collection that allows you to use PUT HTTP request, Right click and create a new request: PUT: http://localhost:3000/heros/:id

Change the age number to something different from any user id then save your change and send get 204 status code image

Now click on Test

Click on the status code and change it to 204

At this point, we need to capture the ID from the POST request

We going back to our POST request and Test tab need to save our json data into global variable

On the right side click on: image

image

Then save it.

Now we go PUT request in Path Param and add id with global value

image

and in the Body tab, we can change the age to a different value and save and run;

image

Now want to delete that Global variable that we create with post and edit it with Put: image

we can create another request to see if the delete works fine or not : image

On the Test tab check the status code and error message

image

We are using static post Hero name right now. We want to get dynamic name to post and verify that name is posted.

In order to use random firstname in our post request, we used {{$randomFirstName}} in request body.

But the problem is, that we cannot verify after we post the name in the Tests field in Postman.

To overcome this problem.

we used the Pre-Request Script tab from Postman.

how can we use {{$randomFirstName}} in pre request script ?

var name = pm.variables.replaceIn('{{$randomFirstName}}');

➡️ we set HERO_NAME global variable from name variable

pm.globals.set("HERO_NAME",name);

image

!image

** ✔️ Lets Summmery the FLOW ➡️ **

✔️ First Pre-Req Scrip

✔️ Then API Request

✔️ Then API Response

✔️ Last Test

✔️ Set global name Then after the request and response are complete, under the Test tab, we read the Global variable HERO_NAME and assign it as an expectedName

image

Run on postman CLI need to install :

https://go.pstmn.io/install-cli-win64

then with command line depends on your operation system and how it works Typing code on the command line can execute tests. Copy past the API key on the command line and run!

Or

✔️ Run on CI/CD pipeline

You can pick Jenkins and run it !

https://learning.postman.com/docs/collections/using-newman-cli/integration-with-jenkins/

I hope you guys enjoying this Topic, Good luck!!✌️

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published