Skip to content

In this workshop you will learn how to automate collections runs for API testing with Postman

License

Notifications You must be signed in to change notification settings

dinakazakevich/postman-workshop-2022

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Postman Workshop 2022

Goal is to learn to test REST APIs with Postman collections, learn to run collections with Newman collection runner and see what an API look like in code.

✅ You will learn

  • To understand and test REST APIs
  • What an API looks like in code (not that scary!)
  • To use Postman's out-of-the box basic automation capabilities
  • Use Newman to run Postman collection in your command line
  • Bonus: Hook up Postman and Newman to Jenkins (only if we have the time).

🧱 Prerequisites

  • UNIX terminal on local machine (you might need configure it separately if you are running on Windows)
  • Postman installed locally https://www.postman.com/
  • Basic understanding of software architecture: backend, frontend, database
  • Ability to read basic javascript not necessarily to write it
  • Basic UNIX console knowledge: running basic commands

🔧 Technologies you will use


#testing4good

🌎 Testing for Good enables great test automation engineering while shaping a more equitable society.👩‍💻 Today, we're asking for donations for Environmental Working Group


Instructor: Dina Kazakevich

I'm a Senior QA Analyst at Adalo and a former mentor at Practicum by Yandex where I taught over 100 aspiring software testers. I've attended the BBST Foundations and the Instructors courses by AST as well as courses by James Bach and Michael Bolton. I'm passionate about remote work and slow traveling and I run an international community of remote tech professionals Remote Ever After (currently in Telegram but migrating to Reddit). I've been living out of my suitecase and traveling the world for over 5 years now.

✨ Bonus task: Set up a local API service with Express.js

  1. Create an express js app https://expressjs.com/en/starter/generator.html
    1. Create a dedicated directory and run this in the terminal
    2. npx express-generator
    3. Install dependencies
    $ cd myapp
    $ npm install
  2. Run the expressjs-sample project locally, look through the routes/users.js
    $ npm start
  3. Access it at http://localhost:3000/
  4. Replace you routes/user.js with the respectful file from https://github.com/dinakazakevich/expressjs-sample
  5. Create a data directory in the root of your project, and copy over my users.json from https://github.com/dinakazakevich/expressjs-sample
  6. Look at how API endpoints look like, search for router.get, router.post, router.delete
  7. Start the express-sample service locally by
  8. Test with Postman, specifically the POST request
  9. Update the POST endpoint to return result: New user aded instead of New user created
  10. Restart the service and run POST request again to confirm it was updated in the request response.

⚙️ Setup

  1. Install Postman https://www.postman.com/
  2. Sign up for a free Adalo account https://www.adalo.com/?via=dina
  3. Logging into your Adalo account
  4. Follow the link and clone this sample Adalo app — we will be using its collections
  5. Enable Free Integrations Trial in Adalo Editor: Settings > App Access > Start Trial
  6. Generate API Key

Creating Postman API requests

  1. Create a simple GET request for the Adalo app Applications collection
    • Hint: You will need to set up authorization
  2. Create a simple POST request for the Adalo app Applications collection, create a new application
    • Simple ☑️: Set Full Name to static values
    • Simple ☑️: Set the application.id to the environment applicationId
    • Hard 💪: Add dynamic variables for First Name, Last Name -- see Postman documenation for details
  3. Create a simple PUT request to update the status of the application
  4. Create a simple DELETE request to delete the application you created in step #2
  5. Add automatic checks to each request Postman documentation
    • Simple ☑️: Check the response status code 200 on each of them
    • Hard 💪: Only for GET request, Check that the applicationId created with POST request is actually present in the response of the GET request.
    • Hard 💪: make every request to be independent from one another. Hint: use pre-request scripts and tests to create and cleanup data
  6. Run the collection in Postman Collection Runner — Postman documentation
  7. Set up a Monitor for the Collection to run every hour — Postman documentation
  8. There is a sample postman collection with Adalo public API set up in ./postman-collections/adalo-postman-collection IMPORTANT: Please use your own Adalo app tokens and Authentication key to run this locally.

Running collections in command line using Newman collection runner

  1. We can also run the Postman Collection in the command line using Newman Open Source Collection Runner
  2. Install Newman
$ npm i newman -g
  1. Export Collection and Environment and save them locally
  2. Run the collection
$ newman run PostmanCollection.json -e environment.json

Generating HTML reports

  1. Install htmlextra https://www.npmjs.com/package/newman-reporter-htmlextra
$ npm i newman-reporter-htmlextra
  1. Run a collection with newman and generate an html report
$ newman run collection.json -e environment.json -r htmlextra

Note: You might need to install/update Node.js and npm

🛣️ Jenkins Pipeline

  1. Install Jenkins https://www.jenkins.io/doc/book/installing/
  2. Troubleshoot Permissions denied error by running
$ sudo chown -R $(whoami) /path/to/restricted/directory
  1. Install Node.js and Newman in Jenkins: more info here https://learning.postman.com/docs/running-collections/using-newman-cli/continuous-integration/
    • Go to your Jenkins server (it's at http://localhost:8080 by default if you are running it locally) and sign in.
    • Go to Manage Jenkins > Manage Plugins and install the NodeJS plugin.
    • Go to Manage Jenkins > Global Tool Configuration and under NodeJS, select Add NodeJS.
    • Enter a name for the Node.js installation.
    • In Global npm packages to install, enter newman.
    • Select Save.
  2. Setup Jenkins
    • With Jenkins running, go to http://localhost:8080 and sign in.
    • On the Dashboard page, select New Item on the left sidebar to create a new job.
    • Select a Freestyle project from the options. Name your project, and select OK.
    • In General > Build, add a build step in the project, and choose Execute Shell. The build step executes a shell command. Enter a shell command to run, such as newman run ~/Desktop/jenkins_demo_postman_collection.json.
    • In Build Environment > Build Environment, select Provide Node & npm bin/ folder to PATH and choose the NodeJS install you configured with Newman.
    • Select Save to finish creating the project.
  3. Run the Build

Additional task — Airtable API

  1. Repeat teh exercise for Airtable Database API
  2. Sign up for a free Airtable account
  3. Clone this app to your account https://airtable.com/shrECwvJjBJzTgiQs/tblymCw8pWMTvUccW/viwtmKb10IVAhPY5F
  4. Generate API key in https://airtable.com/account

Tasks for Airtable

  1. Created simple GET/POST/DELETE reuests
  2. First loop them to clean after the run
  3. Second, make them run an stnadalone tests that create and delete data after themselves.
  4. GET:
    • pre-scripts: (1) create a new record, (2) save the recordId to an environment variable
    • tests: (1) response status 200, (2) response contains new applicant created, (3) delete the record created
  5. POST:
    • pre-scripts: n/a
    • tests: (1) check response status code is 200, (2) Set Environment variable applicantId to the id of the user created, (3) Test that the user we created was returned in the response, (4) Send DELETE request to delete the user you have just created
  6. DELETE
    • pre-scripts: (1) Send POST request to create a new user and save the id of the applicant to an environment variable
    • tests: (1) Status code is 200
  7. Run the Collection manually
  8. Set up a Monitor to run every hour
  9. Run the collection via command line using Newman
  10. Run the collection via command line using Newman and generate and html report
  11. Answers to the above are in a sample collection in ./postman-collections/airtable-postman-collection IMPORTANT: Please use your own Airtable app tokens and Authentication key to run this locally.

Additional reading

  1. Difference between Postman CLI vs Newman Collection Runner

About

In this workshop you will learn how to automate collections runs for API testing with Postman

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published