|
| 1 | +# Postman API Testing Master Class 🔥🚀 |
| 2 | + |
| 3 | +## 01. Introduction ✅ |
| 4 | + |
| 5 | +### Types of API |
| 6 | + |
| 7 | +**There are two types of API's,** |
| 8 | + |
| 9 | +1. Simple Object Access Protocol (SOAP) |
| 10 | +2. Representational State Transfer (REST) |
| 11 | + |
| 12 | +Both are the web services. |
| 13 | + |
| 14 | +### API vs WebServices |
| 15 | + |
| 16 | +- Web Service is an API wrapped in HTTP. |
| 17 | +- All Web Services are API but APIs are not Web Services. |
| 18 | +- A Web Service needs a network while an API doesn't need a network for its operation. |
| 19 | + |
| 20 | +## 02. Environment Setup ✅ |
| 21 | + |
| 22 | +_Postman - API testing tool_ |
| 23 | + |
| 24 | +- We can do manual testing of API's using postman. |
| 25 | +- Web / Desktop testing. |
| 26 | +- Workspace: Area where we maintain files and saved. |
| 27 | +- Create workspace, rename and delete. |
| 28 | +- Creating Collection - contains number of folders and http requests. |
| 29 | + (Create, Rename, Delete and Run the collection) |
| 30 | +- We can create any number of collections under workspace. |
| 31 | + |
| 32 | +### HTTP Request |
| 33 | + |
| 34 | +**REQUEST ➡️ APIs ➡️ RESPONSE** |
| 35 | + |
| 36 | +**_GET_** ➡️ Retrieve the resource from database |
| 37 | +**_POST_** ➡️ Create resource on database |
| 38 | +**_PUT_** ➡️ Update existing resource on database |
| 39 | +**_DELETE_** ➡️ Delete existing resource from database |
| 40 | +**_PATCH_** ➡️ Update partial details of resource |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +### Validations |
| 45 | + |
| 46 | +**status code** |
| 47 | +**time** |
| 48 | +**size data** |
| 49 | +**response body(json/xml)** |
| 50 | +**cookies** |
| 51 | +**headers** |
| 52 | + |
| 53 | +### HTTP Status Code |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## 03. Creating Our Own APIs ✅ |
| 58 | + |
| 59 | +### Creating our own APIs |
| 60 | + |
| 61 | +**Step1 - Install NodeJS** |
| 62 | + |
| 63 | +**Step2 - Check Node and npm package manager version** |
| 64 | + |
| 65 | +```bash |
| 66 | +node -v |
| 67 | +npm -v |
| 68 | +``` |
| 69 | + |
| 70 | +**Step3 - Install json-server** |
| 71 | + |
| 72 | +```bash |
| 73 | + npm install -g json-server |
| 74 | +``` |
| 75 | + |
| 76 | +### Test / Validations |
| 77 | + |
| 78 | +**JSON - JavaScript Object Notation** |
| 79 | + |
| 80 | +**Key Value Pairs => key:value** |
| 81 | + |
| 82 | +**_key is always included in "" quotation_** |
| 83 | + |
| 84 | +### json-server |
| 85 | + |
| 86 | +1. Open Terminal |
| 87 | +2. Run the following command to run API testing |
| 88 | + |
| 89 | +```bash |
| 90 | + json-server file_name |
| 91 | +``` |
| 92 | + |
| 93 | +```json |
| 94 | +{ |
| 95 | + "firstname": "John", |
| 96 | + "secondname": null, |
| 97 | + "age": 30, |
| 98 | + "phone": 1234567890, |
| 99 | + "status": true |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +**Student Data** |
| 104 | + |
| 105 | +```json |
| 106 | +{ |
| 107 | + "students": [ |
| 108 | + { |
| 109 | + "sid": 101, |
| 110 | + "sname": "John", |
| 111 | + "grad": "A" |
| 112 | + }, |
| 113 | + { |
| 114 | + "sid": 102, |
| 115 | + "sname": "Kim", |
| 116 | + "grad": "B" |
| 117 | + }, |
| 118 | + { |
| 119 | + "sid": 103, |
| 120 | + "sname": "Scott", |
| 121 | + "grad": "C" |
| 122 | + } |
| 123 | + ] |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### JSON vs XML |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +## 04. API Response Validations ✅ |
| 132 | + |
| 133 | +### Response Validations |
| 134 | + |
| 135 | +**Status Code** |
| 136 | +**Headers** |
| 137 | +**Cookies** |
| 138 | +**Response time** |
| 139 | +**Response body** |
| 140 | + |
| 141 | +### Assertion Validation |
| 142 | + |
| 143 | +**pm - is a Library** |
| 144 | +**_Functions/ Assertions are available for assertion validations._** |
| 145 | +**_These Functions are written in JavaScript. Postman built-in uses JavaScript._** |
| 146 | + |
| 147 | +`pm.function inside JavaScript Function` |
| 148 | + |
| 149 | +**_We need to write our own JavaScript function and inside that we have to use `pm.function` for assertion validations._** |
| 150 | + |
| 151 | +**Normal Function** |
| 152 | + |
| 153 | +```javascript |
| 154 | +pm.test (“Test Name”, function () |
| 155 | +{ |
| 156 | +// assertion; |
| 157 | +} |
| 158 | +); |
| 159 | +``` |
| 160 | + |
| 161 | +**Arrow Function** |
| 162 | + |
| 163 | +```javascript |
| 164 | +pm.test (“Test Name”, () => |
| 165 | + { |
| 166 | +// assertion; |
| 167 | +} |
| 168 | +); |
| 169 | +``` |
| 170 | + |
| 171 | +### Testing Status Codes |
| 172 | + |
| 173 | +1. Go to Postman |
| 174 | +2. Create or Add a Request |
| 175 | +3. In Request, go to `Tests` tab |
| 176 | +4. Inside Test tab we have to write validation functions |
| 177 | + |
| 178 | +**Test for the response status code:** |
| 179 | + |
| 180 | +```javascript |
| 181 | +pm.test (“Status code is 200”, () => { |
| 182 | +pm.response.to.have.status(200); |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +**If you want to test for the status code being one of a set, include them all in an array and use one of:** |
| 187 | + |
| 188 | +```javascript |
| 189 | +pm.test (“Successful POST request”, () => { |
| 190 | +pm.expect(pm.response.code).to.be.oneOf([201, 202]); |
| 191 | +}); |
| 192 | +``` |
| 193 | + |
| 194 | +**Check the status code text:** |
| 195 | + |
| 196 | +```javascript |
| 197 | +pm.test (“Status code name has string”, () => { |
| 198 | +pm.response.to.have.status(“Created”); |
| 199 | +}); |
| 200 | +``` |
0 commit comments