Web Services and API’s
What are Web Services?
Web services are a standardized way or medium to propagate communication between
the client and server applications on the World Wide Web.
What is an API?
API is the acronym for Application Programming Interface and it that allows two
applications to talk to each other. Each time you use an app like Facebook, send an instant
message, or check the weather on your phone, you’re using an API.
What is difference between API and Web Services?
API and Web service serve as a means of communication
Web service facilitates interaction between two machines over a network
An API acts as an interface between two different applications so that they can
communicate with each other
A Web service is merely an API wrapped in HTTP
How to access Web Services
Popular Web Services Protocols
SOAP-Simple object access protocol
REST-Representational State Transfer
SOAP-Simple object access protocol
SOAP is an XML-based protocol for accessing web services over HTTP. It has some
specification which could be used across all applications. In other words is a definition of
how web services talk to each other or talk to client applications that invoke them.
The XML used to make requests and receive responses in SOAP can become extremely
complex
Part of the magic is the Web Services Description Language (WSDL). This is another file
that’s associated with SOAP.
One of the most important SOAP features is built-in error handling.
An interesting SOAP feature is that you don’t necessarily have to use it with the
HyperText Transfer Protocol (HTTP) transport.
REST- (REpresentational State Transfer)
Architectural style for developing web services
Popular due to its simplicity and the fact that it builds upon existing systems and
features of the internet's HTTP in order to achieve its objectives
REST-based interactions all communicate their status using standard HTTP status codes
REST is also a language-independent architectural style
RESTful web services can be written using any language
REST is its pervasiveness
Comparing Rest vs SOAP
Rest provides Lighter weight alternative
Rest doesn’t have to use XML to provide the response
Small learning curve for Rest
SOAP works well in distributed enterprise environment.
Built in error handling
REST API
Google place search API
Location->Newyork
Radius->50
Key->
How to build rest request URL
1.What is the end point?
2.What are the types of rest request?
3.Headers and cookies?
End point->
1.BaseURL/Hostname-Website that we are trying to reach
2.Resource
3.Parameters
Endpoint->Baseurl/resource?Parameters
Different types of rest request?
1. Get-Retrieve from the server.
Baseurl/resource?Parameters
Weather.com/Weather?Location=newyork
2. Post-to post new data to the api
3. Put->To modify existing Data
4. Delete->to delete existing data
Headers in rest request
1.Authentication
Session id
2.Content Type:
Plain text/XML/JSON
https://developers.google.com/places/web-service/search
Manual Testing tool for API’s
Postman- Postman is a Google Chrome app for interacting with HTTP APIs. It presents you
with a friendly GUI for constructing requests and reading responses.
Download Link: https://www.getpostman.com/downloads/
First API- Google map search API
https://developers.google.com/places/web-service/search
Base url: https://maps.googleapis.com/maps/api
Resource: /place/nearbysearch/json
Parameters: ?location=-
33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key= YOU
R_API_KEY
SWAPI API
Link : https://swapi.co/
Base url: https://swapi.co/api
Resources: /people/
Endpoint: https://swapi.co/api/people/
Employee API
http://dummy.restapiexample.com/
Get: http://dummy.restapiexample.com/api/v1/employees
http://dummy.restapiexample.com/api/v1/employee/{id}
Post: http://dummy.restapiexample.com/api/v1/create
{"name":"NextGen5","salary":"12345","age":"25"}
Put: dummy.restapiexample.com/api/v1/update/{id}
{"name":"NextGen6","salary":"12345","age":"25"}
Delete:
http://dummy.restapiexample.com/api/v1/delete/8597
Automation of Rest API’s
Rest API’s can be mainly automated using the
following Libraries in Robot framework
Request Library
HTTP Library
We will be covering HTTP Library since it has
more keywords
Pre-Requisites for HTTP Library
WebTest
Pip install WebTest
WebOb
Pip install WebTest
jsonpointer
pip install jsonpointer
jsonpatch
pip install jsonpatch
beautifulsoup
pip install beautifulsoup
HTTP library
pip install robotframework-httplibrary
HTTP Library Keywords
1. Create Http Context-
Sets the HTTP host to use for future requests.
host=None, scheme=http
2. GET
Issues a HTTP GET Request
url
3. Response Status Code Should Equal
Fails if the response status code of the previous request was not the specified one.
Status code
4. Get Response Status
Returns the response status line (e.g. "200 OK" or "404 Not found")
5. Log Response Status
Logs the response status line
Specify log_level (default: "INFO") to set the log level.
6. Response Status Code Should Not Equal
Fails if the response status code of the previous request is equal to the one
specified.
Status code
7. Post
Issues a HTTP POST request.
url
8. Set Request Body
Set the request body for the next HTTP request.
Body
9. Log Response Headers-
Logs the response headers, line by line.
Specify log_level (default: "INFO") to set the log level.
10. Response Should Have Header
Fails if the response does not have a header named header_name
header_name
11. Response Should Not Have Header-
Header_name
12. Response Header Should Equal
Fails if the value of response header header_name does not equal expected. Also fails if
the last response does not have a header_nameheader.
Header,expected
13. Response Header Should Not Equal
14. PUT- Issues a http PUT Request
url
15. Get Response Body
Get the response body.
Example:
GET /foo.xml
${body}= Get Response Body
Should Start With ${body} <?xml version="1.0" encoding="UTF-8"
16. Response Body Should Contain
Fails if the response body does not contain should_contain
Example:
GET /foo.xml
Response Body Should version="1.0"
Contain
Response Body Should encoding="UTF-8"
Contain
17. Show Response Body In Browser
18. Set Json Value
Set the target node of the JSON document json_string specified by JSON
Pointer json_pointer to json_value.
json_string, *args
Example:
${result}= Set Json Value {"foo": {"bar": [1,2,3]}} / 12
foo
Should Be Equal ${result} {"foo": 12}