0% found this document useful (0 votes)
278 views72 pages

Automation API TM14 PDF

API testing is important for early defect detection, contract validation, stopping faulty builds, and providing fast and reliable results. Key aspects of API testing include testing functionality, security, performance, error handling, and response content. Common tools for API testing include Postman, SOAPUI, and Curl. The Swagger API documentation tool can be used to design, document, and test APIs. Key elements to test include request structure, types, resources, headers, bodies, status codes, and content type. Variables can be used to store session values and environment configurations in Postman for testing. Tests can validate response time, status codes, headers, bodies, and JSON values.

Uploaded by

MihaiLeonte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
278 views72 pages

Automation API TM14 PDF

API testing is important for early defect detection, contract validation, stopping faulty builds, and providing fast and reliable results. Key aspects of API testing include testing functionality, security, performance, error handling, and response content. Common tools for API testing include Postman, SOAPUI, and Curl. The Swagger API documentation tool can be used to design, document, and test APIs. Key elements to test include request structure, types, resources, headers, bodies, status codes, and content type. Variables can be used to store session values and environment configurations in Postman for testing. Tests can validate response time, status codes, headers, bodies, and JSON values.

Uploaded by

MihaiLeonte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

API testing

Why API automation required?

1. Early defect detection – Most of the time UI layer uses the REST Web services to fetch data
from back end and do the business validation
2. Contract validation – Suppose a scrum team changes some parameters related to a web
service that was developed in the last sprint and they failed to notify your team, so this situation
leads to a production defect of that functionality which you have not touched in the current sprint.
Validating contract is a necessary thing
3. Stops the further build – If a test failed in automation environment, it stops the further build
unless that error is rectified, which assured that only good build are passed to manual testers for
testing
4. Fast result – API Automation test jobs take very less time to complete one test testing cycle.
Very helpful in CICD (Continuous Integration, Continuous Deployment) environments
5. Reliable – If tests are passed without any issue then we can be sure that business layer is not
having any defect
Why API automation required?
RESTful services

● REST - REpresentational State Transfer


● RESTful service - service built on the REST architecture
● Used to build web services that are easy to scale and maintain
● Enables web services build on different programming languages to
communicate with each other
Request structure

● A Uniform Resource Locator (URL) that defines the location of the request
● An HTTP verb that describes what action should be taken
● HTTP headers that provide information to the server about the request
● A request body that provides further details for the request (this can
sometimes be empty)

Request URL: https://petstore.swagger.io/v2/pet/100


Request method: GET
Status code: 200
Version:HTTP/2.0
Request structure

Request URL: https://petstore.swagger.io/v2/pet/89898989


Request method: GET
Status code: 200
Request types

● POST
○ Creates a new resource
● PUT
○ Modifies an existing resource
● PATCH
○ Modifies only the sent parameters
● GET
○ Read details of an existing resource
● DELETE
○ Delete an existing resource
REST resources

● They are based on resources, which are specified in URLs


○ https://petstore.swagger.io/v2/pet/89898989
○ Here 89898989 is the ID of a pet in petstore
○ Executing a GET request will return the information about the pet
that has the ID = 89898989
○ Executing a DELETE request would remove the pet with ID =
89898989
● Other blog post resources: https://developer.wordpress.org/rest-
api/reference/
REST resources

● Consider, for example, a web service for ordering books. Your data might be
organized into customers, which contain orders, which in turn contain individual
books. The resource URL might look like this:
○ http://server.test:8080/order_api/{customer_id}/{order_id}/{book_id}
● Sending a DELETE request to this URL might remove a book from an existing
order, while sending a GET request to this URL might retrieve the details of a
particular book (such as if it is on back order or out of stock).
Request/response headers

● they store important information that can help you during


testing
● headers can be found on both request and response level
● usually you will find headers that store:
○ content type (JSON, XML, plain text)
○ authorization (connection details)
○ cache control (cache response for future access)
Request/response headers
Request/response headers
Request body

● Sometimes, in order to create or modify a resource, you will


have to specify its details when sending request
● The body can be a JSON or XML structure
Response body

● Requests usually return a body as response, which can be


either JSON, XML or plain text format
● This is the information a tester will use to validate that a
resource has correct details
Response status

● 2xx Success (200 OK, 201 Created, 204 No Content)


● 3xx Redirection (301 Moved Permanently)
● 4xx Client errors (401 Unauthorized, 403 Forbidden, 404 Not Found)
● 5xx Server error (500 Internal Server Error, 503 Service Unavailable)

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
RESTful services - JSON

● JSON - JavaScript Object Notation


● Extended from Javascript, can be used with any programming
language, easy to read/write
● Filename extension is .json
● Internet media type is application/json (set in Content-Type
and Accept headers)
● Used for transmitting structured data by web services
RESTful services - XML

● XML - Extensible Markup Language


● Tags are used to identify, organize and store the data
● No predefined tags, you can extend your own descriptive tags
● Easy to read/write by humans, easy to process by other
services
API Testing - JSON, XML

XML JSON

<Pet> {
<id>12345</id> "id": 12345,
<name>wantsomeDog</name> "name": "wantsomeDog",
<status>available</status> "status": "available"
</Pet> }
API Testing

● Application Programming Interface, space where all


programming logic is exposed
● usually developed before GUI -> early testing
● data exchange between two separate systems
● no concern about the look&feel of an application
● no input/output directly, but calls sent by a software
API Testing
API testing - What to test

● functionality: is resource created, are details correct?


● security: access levels, authentication
● performance: check response time
● error handling: invalid data is processed properly
● response content well formed: valid JSON or XML content
Tools for API testing

● Postman
● SOAPUI
● Curl
● Runscope
● Cfix
API Testing - SWAGGER

● http://petstore.swagger.io/
● Swagger is a suite of API developer tools
● It describes an API in a common language, easy to
understand
● Can be used to design, document and write code
for an API
● Swagger-UI is used to interact with the APIs
API Testing - SWAGGER

Use API Key authorization


API Testing - SWAGGER

● Perform the following requests:


○ Add a new pet to the store
○ Find pet by ID
○ Update an existing pet
○ Delete a pet
API Testing - SWAGGER - Add pet

● POST /pet - Add a


new pet to the store
● Content-Type:
application/json
API Testing - SWAGGER - Add pet

● POST
● http://petstore.swagger.io/v2/pet
● -H "content-type: application/json"
● -H "api_key: special-key"
API Testing - SWAGGER - Add pet

● Exercises
○ Try to add a pet without specifying the status
element
○ Perform request after removing the first
bracket in JSON object
API Testing - Get pet by ID

● GET /pet/<petID>
● http://petstore.swagger.io/v2/pet/555
API Testing - SWAGGER - Get pet by ID

● Exercises
○ Perform request using an ID that does not exist
○ Try to execute call with empty value for ID
API Testing - SWAGGER - Update pet

● PUT
● Update an existing PET
API Testing - SWAGGER - Update pet

● Exercises
○ Perform update request using an ID that does
not exist
○ Execute request after removing a comma from
JSON object
API Testing - SWAGGER - Delete pet

DELETE /pet/<petId>
API Testing - Delete pet

● Exercises
○ Perform request using an ID that does not exist
○ Try to execute call with empty value for ID
API Testing - Postman

● https://www.getpostman.com
● Custom HTTP calls can be performed
● Response body, response status
● Create collections, call them anytime
● Write tests and save environment for later use
API Testing - Postman
API Testing - Postman
Environments
Session values
Variables
Environment variable

Setting
pm.environment.set(“variable_key”, “variable_value”);

Getting
var value = pm.environment.get(“variable_key”);
Global variables

Setting
pm.globals.set(“variable_key”, “variable_value”);
Getting
pm.globals.get(“variable_key”);
Clear
pm.globals.unset(“variable_key”);

Search for the variable across globals and the active environment

var value = pm.variables.get(“variable_key”);


Variable scopes
Pre request scripts and tests
Pre request scripts and tests
Creating Variables using Pre Request Script
How to Create Postman Tests

Response time is less than 200ms


pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Status code is 200
pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});
How to Create Postman Tests

Code name contains a string


pm.test(“Status code name has string”, function () {
pm.response.to.have.below(“Created”);
});
Successful POST request status code
pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
How to Create Postman Tests

Check if response body contains a string


pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include.(“search string”);
});

Check if response body is equal to a string


pm.test(“Body is correct”, function () {
pm.response.to.have.body(“body_string”);
});
How to Create Postman Tests

Check for a JSON value


pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql.(100);
});
Content-Type header is present
pm.test(“Content-Type header is present”, function () {
pm.response.to.have.header(“Content-Type”);
});
Postman Tests
How to Create Collections

Collection are groups of requests that can be run together as


a series of requests against a corresponding environment

Automate API testing

Make better use of scripts: pass data between requests

Build workflows that mirror your APIS’ use case


How to Create Collections
How to Create Collections
How to Run Collections using Collection Runner
How to Run Collections using Collection Runner
How to Run Collections using Collection Runner
How to Run Collections using Collection Runner
NEWMAN

NEWMAN is a command line collection runner for POSTMAN

You can configure continuous integration tools to respond to


NEWMAN’s exit status codes and correspondingly pass or fail a build
How to Run Collections using Newman

Step 1) Install nodejs using this link: http://nodejs.org/download/

Step 2) Open the command line and enter

npm install -g newman


How to Run Collections using Newman
How to Run Collections using Newman
How to Run Collections using Newman

Step 7) Environment should now be exported to the same local directory as Collection.

Step 8) Now go back to command line and change the directory to where you have
saved the collection and environment.

cd C:\Users\Asus\Desktop\Postman Tutorial
Step 9) Run your collection using this command:

newman run PostmanTestCollection.postman_collection.json -e


Testing.postman_globals.json
Run results should now appear such as below.
How to Run Collections using Newman
POSTMAN - Summary

▪ Postman is currently one of the most popular tools used in API testing
▪ Accessibility, Use of Collections, Collaboration, Continuous Integration,
are some of the Key features to learn in Postman
▪ It's recommended you create an account in Postman, so your
collections are available online
▪ You can parameterize request in Postman
▪ You can create Tests to verify a postman request
▪ Collections can be run using Newman or Collection Runner
JENKINS
JENKINS
JENKINS DEMO
References and Recommended Reading

● REST
○ http://www.restapitutorial.com/lessons/whatisrest.html
● Swagger
○ https://swagger.io
● Postman
○ https://www.getpostman.com/
References and Recommended Reading

● Postman documentation
○ https://www.getpostman.com/docs/
● Postman test examples
○ https://www.getpostman.com/docs/postman/scripts/test_
examples
How to Use JMeter for Performance & Load Testing

https://www.guru99.com/jmeter-performance-
testing.html
Questions?

You might also like