✅ How to do API testing
✅ Common API methods
✅ API automation
✅ Fast
✅ Reliable
✅ Check business logic of app
- Open your browser
- Open your Network tab of dev tools
- Go to
https://jsonplaceholder.typicode.com/comments
in browser
- Go to
cypress/e2e/network/exercise.cy.js
- Add the following code inside of
context(){}
it('comments returns 200 and 500 body length', () => {
// https://on.cypress.io/request
cy.request(`${baseUrl}/comments`).should((response) => {
expect(response.status).to.eq(200);
// the server sometimes gets an extra comment posted from another machine
// which gets returned as 1 extra object
expect(response.body).to.have.property('length').and.be.oneOf([500, 501]);
});
});
Run the tests
npx cypress run --spec **/network/exercise.cy.js
Use POST when you want to add a child resource under resources collection
- In your browser go to (API Fortress)[https://app.saucelabs.com/api-testing]
- Use this URL for your POST
https://jsonplaceholder.typicode.com/posts
- Use this body
{
"userId": 11,
"title": "any title you want",
"body": "any body"
}
- Send the request
- Go to
cypress/integration/network/exercise.spec.js
- Follow instructions for test
it("Can create new user on /posts", () => {
- Run the tests
npx cypress run --spec **/network/exercise.spec.js
Use PUT when we want to modify a singular resource that is already a part of resources collection
- In your browser go to (API Fortress)[https://app.saucelabs.com/api-testing]
- Use this URL for your PUT
https://jsonplaceholder.typicode.com/posts/1
- Use this body
{
"id": 11,
"title": "using a PUT",
"body": "any body",
"userId": 1
}
- Send the request
ℹ️ resource will not be really updated on the server but it will be faked as if
- Go to
cypress/integration/network/exercise.spec.js
- Implement test
it("Can update posts")
by following instructions - Run the tests
npx cypress run --spec **/network/exercise.spec.js
✅ Use GET requests to retrieve resource representation/information only
✅ Use POST when you want to add a child resource under resources collection
✅ Use PUT when we want to modify a singular resource that is already a part of resources collection
✅ Use an HTTP client like API Fortress or Postman to perform non-GET operations
🎁 Bonus exercises in bonus.spec.js
👩💻API Testing with Java by Bas Dijkstra