web api test automation using frisby & node.js
TRANSCRIPT
Agenda
• Introduction
• This session discusses:– Web API Test
– Frisby
– A case study from SHIFT ASIA
• Technology discussed:– Node.js
– REST APIs with JSON
• Code download available at:– https://bitbucket.org/ShiftAsia/api_test
2
Bao Nguyen
• R&D Manager at SHIFT ASIA
• Full-stack developer
Quang To
• Test Engineer at SHIFT ASIA
• Experience in various aspects of Testing
3
Introduction
Objectives
Upon completion of the meetup, audiences should be able to:
• Understand Web APIs
• Differentiate SOAP from REST Web APIs
• Learn well-known API Test tools
• Develop API Test Automation using Frisby and Node.js
4
SOAP
• Simple Object Access Protocol
• Standardized protocol
• Rely heavily on XML
• Use WSDL (Web Service Definition Language) as a contract between the provider and the consumer of the service
REST
• REpresentational State Transfer
• Architectural style
• JSON is preferred
• Use HTTP standard verbs, such as: GET, POST, PUT, DELETE
8
SOAP vs. REST
Why API Test?
• System complexity increased
Multi-tiered model with private APIs making up the middleware
• A huge demand of public APIs
• Getting feedback as soon as possible
9
Web REST API Call
11
1Request
Url + Verb
2Response
Status code + Header + Body
http://swapi.co/api/people/1/
allow →GET, HEAD, OPTIONScontent-type →application/jsonvary →Accept
{"name": "Luke Skywalker",“height": "172",“mass": “77",“hair_color": “blond“,…}
HTTP status →200 OK
Frisby – Overview
• REST API testing framework built on Node.js
• Simple
• Fast
• Continuous Integration
• http://frisbyjs.com/
• npm install --save-dev frisby
15
API Verbs
16
HTTP Verb CRUDEntire Collection
(e.g. /users)Specific Item
(e.g. /users/{id})
POST Create 201 (Created) 404 (Not Found), 409 (Conflict) if resource already exists.
GET Read 200 (OK) 200 (OK), single user. 404 (Not Found), if ID not found or invalid.
PUT Update/Replace 404 (Not Found) 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid.
PATCH Update/Modify 404 (Not Found) 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid.
DELETE Delete 404 (Not Found) 200 (OK). 404 (Not Found), if ID not found or invalid.
Frisby – Methods
• Expectations
– expectStatus()
– expectHeader()
– expectHeaderContains()
– expectHeaderToMatch()
– expectJSON()
– expectBodyContains()
– expectJSONTypes()
– expectJSONLength()
– expectMaxResponseTime()
18
Status code
Returned value
Returned value
Returned value
Returned value
Returned value
Schema
Schema
Response time
Frisby – Challenges
• Asynchronous
• Demo
21
Test API 1 API 2Request 1
Response 1
Request 2
Response 2
Response values transferred
SHIFT ASIA – Methodology
27
Requirements User Stories
Test Cases
Test Scripts
Test Designer
Test Engineer
Tester
Tester
#AutomateBuild#AutomateTestExecution
#AutomateReporting
SHIFT ASIA – Case Study
28
TEST SCRIPTTester#TestDevelopment
SCRIPT GENERATIONTest Engineer#Utility#Helpers
HARD-CORETest Architect#StandardStructure#NamingConvention#Settings#Modules Node.js
FrisbyJasmine-node
shelljs
Helper modules
Test in Yaml Data in Yaml
Yaml to JSON files
Run API tests