0% found this document useful (0 votes)
8 views

API Workshop Slides

Uploaded by

libinlong123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

API Workshop Slides

Uploaded by

libinlong123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

formerly WeTest

API Testing Workshop


by Vincent Dirks
Let’s test this workshop...
How might things go wrong today?
Fiserv code of conduct
● sign attendance sheet so that we know to rescue you in case of fire

● know your escape route down the stairs

● stay within vicinity of level 6 conference room no snooping around

● use a visitor swipe card to go to the toilet if you forget call 021-0269-6216

● be mindful of people working today/tonight be glad it isn’t you

● use guest wifi FISV_HotSpot B00tes_Void0219!


Fiserv evacuation procedure
WHEN YOU HEAR THE FIRE ALARM:
Evacuate immediately, via the closest fire exit, using the fire stairs.
DO NOT take anything with you. E.g. Coffee, phones, bags
In a situation where you cannot walk down the stairs, let a fire
warden know and wait inside the stairwell. Someone will
come and get you.
Once you’re out of the building, go to the assembly area
and wait for the “All Clear” from the fire wardens before
returning to the office.
Please don’t cross the road to go to the shops or café.
sign in

exit
toilets
level 6

conference room
Let’s team up
● Similar size groups
● Spread the skills between groups
● No groups less than 4 people
● One group per table

Rainbow of skills
please arrange yourselves in order of programming & javascript skills

1. never seen code in my life 6. program occasionally in javascript


2. seen some code before 7. written a program in node.js
3. Code looks cool, not sure how to start 8. I love node, it’s soo cool
4. I did some programming at school 9. I am a dev
5. I occasionally program now 10. I will solve all your programming problems

Vincent Dirks
Overview
● Create a Web API Server running on your own laptop - This is what dev’s do
● Use Postman to test Web API

Web API Postman


● Install Visual Studio Code ● Install Postman
● Install Node.js ● Create GET request
● Initialise new project ● Save it to a new collection
● Install npm dependencies
● Create files & copy code
● Start Web API Server

Vincent Dirks
Install Postman
from: https://www.getpostman.com/downloads/
and follow OS specific instructions
Install Visual Studio Code
from: https://code.visualstudio.com/Download

install support for javascript


Install Node.js
from https://nodejs.org/en/download/
and follow OS specific instructions

Open a terminal command prompt


eg. powershell or bash

Check your $PATH


Windows (powershell) Get-ChildItem Env
- should to include C:\Program Files\nodejs

MAC OSX (bash) echo $PATH


- should to include /usr/local/bin

To fix it for your OS


- google it

Check Node is installed


node -v
Initialise new project
● create project folder eg C:\workshop

● in VS Code
○ open terminal window
■ click the setting cog wheel
■ open the command palette
■ search for ‘terminal’
■ select ‘Create new integrated terminal in active window’

○ execute commands
■ cd \workshop
■ npm init
● should be able to accept all defaults
● this creates package.json file inside workshop folder
● you will be able to update above answers in this file at any time
Install npm dependencies
execute commands
npm install express --save
npm install babel-cli --save
npm audit fix (if needed)
npm install babel-preset-es2015 --save
npm audit fix (if needed)
may still have vulnerability ... that's ok

These commands fetch the libraries from the npm


repository, installs them for your project, and adds the
dependencies to the package.json file.

Check out, the package.json file and see that the


dependencies have been added.
Create files & copy code
in workshop folder create
● file: app.js
● file: .babelrc
● folder: database
○ file: db.js

for each file copy the content from the link below and
paste it into your files
● .babelrc
● app.js
● database/db.js

feel free to have a poke around the code and see what
you can learn, and ask questions from your teammates
and presenters
Start Web API Server 🙂
execute command
node_modules/.bin/babel-node app.js

open page in browser


http://localhost:5000/api/v1/person/info

too cool!!
you’ve programmed your first Web API
well done! Hope this feels good!!
Postman
● open postman application
● paste http://localhost:5000/api/v1/person/info into the request
● click send

● save the request to a new collection


modifications - nodemon
● change some data in the database file db.js
● save it
● refresh http://localhost:5000/api/v1/person/info in the browser
⇒ It is not refreshed 😠
● stop the server with ctrl^c & restart it
● refresh the page in your browser
⇒ what a pain ... 😴
● execute
npm install nodemon --save-dev
● Open package.json and edit “scripts” element to add
"start": "node_modules/.bin/nodemon app.js --exec babel-node --"
● start app with
npm run start
● modify the db.js file again
⇒ server restarts automatically
⇒ refresh browser, and data should be updated
create POST endpoint
● execute
npm install body-parser --save
(npm audit fix if needed, its ok if it
didn't fix)
● update app.js

● create POST request in Postman


○ copy GET request
○ change method to POST
○ add body
{
"name": "Ada Lovelace",
"address": "Marylebone, London, England"
}
○ save & send it

● verify it with original GET request


end of session one
at home: play with everything you have learned
start of session two
let’s play Postman
Adding tests - code snippets
● click Test tab and expand Snippets panel
● scroll down to & click “Status code: Code is 200”

● click Send

in your teams
❖ add more tests to the GET request
❖ add tests to the POST request
Collection Runner runs all requests in a collection in order
● copy the GET request & drag to after the POST request
● click Runner (top left of Postman window)
● select your collection
● click Run Workshop
Postman Variables
you can create variables in Postman and use them anywhere in your request!
you can even update them in the code

use variables for


● storing commonly used parameters
○ eg. a username, and their password
○ the root URL for the test environment
● remembering session related data
○ eg. the ID of a recently created record
○ the IDs of a related records
■ eg. a user’s list of transactions

variables can be stored in variables (globals and environments) can be


● globals ● exported to files
○ one only instance ○ saved to version control
● environments ● imported from files
○ multiple named environments ● edited as JSON
○ selectable/switchable ● shared with colleagues
● used by Newman cli
extend the API
so that we can play with some variables in Postman
//create get by index
Let’s add one more endpoint to fetch individual records app.get('/api/v1/person/info/:id', (request, response) => {
● Copy and paste the code into app.js const record = db.find(record => {record.id ===
Number(request.params.id)})
record && response.status(200).send({
success: 'true',
in your teams message: 'information retrieved successfully',
db: record
● create a new request in Postman });
● add some tests !record && response.status(404).send({
success: 'false',
message: `record id ${request.params.id} not found`
});
in Postman });
● go to edit the Globals in Postman
● add a new variable ‘index’ and give it value 1
● change the new request’s url to
http://localhost:5000/api/v1/person/info/{{index}}
● send it!
● change the value in the globals & send again

● add code snippet to the POST request to save the index


● run the collection
==============================================
Postman play
● console logging & external datafiles
● exporting the entire collection & version control
● running the collection in Newman CLI
● running Newman as a node.js module in node.js script
● creating a code library
● setting nextRequest
● using tv4 schema validator
● newman reporters & custom reporters

lastly what kind of workshop would we like next?

You might also like