30 Days of API Testing Day 21: Complete Exercise 3

Hey Duong,

You need to set the token as a cookie in order to do a POST, the cookie you acquired in step one.

That is the problem here I think.

Regards

3 Likes

Hi @friendlytester,

You are correct. After setting the token as a cookie I could get status code 200

I just suggest you update the line

Extract the token and store it

to

Extract the token and store it as a cookie

So the newbie members like me will not disturb you :sweat_smile:

Thank you for your quick support.

2 Likes

I completed this exercise using Katalon Studio.

4 Likes

Yes I used postman and able to parameterise the request run time by setting the token and roomid as env variables. And request are successful.
But if we use Same checkin and checkout time (time&date) then the request will give 409 (Conflict).
We need to handle timestamp in a generic way.

2 Likes

Let try to use Chakram API Testing Framework and Mocha - I have finished this challenges using my own API

Step 1: Get full candidates list, this API will return a list of candidates in the system
Step 2: From the response in step 1, try to get the first candidateโ€™s id and then pass it as the query string for step 3
Step 3: Call API to get specific candidate by candidate id

5 Likes

How do you extract the token and store it as a cookie in Postman? Iโ€™m new to this and have tried to Google it but canโ€™t find the information.

1 Like

Used advanced rest client

  1. retrieved a token from login endpoint
  2. performed a get against the room endpoint
  3. performed a post against the booking endpoint

4 Likes

Hi @helena, you can use environment variables or global variable to parameterize the the request. For below example, I use โ€œjson()โ€ method to extract the token then set it to an environment variale โ€œeTokenโ€, after running test, we will see an โ€œeTokenโ€ variable would be created:

Hope it might help you :blush:

5 Likes

Thank you! Will try this at once :smiley:

1 Like

I completed the exercise using Postman&Newman.

2018-12-02_16-06-51

2 Likes

I use my own framework to complete the exercise 3 with the steps below:

  1. Get a token by doing a POST against the auth API at https://automationintesting.online/auth/login endpoint
  2. Save the token to the request headers
  3. Extract a room ID from one of the rooms by performing a GET call against https://automationintesting.online/room/
  4. Do a POST call against the booking API to make a booking for the room, https://automationintesting.online/booking/
  5. Verify the booking info

5 Likes

My exercise 3 with Katalon is:

  1. Login https://automationintesting.online/auth/login to get token
    image
  2. Set Global Variable =token to use for booking
    image
  3. Setting cookie= token Global Variable
    image
    image
  4. Do a POST call against the booking API to make a booking for the room, https://automationintesting.online/booking/
    image
    image
5 Likes

I use Postman for this exercise.

My Solution:

  • Create a Environment Variable to store the value
  • Create a Postman collections and save all request below:
    โ€“ GET room request. And save any roomid of any room.
    โ€“ POST login. and save the token to env variable
    โ€“ POST booking. Using the token and roomid above to get the request

Here is my result

Collection:
image

Post method to login and store the token to env variable

image

Get method to get room and story a room_id to env variable
image

Post method to booking
image

Here is the result when running the collection
image

I will do this exercise again on Katalon when I have free time.

5 Likes

I completed exercise 3 by Katalon:

  • Login to the room:

  • GET call against:

  • POST call:

  • TCs:

5 Likes

I used Katalon to do this exercise:

4 Likes

I use Restlet-Client for this exercise, also applied on my project.

  • Create a Environment Variable to store the value
  • Create a Postman collections and save all request below:
    โ€“ GET room request. And save any roomid of any room.
    โ€“ POST login. and save the token to env variable
    โ€“ POST booking. Using the token and roomid above to get the request

Here is my results

  • Collection:
    image
  • GET method to call against
    image
  • POST call against the booking API to make a booking for the room
    image
    But the status of the request Collection, Gettoken, POST mothod always return 403 Forbidden
    image
    Could you please help to check if I miss any step or the issue come from your API.

Thanks!

2 Likes

I used API Fortress to complete exercise 3. Building upon what I did on exercise 2, I saved the roomid value returned after creating a room as a dynamic variable and include it in a following request to book that room.

I used a mixture of Postman to explore the API and RestAssured to do the code

All of the exercises/tricks are in this list

I used Postman to complete this exercise.

What I learned

  1. 409 CONFLICT
    The request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request.

  2. Test data can be generated in Postman Pre-request tab. I used it to generate random date. Otherwise POST request returns 409 code.

When I used pm.globals.set("startDate", startDate);to store generated data, data used in request body did not changefor some reason (e.g Postman always used same key value pair โ€œstartDate: 2022-06-19T09:48:26.469Zโ€). When I used postman.setEnvironmentVariable("startDate", startDate);, Postman used new generated data in each request.

  1. We can run a sequence of requests using collection menu:
  • click 3 dots when collection is selected
  • click Run collection

One thing I did not understand is how I can find out the correct format of Header for post request. It should have token in โ€œCookieโ€ key, but how would I find it? When I view request in Network tab it does not show cookie after clicking Book button on this page Restful-booker-platform demo .

Request Headers
Content-Type: application/json
Cookie: token=1x3TfVxE9KRhnT5z; token=1x3TfVxE9KRhnT5z
User-Agent: PostmanRuntime/7.29.0

Here is Pre-request script to generate date

// generate random dates

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

startDate = getRndInteger(10, 20);
endDate = startDate + 2;

console.log(startDate)
console.log(endDate);

startDate = "2022-06-"+ startDate.toString()+"T09:48:26.469Z";
endDate = "2022-06-"+ endDate.toString()+"T09:48:26.469Z";
console.log("startDate: "+startDate);
console.log("endDate: "+endDate);

postman.setEnvironmentVariable("startDate", startDate);
postman.setEnvironmentVariable("endDate", endDate);

Links:

1 Like

Here is example of the test written usingTavern, YAML and Python. You can find my repository on GitHub.

# test_basics.tavern.yaml
# Saving data from response using external functions

test_name: Saving data from response using external functions ex11
stages:
  - name: Validate status code 200

    request:
      url: http://www.recipepuppy.com/api/
      method: GET
      json:
        i: avocado
        q: kale
        p: 1

    response:
      json:
        title: "Recipe Puppy"
      save:
        $ext:
          function: utils:save_data
        json:
          test_ingredients: results[0].ingredients

  - name: Validate saved  data

    request:
      url: http://www.recipepuppy.com/api/
      method: GET
      json:
        i: avocado
        q: kale
        p: 1

    response:
      status_code: 200
      json:
        results:
          - title: "{test_title}"
            ingredients: "{test_ingredients}"


#In this case, both `{test_title}` and `{test_ingredients}` are available for use
#in later requests
 # utils.py
def save_data(response):
    return Box({"test_title": response.json()["results"][0]["title"]})