postman automation workshop
postman automation workshop
TESTING 101
1.2.The first thing we will do is open Postman and create a collection to work on this
exercise, which we will call "Test1".
1.3.Next, we will enter the link to the page with the booking services, copy the cURLs
of each service and import them into our created collection.
This should stay like this.
2.2.Obtaining reservation
23.Reservation update
2.4. Reservation removal
We can see that this time the status is not 200, but 201 Created.
Remember that all the request sending results are seen at the bottom in the Body
section.
3. Automation with test and pre-request script:
3.1.For booking reservation creation, the most basic automated test in Postman is to
check the status; To do this, we will rely on snippets. We will automate by entering
the tests tab, where, on the left side, we will find the snippets and select the one
indicated for this operation.
In the Test Results tab, we can verify that the status obtained from this operation,
with the expected result by invoking the snippet “Status code is 200”, is satisfactory.
The last test we will do is validate the three fields we work with: “FirstName”,
“LastName” and “TotalPrice”, invoking another snippet called “Response body: JSON
value check”.
As it is a function that requires detailed explanation, I asked chatgpt to explain it line by
line
Extract data from response: var jsonData = pm.response.json(); gets the response of the
submitted request and converts it from JSON format to a JavaScript object, which can be
accessed and manipulated within the script. pm.response is an object that Postman provides
to interact with the request response, and .json() is a method to parse the body of the
response assuming it is valid JSON.
Make an assertion: pm.expect(jsonData.value).to.eql(100); uses the Chai.js assertions
module (via pm.expect) to check if a specific value in the response is equal to the expected
value. In this case, the value of the value property of the JSON object is expected to be equal
to 100. If the value is 100, the test passes; if not, the test fails.
To perform specific tests, we will name our tests “Verify ID Exists”, “Verify First Name
Exists”, “Verify Last Name Exists”, and “Verify Price Exists”. In addition to checking if the
value exists with a pm.expect, we will take the opportunity to save the creation results
in environment variables. This will allow us to use them in testing the remaining
services.
Before that we will create a variable taking the value of the field in the Json code, for
example jsonData.booking.firstname
To convert the json response into javascript code we use jsonData and point to the
fields in its structure.
Likewise in the pm. expect we do the same interaction by starting the taking of variables with
jsonData.
It will be an occasion to use ChatGPT, which will help us develop a function. We must
keep in mind that, day after day, artificial intelligence becomes more relevant.
However, for today, since we are just getting started, we will use it only for the pre-
request script and automate this part of the work. As we progress through each
workshop or newsletter, we will go deeper into the use of AI. It is important to
remember that the ID in the URL will be replaced by {{variableid}}.
Using chat gpt to automate the pre request script
We will replace the mention of "random number" with our variable "rndprice", which
indicates that we have programmed our function before executing the request. Next,
we will proceed to perform the test to update the price.
As usual, we will use the snippet to check the status. Now, we will proceed to
perform the assertion to confirm that the price update has been carried out
correctly.