While doing API testing there are test scenarios that require you to chain multiple APIs in order to get the desired result. API Chaining lets you perform multiple API calls, where value from the first API is passed on to the second API and so on. To further deep dive, let’s automate the below scenario –
1. Perform a GET request to the endpoint https://www.metaweather.com/api/location/search/?query=sn.
2. Now from the response body, save the city name under location variable.
3. Now use the location variable in the second request as https://www.metaweather.com/api/location/search/?query=location
4. Now validate that for the second response, the status code is 200 and the response body has the city name equal to the value stored in the location variable from the 1st API’s response.
Step 1: Let’s do a GET request to the endpoint: https://www.metaweather.com/api/location/search/?query=sn and check the response.
1 2 3 4 5 6 7 8 | [ { "title": "Fresno", "location_type": "City", "woeid": 2407517, "latt_long": "36.740681,-119.785728" } ] |
We will now save the title(from the response body) Fresno in the variable location. Now at the time of writing this article, the city that we are getting is Fresno but it might be different afterward. So we have to make sure that we write the test in such a way that if our city changes, the tests should not be affected.
Now for the second API, we will append the location variable to the request URL, https://www.metaweather.com/api/location/search/?query=location which translates into https://www.metaweather.com/api/location/search/?query=Fresno. The response we get is:
1 2 3 4 5 6 7 8 | [ { "title": "Fresno", "location_type": "City", "woeid": 2407517, "latt_long": "36.740681,-119.785728" } ] |
And now we will validate that the title of the second response is equal to the value saved in the location variable.
Step 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | describe('Example to demonstrate API Chaining in Cypress', function () { it('Chain two API requests and validate the response', () => { //Part 1 cy.request({ method: 'GET', url: 'https://www.metaweather.com/api/location/search/?query=sn', }).then((response) => { const location = response.body[0].title return location }) //Part 2 .then((location) => { cy.request({ method: 'GET', url: 'https://www.metaweather.com/api/location/search/?query=' + location }).then((response) => { expect(response.status).to.eq(200) expect(response.body[0]).to.have.property('title', location) }) }) }) }) |
1 2 3 4 5 6 7 8 | //Part 1 cy.request({ method: 'GET', url: 'https://www.metaweather.com/api/location/search/?query=sn', }).then((response) => { const location = response.body[0].title return location }) |
In part 1, we are making a GET request to the endpoint https://www.metaweather.com/api/location/search/?query=sn and then saving the title value to the variable location and then returning it, so that it can be used by the next request.
1 2 3 4 5 6 7 8 9 10 | //Part 2 .then((location) => { cy.request({ method: 'GET', url: 'https://www.metaweather.com/api/location/search/?query=' + location }).then((response) => { expect(response.status).to.eq(200) expect(response.body[0]).to.have.property('title', location) }) }) |
Now in part 2, the location variable is passed as .then(location) and appended to the API end point. And, then we are making a GET request to this endpoint.
expect(response.status).to.eq(200) checks that the we get a 200 success response. expect(response.body[0]).to.have.property(‘title’, location) checks that the title in the part 2 API response is equal to the location variable from part 1.
Step 3: After Successful execution:
Do check out 🙂
Github:Â https://github.com/alapanme/Cypress-Automation
All Cypress Articles: https://testersdock.com/cypress-tutorial/
Thank you a lot. It was very helpful for me.