api-automation-library
v1.0.0
Published
Reusable API library for automation testing. It calls an API and checks the answer.
Downloads
149
Readme
api-automation-library
Reusable API library for automation testing. It calls an API and checks the answer.
An API is a hidden helper. You ask it a question. It sends an answer. This library asks, then checks the answer.
Real example
Ask: who is user 1?
https://jsonplaceholder.typicode.com/users/1
A good answer says:
- the reply worked (status 200)
- the id is 1
- the name is Leanne Graham
import { auditApi } from "api-automation-library";
const report = await auditApi({
request: { url: "https://jsonplaceholder.typicode.com/users/1" },
expect: {
status: [200],
maxResponseTimeMs: 5000,
jsonPath: [
{ path: "id", equals: 1 },
{ path: "name", equals: "Leanne Graham" },
],
},
});
console.log(report.passed);If every check is good, report.passed is true. If the name is wrong, or no answer comes, report.passed is false.
Install
npm install api-automation-libraryNode.js 18 or newer.
Checks
| Check | Meaning | |---|---| | status | The answer code is one you allow. 200 means yes. | | responseTime | The answer came within the time you allow. | | headers | The answer has the labels you asked for. | | schema | The answer has the right fields and types. | | jsonPath | One fact is exactly right, such as the name. | | bodyContains | The answer text contains the words you expect. |
Pass report: { dir: "api-report" } to also save report.json and report.html.
Try it in this repo
npm install
npm test
npm run examplenpm run example calls the real user API and prints pass or fail.
