├── .circleci └── config.yml ├── .github └── workflows │ └── main.yml ├── .gitlab-ci.yml ├── .travis.yml ├── README.md ├── bitbucket-pipelines.yml ├── public ├── Bitbucket_Pipeline.PNG ├── CircleCI.PNG ├── GitLab.PNG └── TravisCI.PNG └── tests ├── Restful_Booker_Collection.postman_collection.json └── Restful_Booker_Environment.postman_environment.json /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | newman: postman/newman@1.0.0 4 | jobs: 5 | build: 6 | executor: newman/postman-newman-docker 7 | steps: 8 | - checkout 9 | - newman/newman-run: 10 | collection: ./tests/Restful_Booker_Collection.postman_collection.json 11 | environment: ./tests/Restful_Booker_Environment.postman_environment.json 12 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: "Newman Tests" 4 | on: [push, pull_request] 5 | 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | container: 10 | image: postman/newman 11 | steps: 12 | - uses: actions/checkout@v1 13 | - name: Run API Tests 14 | run: newman run ./tests/Restful_Booker_Collection.postman_collection.json -e ./tests/Restful_Booker_Environment.postman_environment.json 15 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - test 3 | 4 | newman_tests: 5 | stage: test 6 | image: 7 | name: postman/newman 8 | entrypoint: [""] 9 | script: 10 | - newman --version 11 | - newman run ./tests/Restful_Booker_Collection.postman_collection.json -e ./tests/Restful_Booker_Environment.postman_environment.json 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '14' 4 | 5 | install: 6 | - npm install newman 7 | 8 | before_script: 9 | - node --version 10 | - npm --version 11 | - node_modules/.bin/newman --version 12 | 13 | script: 14 | - node_modules/.bin/newman run tests/Restful_Booker_Collection.postman_collection.json -e tests/Restful_Booker_Environment.postman_environment.json --color auto --disable-unicode 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postman-ci-pipeline-example 2 | 3 | This is very simple project to demonstrate using [Newman](https://github.com/postmanlabs/newman) to run a Postman collections through some of the different CI systems. There are a number of different ways that you can use `Newman` in these CI systems. 4 | 5 | - TravisCI is creating a node environment and installing the `Newman` NPM package, then running the Collection via a CLI script. 6 | - CircleCI is using the `postman/newman` orb, from there orb [registry](https://circleci.com/orbs/registry/). This a packaged version of the Newman Docker image held on their internal registry. 7 | - Bitbucket Pipelines is creating an environment and using the `postman/newman` Docker image and running the collection file in a container. 8 | - GitLab is using the `postman/newman` Docker image and running the collection file in a container. 9 | 10 | The collection and environment files have been taken from the [All-Things-Postman Repo](https://github.com/DannyDainton/All-Things-Postman). Each of the different systems will output the same CLI summary to the console. 11 | 12 | ## Build badges 13 | 14 | Using [Shields.io](https://shields.io/) we can get a visual indication about about build status and display this on our repo. I've included the ones for Travis and CircleCI here as these run when a commit is made against this repo. I've added the ones for BitBucket and Gitlab to the sample projects on those repos. 15 | 16 | --- 17 | 18 | ## TravisCI 19 | 20 | [![Build Status](https://travis-ci.org/DannyDainton/postman-ci-pipeline-example.svg?branch=master)](https://travis-ci.org/DannyDainton/postman-ci-pipeline-example) 21 | 22 | In order to use TravisCI in your own projects, you will need to signup and then sync it to your Github account. On the free TravisCI tier, you will only be able to run your Public repos through TravisCI. 23 | 24 | More information about the sign up and set up process for TravisCI can be found [here](https://docs.travis-ci.com/user/getting-started). 25 | 26 | ### How does it work with TravisCI 27 | 28 | All the magic happens in the `.travis.yml` file. This is building out the node environment and installing the required `Newman` module needed to run the collection file. 29 | 30 | Once the node environment has been created, the `script` section is run - This would be run in the same way that you would run `Newman` from the command line on your local machine. 31 | 32 | You can specify a number of different command line args to the script that would change the way that `Newman` runs the command. I've added the `--color` and `--disable-unicode` flags as an example of the args that can be used. More details about the other command line args can be found [here](https://github.com/postmanlabs/newman#command-line-options). 33 | 34 | ```yml 35 | language: node_js 36 | node_js: 37 | - "14" 38 | 39 | install: 40 | - npm install newman 41 | 42 | before_script: 43 | - node --version 44 | - npm --version 45 | - node_modules/.bin/newman --version 46 | 47 | script: 48 | - node_modules/.bin/newman run tests/Restful_Booker_Collection.postman_collection.json -e tests/Restful_Booker_Environment.postman_environment.json --color auto --disable-unicode 49 | ``` 50 | 51 | Once the collection has run via TravisCI, you will see the log output of that build in the Travis UI. Any `Tests` that are in the collection, will run and the results will be displayed in the log output. 52 | 53 | #### An example of the summary run output... 54 | 55 | ![TravisCI](/public/TravisCI.PNG) 56 | 57 | --- 58 | 59 | ## CircleCI 60 | 61 | [![Build Status](https://img.shields.io/circleci/project/github/DannyDainton/postman-ci-pipeline-example.svg)](https://circleci.com/gh/DannyDainton/postman-ci-pipeline-example) 62 | 63 | In order to use CircleCI on your projects you will need an account. You can sign in via Github, which makes it easier to add your projects from your repos. 64 | 65 | More information about getting started with CircleCI, can be found [here](https://circleci.com/docs/2.0/first-steps/#section=getting-started). 66 | 67 | ### How does it work with CircleCI 68 | 69 | All the magic happens in the `.circleci/config.yml` file. This is using the `Postman` Orb to run the collection file. 70 | 71 | > Orbs are packages of config that you can use to quickly get started with the CircleCI platform. Orbs enable you to share, 72 | > standardize, and simplify config across your projects. You may also want to use orbs as a reference for config best 73 | > practices. Refer to the CircleCI Orbs Registry for the complete list of available orbs. 74 | 75 | The basic `config.yml` file will look like the example below. There several other Newman specific options available, just like when running `Newman` from the CLI. More information about the options can be found [here](https://circleci.com/orbs/registry/orb/postman/newman). 76 | 77 | ```yml 78 | version: 2.1 79 | orbs: 80 | newman: postman/newman@0.0.2 81 | jobs: 82 | build: 83 | executor: newman/postman-newman-docker 84 | steps: 85 | - checkout 86 | - newman/newman-run: 87 | collection: ./tests/Restful_Booker_Collection.postman_collection.json 88 | environment: ./tests/Restful_Booker_Environment.postman_environment.json 89 | ``` 90 | 91 | #### An example of the summary run output... 92 | 93 | ![CircleCI](/public/CircleCI.PNG) 94 | 95 | --- 96 | 97 | 98 | There a couple of other CI systems that I wanted to demo - These unlike TravisCI and CircleCI, hold the code in their own repos and not within Github. They both work in a simialir ways and use a `.yml` for the build config but the output can be seen in the application UI, rather than moving away to view this in a 3rd dashboard. 99 | 100 | In order to use these CI systems you would need to create an account and sign in, before you're able to create your first projects. 101 | 102 | - [Bitbucket](https://bitbucket.org/account/signup/) 103 | - [GitLab](https://gitlab.com/users/sign_in#register-pane) 104 | 105 | ## Bitbucket Pipelines 106 | 107 | ### How does it work with Bitbucket Pipelines 108 | 109 | All the magic happens in the `bitbucket.pipelines.yml` file. This is using the `postman/newman` Docker image, to run the collection file. 110 | 111 | ```yml 112 | image: postman/newman 113 | 114 | pipelines: 115 | default: 116 | - step: 117 | script: 118 | - newman --version 119 | - newman run ./tests/Restful_Booker_Collection.postman_collection.json -e ./tests/Restful_Booker_Environment.postman_environment.json 120 | ``` 121 | 122 | #### An example of the summary run output... 123 | 124 | ![Bitbucket Pipeline](/public/Bitbucket_Pipeline.PNG) 125 | 126 | A sample Bitbucket Pipeline project can be found here: https://bitbucket.org/ddainton/postman-ci-pipeline-example/src/master/ 127 | 128 | --- 129 | 130 | ## GitLab 131 | 132 | ### How does it work with GitLab 133 | 134 | All the magic happens in the `.gitlab-ci.yml` file. This is using the `postman/newman` Docker image, to run the collection file. 135 | 136 | ```yml 137 | stages: 138 | - test 139 | 140 | newman_tests: 141 | stage: test 142 | image: 143 | name: postman/newman 144 | entrypoint: [""] 145 | script: 146 | - newman --version 147 | - newman run ./Restful_Booker_Collection.postman_collection.json -e ./Restful_Booker_Environment.postman_environment.json 148 | ``` 149 | 150 | #### An example of the summary run output... 151 | 152 | ![GitLab](/public/GitLab.PNG) 153 | 154 | A sample Gitlab project can be found here: https://gitlab.com/DannyDainton/postman-ci-pipeline-example 155 | 156 | --- 157 | 158 | That's it - It's a very simple example of how you *could* use some of the CI Systems out there to run Postman collections with Newman. 159 | 160 | If you have any questions, you can drop me a message on Twitter `@dannydainton`. 161 | -------------------------------------------------------------------------------- /bitbucket-pipelines.yml: -------------------------------------------------------------------------------- 1 | image: postman/newman 2 | 3 | pipelines: 4 | default: 5 | - step: 6 | script: 7 | - newman --version 8 | - newman run ./tests/Restful_Booker_Collection.postman_collection.json -e ./tests/Restful_Booker_Environment.postman_environment.json 9 | -------------------------------------------------------------------------------- /public/Bitbucket_Pipeline.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DannyDainton/postman-ci-pipeline-example/cade5a5816bf898e4794b2e6d453af410b4fb4f4/public/Bitbucket_Pipeline.PNG -------------------------------------------------------------------------------- /public/CircleCI.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DannyDainton/postman-ci-pipeline-example/cade5a5816bf898e4794b2e6d453af410b4fb4f4/public/CircleCI.PNG -------------------------------------------------------------------------------- /public/GitLab.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DannyDainton/postman-ci-pipeline-example/cade5a5816bf898e4794b2e6d453af410b4fb4f4/public/GitLab.PNG -------------------------------------------------------------------------------- /public/TravisCI.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DannyDainton/postman-ci-pipeline-example/cade5a5816bf898e4794b2e6d453af410b4fb4f4/public/TravisCI.PNG -------------------------------------------------------------------------------- /tests/Restful_Booker_Collection.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "e1e36c17-9661-47a9-ba36-aa74988282f4", 4 | "name": "Restful_Booker_Collection", 5 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" 6 | }, 7 | "item": [ 8 | { 9 | "name": "Health Check", 10 | "item": [ 11 | { 12 | "name": "Ping the API", 13 | "event": [ 14 | { 15 | "listen": "test", 16 | "script": { 17 | "id": "9a52cee5-7399-49d7-b6e3-056779178c07", 18 | "type": "text/javascript", 19 | "exec": [ 20 | "pm.test(\"Status code is 201\", () => pm.response.to.have.status(201))", 21 | "", 22 | "pm.test('Content-Type header is correct', () => pm.response.to.have.header('Content-Type', 'text/plain; charset=utf-8'))" 23 | ] 24 | } 25 | }, 26 | { 27 | "listen": "prerequest", 28 | "script": { 29 | "id": "945042dc-1d26-4f1d-8505-61d928416bac", 30 | "type": "text/javascript", 31 | "exec": [ 32 | "", 33 | "", 34 | "" 35 | ] 36 | } 37 | } 38 | ], 39 | "request": { 40 | "method": "GET", 41 | "header": [], 42 | "body": { 43 | "mode": "raw", 44 | "raw": "" 45 | }, 46 | "url": { 47 | "raw": "{{baseURL}}/ping", 48 | "host": [ 49 | "{{baseURL}}" 50 | ], 51 | "path": [ 52 | "ping" 53 | ] 54 | } 55 | }, 56 | "response": [] 57 | } 58 | ], 59 | "event": [ 60 | { 61 | "listen": "prerequest", 62 | "script": { 63 | "id": "fbbd986f-ad1d-4f4e-b402-18100283fd7e", 64 | "type": "text/javascript", 65 | "exec": [ 66 | "" 67 | ] 68 | } 69 | }, 70 | { 71 | "listen": "test", 72 | "script": { 73 | "id": "2f5148e7-8c33-4074-a600-8831157c40f0", 74 | "type": "text/javascript", 75 | "exec": [ 76 | "pm.test(\"Status code is 201\", () => pm.response.to.have.status(201))", 77 | "" 78 | ] 79 | } 80 | } 81 | ] 82 | }, 83 | { 84 | "name": "Get all Bookings", 85 | "item": [ 86 | { 87 | "name": "Get all bookings", 88 | "event": [ 89 | { 90 | "listen": "test", 91 | "script": { 92 | "id": "1d436a72-c0cc-4fee-9f43-bd017d0d373f", 93 | "type": "text/javascript", 94 | "exec": [ 95 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 96 | ] 97 | } 98 | } 99 | ], 100 | "request": { 101 | "method": "GET", 102 | "header": [], 103 | "body": { 104 | "mode": "raw", 105 | "raw": "" 106 | }, 107 | "url": { 108 | "raw": "{{baseURL}}/booking", 109 | "host": [ 110 | "{{baseURL}}" 111 | ], 112 | "path": [ 113 | "booking" 114 | ] 115 | } 116 | }, 117 | "response": [] 118 | }, 119 | { 120 | "name": "Get all bookings with all parameters", 121 | "event": [ 122 | { 123 | "listen": "test", 124 | "script": { 125 | "type": "text/javascript", 126 | "exec": [ 127 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 128 | ] 129 | } 130 | } 131 | ], 132 | "request": { 133 | "method": "GET", 134 | "header": [], 135 | "body": { 136 | "mode": "raw", 137 | "raw": "" 138 | }, 139 | "url": { 140 | "raw": "{{baseURL}}/booking?firstname=sally&lastname=brown&checkin=2017-11-11&checkout=2017-11-15", 141 | "host": [ 142 | "{{baseURL}}" 143 | ], 144 | "path": [ 145 | "booking" 146 | ], 147 | "query": [ 148 | { 149 | "key": "firstname", 150 | "value": "sally" 151 | }, 152 | { 153 | "key": "lastname", 154 | "value": "brown" 155 | }, 156 | { 157 | "key": "checkin", 158 | "value": "2017-11-11" 159 | }, 160 | { 161 | "key": "checkout", 162 | "value": "2017-11-15" 163 | } 164 | ] 165 | } 166 | }, 167 | "response": [] 168 | }, 169 | { 170 | "name": "Get all bookings with the firstname and last name parameter", 171 | "event": [ 172 | { 173 | "listen": "test", 174 | "script": { 175 | "type": "text/javascript", 176 | "exec": [ 177 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 178 | ] 179 | } 180 | } 181 | ], 182 | "request": { 183 | "method": "GET", 184 | "header": [], 185 | "body": { 186 | "mode": "raw", 187 | "raw": "" 188 | }, 189 | "url": { 190 | "raw": "{{baseURL}}/booking?firstname=sally&lastname=brown", 191 | "host": [ 192 | "{{baseURL}}" 193 | ], 194 | "path": [ 195 | "booking" 196 | ], 197 | "query": [ 198 | { 199 | "key": "firstname", 200 | "value": "sally" 201 | }, 202 | { 203 | "key": "lastname", 204 | "value": "brown" 205 | } 206 | ] 207 | } 208 | }, 209 | "response": [] 210 | }, 211 | { 212 | "name": "Get all bookings with the firstname parameter", 213 | "event": [ 214 | { 215 | "listen": "test", 216 | "script": { 217 | "id": "e9268b31-7153-4d71-afec-55bdd4da3716", 218 | "type": "text/javascript", 219 | "exec": [ 220 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 221 | "" 222 | ] 223 | } 224 | } 225 | ], 226 | "request": { 227 | "method": "GET", 228 | "header": [], 229 | "body": { 230 | "mode": "raw", 231 | "raw": "" 232 | }, 233 | "url": { 234 | "raw": "{{baseURL}}/booking?firstname=sally", 235 | "host": [ 236 | "{{baseURL}}" 237 | ], 238 | "path": [ 239 | "booking" 240 | ], 241 | "query": [ 242 | { 243 | "key": "firstname", 244 | "value": "sally" 245 | } 246 | ] 247 | } 248 | }, 249 | "response": [] 250 | }, 251 | { 252 | "name": "Get all bookings with the lastname parameter", 253 | "event": [ 254 | { 255 | "listen": "test", 256 | "script": { 257 | "type": "text/javascript", 258 | "exec": [ 259 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 260 | ] 261 | } 262 | } 263 | ], 264 | "request": { 265 | "method": "GET", 266 | "header": [], 267 | "body": { 268 | "mode": "raw", 269 | "raw": "" 270 | }, 271 | "url": { 272 | "raw": "{{baseURL}}/booking?lastname=brown", 273 | "host": [ 274 | "{{baseURL}}" 275 | ], 276 | "path": [ 277 | "booking" 278 | ], 279 | "query": [ 280 | { 281 | "key": "lastname", 282 | "value": "brown" 283 | } 284 | ] 285 | } 286 | }, 287 | "response": [] 288 | }, 289 | { 290 | "name": "Get all bookings with the checkin parameter", 291 | "event": [ 292 | { 293 | "listen": "test", 294 | "script": { 295 | "type": "text/javascript", 296 | "exec": [ 297 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 298 | ] 299 | } 300 | } 301 | ], 302 | "request": { 303 | "method": "GET", 304 | "header": [], 305 | "body": { 306 | "mode": "raw", 307 | "raw": "" 308 | }, 309 | "url": { 310 | "raw": "{{baseURL}}/booking?checkin=2017-11-11", 311 | "host": [ 312 | "{{baseURL}}" 313 | ], 314 | "path": [ 315 | "booking" 316 | ], 317 | "query": [ 318 | { 319 | "key": "checkin", 320 | "value": "2017-11-11" 321 | } 322 | ] 323 | } 324 | }, 325 | "response": [] 326 | }, 327 | { 328 | "name": "Get all bookings with the checkout parameter", 329 | "event": [ 330 | { 331 | "listen": "test", 332 | "script": { 333 | "type": "text/javascript", 334 | "exec": [ 335 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 336 | ] 337 | } 338 | } 339 | ], 340 | "request": { 341 | "method": "GET", 342 | "header": [], 343 | "body": { 344 | "mode": "raw", 345 | "raw": "" 346 | }, 347 | "url": { 348 | "raw": "{{baseURL}}/booking?checkout=2017-11-11", 349 | "host": [ 350 | "{{baseURL}}" 351 | ], 352 | "path": [ 353 | "booking" 354 | ], 355 | "query": [ 356 | { 357 | "key": "checkout", 358 | "value": "2017-11-11" 359 | } 360 | ] 361 | } 362 | }, 363 | "response": [] 364 | } 365 | ] 366 | }, 367 | { 368 | "name": "Get a single booking", 369 | "item": [ 370 | { 371 | "name": "Get a single booking", 372 | "event": [ 373 | { 374 | "listen": "test", 375 | "script": { 376 | "id": "a2bc2659-9bfc-452d-9ee5-9783f27c6c49", 377 | "type": "text/javascript", 378 | "exec": [ 379 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 380 | "", 381 | "pm.test('Content-Type header is correct', () => pm.response.to.have.header('Content-Type', 'application/json; charset=utf-8'))" 382 | ] 383 | } 384 | }, 385 | { 386 | "listen": "prerequest", 387 | "script": { 388 | "id": "6c895279-c3c4-4366-bf00-b6a24803bb45", 389 | "type": "text/javascript", 390 | "exec": [ 391 | "" 392 | ] 393 | } 394 | } 395 | ], 396 | "request": { 397 | "method": "GET", 398 | "header": [ 399 | { 400 | "key": "Accept", 401 | "value": "application/json" 402 | } 403 | ], 404 | "body": { 405 | "mode": "raw", 406 | "raw": "" 407 | }, 408 | "url": { 409 | "raw": "{{baseURL}}/booking/11", 410 | "host": [ 411 | "{{baseURL}}" 412 | ], 413 | "path": [ 414 | "booking", 415 | "1" 416 | ] 417 | } 418 | }, 419 | "response": [] 420 | }, 421 | { 422 | "name": "Get a single booking with XML header set", 423 | "event": [ 424 | { 425 | "listen": "test", 426 | "script": { 427 | "type": "text/javascript", 428 | "exec": [ 429 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))" 430 | ] 431 | } 432 | } 433 | ], 434 | "request": { 435 | "method": "GET", 436 | "header": [ 437 | { 438 | "key": "Accept", 439 | "value": "application/xml" 440 | } 441 | ], 442 | "body": { 443 | "mode": "raw", 444 | "raw": "" 445 | }, 446 | "url": { 447 | "raw": "{{baseURL}}/booking/11", 448 | "host": [ 449 | "{{baseURL}}" 450 | ], 451 | "path": [ 452 | "booking", 453 | "1" 454 | ] 455 | } 456 | }, 457 | "response": [] 458 | }, 459 | { 460 | "name": "Get a single booking dynamically", 461 | "event": [ 462 | { 463 | "listen": "prerequest", 464 | "script": { 465 | "id": "e8cda791-42de-4b88-8b15-669fea4204a1", 466 | "type": "text/javascript", 467 | "exec": [ 468 | "pm.environment.set(\"booking_id\", Math.floor((Math.random() * 10) + 1))" 469 | ] 470 | } 471 | }, 472 | { 473 | "listen": "test", 474 | "script": { 475 | "id": "54b4b504-e764-4748-b1b1-24aec8a507a5", 476 | "type": "text/javascript", 477 | "exec": [ 478 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 479 | "", 480 | "pm.environment.unset('booking_id')" 481 | ] 482 | } 483 | } 484 | ], 485 | "request": { 486 | "method": "GET", 487 | "header": [], 488 | "body": { 489 | "mode": "raw", 490 | "raw": "" 491 | }, 492 | "url": { 493 | "raw": "{{baseURL}}/booking/{{booking_id}}", 494 | "host": [ 495 | "{{baseURL}}" 496 | ], 497 | "path": [ 498 | "booking", 499 | "{{booking_id}}" 500 | ] 501 | } 502 | }, 503 | "response": [] 504 | } 505 | ], 506 | "event": [ 507 | { 508 | "listen": "prerequest", 509 | "script": { 510 | "id": "bed71d80-d8ce-4cdb-8a88-29bf0e78c8d4", 511 | "type": "text/javascript", 512 | "exec": [ 513 | "" 514 | ] 515 | } 516 | }, 517 | { 518 | "listen": "test", 519 | "script": { 520 | "id": "44406529-93e1-4cb2-9f9f-68d1caa5cc9e", 521 | "type": "text/javascript", 522 | "exec": [ 523 | "if(pm.response.to.have.header('Content-Type') === 'application/json; charset=utf-8') {", 524 | " pm.test(\"Response data format is correct\", () => {", 525 | " var jsonData = pm.response.json()", 526 | " pm.expect(jsonData.firstname).to.be.a('string')", 527 | " pm.expect(jsonData.lastname).to.be.a('string')", 528 | " pm.expect(jsonData.totalprice).to.a('number')", 529 | " pm.expect(jsonData.depositpaid).to.be.a('boolean')", 530 | " pm.expect(jsonData.bookingdates.checkin).to.be.a('string')", 531 | " pm.expect(jsonData.bookingdates.checkin).to.match(/^\\d{4}-\\d{2}-\\d{2}$/)", 532 | " pm.expect(jsonData.bookingdates.checkout).to.be.a('string')", 533 | " pm.expect(jsonData.bookingdates.checkout).to.match(/^\\d{4}-\\d{2}-\\d{2}$/)", 534 | "});", 535 | "", 536 | "(pm.response.json().additionalneeds === undefined ? pm.test.skip : pm.test)('Customer has additional needs', () => {", 537 | " pm.expect(pm.response.json().additionalneeds).to.be.a('string')", 538 | "});", 539 | "}", 540 | "", 541 | "" 542 | ] 543 | } 544 | } 545 | ] 546 | }, 547 | { 548 | "name": "Create a new booking", 549 | "item": [ 550 | { 551 | "name": "Create a new booking with JSON", 552 | "event": [ 553 | { 554 | "listen": "prerequest", 555 | "script": { 556 | "id": "5c5cbd2d-710e-4056-88c2-20225f973bf9", 557 | "type": "text/javascript", 558 | "exec": [ 559 | "" 560 | ] 561 | } 562 | }, 563 | { 564 | "listen": "test", 565 | "script": { 566 | "id": "858da7f1-a936-4fa4-9eac-7e79f09586c3", 567 | "type": "text/javascript", 568 | "exec": [ 569 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 570 | "", 571 | "" 572 | ] 573 | } 574 | } 575 | ], 576 | "request": { 577 | "method": "POST", 578 | "header": [ 579 | { 580 | "key": "Content-Type", 581 | "value": "application/json" 582 | } 583 | ], 584 | "body": { 585 | "mode": "raw", 586 | "raw": "{\n \"firstname\" : \"Sally\",\n\t\"lastname\" : \"Brown\",\n\t\"totalprice\" : 111,\n\t\"depositpaid\" : true,\n\t\"additionalneeds\" : \"Breakfast\",\n\t\"bookingdates\" : {\n\t\t\"checkin\" : \"2013-02-23\",\n\t\t\"checkout\" : \"2014-10-23\"\n\t}\n}" 587 | }, 588 | "url": { 589 | "raw": "{{baseURL}}/booking", 590 | "host": [ 591 | "{{baseURL}}" 592 | ], 593 | "path": [ 594 | "booking" 595 | ] 596 | } 597 | }, 598 | "response": [] 599 | }, 600 | { 601 | "name": "Create a new booking with XML", 602 | "event": [ 603 | { 604 | "listen": "prerequest", 605 | "script": { 606 | "id": "eeb0b51e-2c47-4271-8399-7fd1cd934d72", 607 | "type": "text/javascript", 608 | "exec": [ 609 | "" 610 | ] 611 | } 612 | }, 613 | { 614 | "listen": "test", 615 | "script": { 616 | "id": "43617033-fef8-4fae-9cef-6ddbcf0260d6", 617 | "type": "text/javascript", 618 | "exec": [ 619 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 620 | "" 621 | ] 622 | } 623 | } 624 | ], 625 | "request": { 626 | "method": "POST", 627 | "header": [ 628 | { 629 | "key": "Content-Type", 630 | "value": "text/xml" 631 | }, 632 | { 633 | "key": "Accept", 634 | "value": "application/xml" 635 | } 636 | ], 637 | "body": { 638 | "mode": "raw", 639 | "raw": "\r\n Sally\r\n Brown\r\n 111\r\n true\r\n Breakfast\r\n \r\n 2013/02/23\r\n 2014/10/23\r\n \r\n" 640 | }, 641 | "url": { 642 | "raw": "{{baseURL}}/booking", 643 | "host": [ 644 | "{{baseURL}}" 645 | ], 646 | "path": [ 647 | "booking" 648 | ] 649 | } 650 | }, 651 | "response": [] 652 | }, 653 | { 654 | "name": "Dynamically create a new booking with JSON", 655 | "event": [ 656 | { 657 | "listen": "prerequest", 658 | "script": { 659 | "id": "ebb2be51-3874-4771-866e-9e76bee42c85", 660 | "type": "text/javascript", 661 | "exec": [ 662 | "// This is sending a request to the randomuser API to get some names ", 663 | "pm.sendRequest(\"https://randomuser.me/api/\", (err, res) => {", 664 | " var firstname = res.json().results[0].name.first", 665 | " var lastname = res.json().results[0].name.last", 666 | " pm.environment.set(\"first_name\", JSON.stringify((_.capitalize(firstname))))", 667 | " pm.environment.set(\"last_name\", JSON.stringify((_.capitalize(lastname))))", 668 | "})", 669 | "", 670 | "// This is setting a random number between 0 and a 1000 for the Total Price", 671 | "pm.environment.set(\"total_price\", _.random(0, 1000))", 672 | "", 673 | "// This is selected either True or False for the Desposit Paid property", 674 | "const depositPaid = [true, false]", 675 | "pm.environment.set(\"depositPaid\", _.shuffle(depositPaid)[0])", 676 | "", 677 | "// This is using momentjs to created a Check In and Out date in a specific format", 678 | "const moment = require('moment')", 679 | "pm.environment.set(\"check_in\", JSON.stringify(moment().format('YYYY-MM-DD')))", 680 | "pm.environment.set(\"check_out\", JSON.stringify(moment().add(_.random(1, 14), 'days').format('YYYY-MM-DD')))", 681 | "", 682 | "// This is selecting a random item from the list and adding this an an additional need", 683 | "const items = [\"None\", \"Breakfast\", \"Lunch\", \"Dinner\", \"Late Checkout\", \"Newspaper\", \"Extra Pillow\"]", 684 | "pm.environment.set(\"additional_needs\", JSON.stringify(_.shuffle(items)[0]))" 685 | ] 686 | } 687 | }, 688 | { 689 | "listen": "test", 690 | "script": { 691 | "id": "34a00c54-ce1d-4a19-9ff4-59ffc06f5e39", 692 | "type": "text/javascript", 693 | "exec": [ 694 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 695 | "", 696 | "function cleanup() {", 697 | " const clean = ['first_name', 'last_name', 'total_price', 'depositPaid', 'check_in', 'check_out', 'additional_needs']", 698 | " for(let i = 0; i < clean.length; ++i){", 699 | " pm.environment.unset(clean[i])", 700 | " }", 701 | "}", 702 | "cleanup()", 703 | "" 704 | ] 705 | } 706 | } 707 | ], 708 | "request": { 709 | "method": "POST", 710 | "header": [ 711 | { 712 | "key": "Content-Type", 713 | "value": "application/json" 714 | } 715 | ], 716 | "body": { 717 | "mode": "raw", 718 | "raw": "{\r\n\t\"firstname\" : {{first_name}},\r\n\t\"lastname\" : {{last_name}},\r\n\t\"totalprice\" : {{total_price}},\r\n\t\"depositpaid\" : {{depositPaid}},\r\n\t\"additionalneeds\" : {{additional_needs}},\r\n\t\"bookingdates\" : {\r\n\t\t\"checkin\" : {{check_in}},\r\n\t\t\"checkout\" : {{check_out}}\r\n\t}\r\n}" 719 | }, 720 | "url": { 721 | "raw": "{{baseURL}}/booking", 722 | "host": [ 723 | "{{baseURL}}" 724 | ], 725 | "path": [ 726 | "booking" 727 | ] 728 | } 729 | }, 730 | "response": [] 731 | }, 732 | { 733 | "name": "Dynamically create a new booking with XML", 734 | "event": [ 735 | { 736 | "listen": "prerequest", 737 | "script": { 738 | "id": "f4db6ba1-d350-4d89-a62b-97543a0886df", 739 | "type": "text/javascript", 740 | "exec": [ 741 | "pm.sendRequest(\"https://randomuser.me/api/\", (err, res) => {", 742 | " var firstname = res.json().results[0].name.first", 743 | " var lastname = res.json().results[0].name.last", 744 | " pm.environment.set(\"first_name\", JSON.stringify((_.capitalize(firstname))))", 745 | " pm.environment.set(\"last_name\", JSON.stringify((_.capitalize(lastname))))", 746 | "})", 747 | "", 748 | "pm.environment.set(\"total_price\", _.random(0, 1000))", 749 | "", 750 | "const depositPaid = [true, false]", 751 | "pm.environment.set(\"depositPaid\", _.shuffle(depositPaid)[0])", 752 | "", 753 | "const moment = require('moment')", 754 | "pm.environment.set(\"check_in\", JSON.stringify(moment().format('YYYY-MM-DD')))", 755 | "pm.environment.set(\"check_out\", JSON.stringify(moment().add(_.random(1, 14), 'days').format('YYYY-MM-DD')))", 756 | "", 757 | "const items = [\"None\", \"Breakfast\", \"Lunch\", \"Dinner\", \"Late Checkout\", \"Newspaper\", \"Extra Pillow\"]", 758 | "pm.environment.set(\"additional_needs\", JSON.stringify(_.shuffle(items)[0]))" 759 | ] 760 | } 761 | }, 762 | { 763 | "listen": "test", 764 | "script": { 765 | "id": "34a00c54-ce1d-4a19-9ff4-59ffc06f5e39", 766 | "type": "text/javascript", 767 | "exec": [ 768 | "pm.test(\"Status code is 200\", () => pm.response.to.have.status(200))", 769 | "", 770 | "function cleanup() {", 771 | " const clean = ['first_name', 'last_name', 'total_price', 'depositPaid', 'check_in', 'check_out', 'additional_needs']", 772 | " for(let i = 0; i < clean.length; ++i){", 773 | " pm.environment.unset(clean[i])", 774 | " }", 775 | "}", 776 | "cleanup()", 777 | "" 778 | ] 779 | } 780 | } 781 | ], 782 | "request": { 783 | "method": "POST", 784 | "header": [ 785 | { 786 | "key": "Content-Type", 787 | "value": "text/xml" 788 | }, 789 | { 790 | "key": "Accept", 791 | "value": "application/xml" 792 | } 793 | ], 794 | "body": { 795 | "mode": "raw", 796 | "raw": "\r\n {{first_name}}\r\n {{last_name}}\r\n {{total_price}}\r\n {{depositPaid}}\r\n {{additional_needs}}\r\n \r\n {{check_in}}\r\n {{check_out}}\r\n \r\n" 797 | }, 798 | "url": { 799 | "raw": "{{baseURL}}/booking", 800 | "host": [ 801 | "{{baseURL}}" 802 | ], 803 | "path": [ 804 | "booking" 805 | ] 806 | } 807 | }, 808 | "response": [] 809 | } 810 | ] 811 | } 812 | ], 813 | "event": [ 814 | { 815 | "listen": "prerequest", 816 | "script": { 817 | "id": "dc3d0ab5-1b11-4abe-b203-7240dc6b3fd7", 818 | "type": "text/javascript", 819 | "exec": [ 820 | "" 821 | ] 822 | } 823 | }, 824 | { 825 | "listen": "test", 826 | "script": { 827 | "id": "3dfdde3a-f257-4fde-8b54-9301f0e4f6c8", 828 | "type": "text/javascript", 829 | "exec": [ 830 | "" 831 | ] 832 | } 833 | } 834 | ] 835 | } 836 | -------------------------------------------------------------------------------- /tests/Restful_Booker_Environment.postman_environment.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "557514bb-2bee-4e66-bf94-0fe5af12e1a7", 3 | "name": "Restful_Booker_Environment", 4 | "values": [ 5 | { 6 | "value": "https://restful-booker.herokuapp.com", 7 | "key": "baseURL", 8 | "enabled": true 9 | } 10 | ], 11 | "_postman_variable_scope": "environment", 12 | "_postman_exported_at": "2018-08-23T18:39:11.962Z", 13 | "_postman_exported_using": "Postman/6.2.5" 14 | } --------------------------------------------------------------------------------