Post API Request using Playwright

Tutorial 16​​ ​​ Post​​ API​​ Request​​ using​​ Playwright​​ 

What you will Learn​​ in this​​ blog:

  • Post​​ Request​​ API

  • Assert response​​ 

  • POST Request (Register successful)

  • POST Request (Register​​ unsuccessful)

  • Code snippets

Post​​ Request​​ API

Let us​​ now see how to POST an API request using PW.

Launch​​ https://reqres.in/​​ 

Notice​​ below​​ that we need to pass some body as part of POST request (unlike a GET request)​​ 

So let us see how to create or POST a new user id using PW.

The highlighted portion is the typical POST​​ request syntax

Next, we can add the usual lines#10,11 to parse and log the response

Save and execute.

Notice that POST request is successful​​ and new user ‘id’ got created

Assert Response

Let us now assert the response data (id and createdAt) that we have got in the console

We can simply write highlighted lines​​ (see below)​​ to assert the response data

Save and​​ run

POST Request (Register successful)

Let us now see another example of a POST request.​​ 

Below we can see that, in the response, we get a token

So, let us change the end point and​​ post the data as shown below. We have used the same email id and password that can be seen above.​​ Save and run

Notice​​ above​​ that token gets generated (that means user​​ registration was successful) and our test passes.​​ 

Next we can assert the token

Save and run, the test passes

POST Request (Register unsuccessful)

Let us take this last scenario. We have an error text in the response data

Change the endpoint and post data

Add the assertion

Save and run

Code snippet (POST Request)

import​​ {test,expect}​​ from​​ '@playwright/test'

 

test("api post response",​​ async​​ ({​​ request​​ })​​ =>​​ {

   const​​ response​​ =  await​​ request.post('https://reqres.in/api/users',{

     ​​ data:​​ {

         id:​​ 555,

      },

   })

   

   const​​ respBody​​ =​​ JSON.parse(await​​ response.text())

   console.log(respBody)  

})

 

Code snippet (assert response data)

import​​ {test,expect}​​ from​​ '@playwright/test'

 

test("api post response",​​ async​​ ({​​ request​​ })​​ =>​​ {

   const​​ response​​ =  await​​ request.post('https://reqres.in/api/users',{

     ​​ data:​​ {

         id:​​ 555,

      },

   })

   

   const​​ respBody​​ =​​ JSON.parse(await​​ response.text())

   //console.log(respBody)  

   expect(respBody.id).toBe(555)

   expect(respBody.createdAt).toBeTruthy()

 

})

 

Code snippet (POST request,​​ assert​​ registration successful)

import​​ {test,expect}​​ from​​ '@playwright/test'

 

test("api post response",​​ async​​ ({​​ request​​ })​​ =>​​ {

   const​​ response​​ =  await​​ request.post('https://reqres.in/api/login',{

     ​​ data:​​ {

         email:​​ 'eve.holt@reqres.in',

         password:​​ 'cityslicka',

      },

   })

   

   const​​ respBody​​ =​​ JSON.parse(await​​ response.text())

 ​​ ​​​​ //console.log(respBody)  

   

   expect(respBody.token).toBeTruthy()

 

})

 

Code snippet (for registration unsuccessful)

import​​ {test,expect}​​ from​​ '@playwright/test'

 

test("api post response",​​ async​​ ({​​ request​​ })​​ =>​​ {

   const​​ response​​ =  await​​ request.post('https://reqres.in/api/register',{

     ​​ data:​​ {

         email:​​ 'sydney@fife',        ​​ 

      },

   })

   

   const​​ respBody​​ =​​ JSON.parse(await​​ response.text())

   console.log(respBody)  

   

   expect(respBody.error).toBe("Missing password")

})

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024