Skip to main content

API Automation using Java


Web Services Explained   
  Web Service can be defined as 
1. Method of communication between two devices over network
2. Is a collection of standards or protocols for exchanging information between two devices or application.
3. Web service is a language independent way of communication.
For example, java application can interact with Java, .Net and PHP applications.  
Types of Web Services
There are mainly two types of web services.
1. SOAP web services.
2. RESTful web services. 
SOAP Web Services 
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing web services. SOAP is a W3C recommendation for communication between two applications.It is platform independent and language independent. By using SOAP, you will be able to interact with other programming language applications.
 Advantages of Soap Web Services 
WS Security: SOAP defines its own security known as WS Security.
Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform.
Disadvantages of Soap Web Services
Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resource.
WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to discover the service.
  
RESTful Web Services
 Representational State Transfer (REST) is an architectural style for designing loosely coupled web services. It is mainly used to develop lightweight, fast, scalable, and easy to maintain, web services that often use HTTP as the means of communication.
                                                OR  
A REST API defines a set of operations where developers can perform requests and receive responses via HTTP protocol 
Advantages of RESTful Web Services :
  1. Because REST APIs use HTTP, they can be used by practically any programming language
  2. It acts as medium to propagate communication between the client and server applications on the World Wide Web
  3. Stateless No client data is stored on the server between requests and session state is stored on the client.
  4. http is the transport protocol for REST
Fast: RESTful Web Services are fast because there is no strict specification like SOAP. It consumes less bandwidth and resource.
Language and Platform independent: RESTful web services can be written in any programming language and executed in any platform.
Can use SOAP: RESTful web services can use SOAP web services as the implementation.
Permits different data format: RESTful web service permits different data format such as Plain Text, HTML, XML and JSON.
Rest API Examples :
 The Twitter REST API

Facebook REST API (deprecating)


Google Translate REST APIImage result for rest api diagram example














The key principles of REST are as follows:

• Represent everything with a unique ID; a URI

• Stateless communication

• Make use of standard HTTP methods such as GET, POST, DELETE, and PUT

GET- The GET method is used to extract information from the given server using a given URI. While using GET request, it should only extract data and should have no other effect on the data.

No Payload/Body required

POST- A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

PUT- Replaces all current representations of the target resource with the uploaded content.

DELETE- Removes all current representations of the target resource given by a URI.

• Resources can have multiple representations





REST API Automation Testing using Java



Rest-Assured is a Java based library that is used to test RESTful Web Services. This library behaves like a headless Client to access REST web services. 
Rest-Assured library also provides ability to validate the HTTP Responses received from server. For e.g. we can verify the Status code, Status message, Headers and even the Body of the response.



Authentication, Part 1

There are several techniques APIs use to athenticate a client. These are called authentication schemes.

Basic Authentication

Also referred as Basic Auth. Basic Auth only requires a user name and password. The client takes these two credentials, converts them to a single value and passes that along in the HTTP header called Authentication.
The server compares the Authorization header and compares it to the credential it has stored. If it matches, the server fulfills the request. If there is no match, the server returns status code 401.
API Key Authentication
API Key Authentication is a technique that overcomes the weakness of using shared credentials. by requiring the API to be accessed with a unique key. Unlike Basic Auth, API keys were conceived at multiple companies in the early days of the web. As a result, API Key Authentication has no standard and everybody has its own way of doing it.
The most common approach has been to include it onto the URL(http://example.com?apikey=mysecret_key).

Authentication, Part 2

Open Authorization (OAuth) automates the key exchange. OAuth only requires user credentials, then behind the scenes, the client and server are chatting back and forth to get the client a valid key.
Currently there are two versions of OAuth, OAuth1 and OAuth2.
OAuth2
The players involved are:
The User - A person that wants to connect to the website
 The Client - The website that will be grated the access to the user's data
 The Server - The website that has the user's data
Step 1 — User Tells Client to Connect to Server
Step 2 — Client Directs User to Server
The client sends the user over to the server’s website, along with a URL that the server will send the user back to once the user authenticates, called the callback URL.
Step 3 — User Logs-in to Server and Grants Client Access
With their normal user name and password, the user authenticates with the server.
Step 4 — Server Sends User Back to Client, Along with Code
The server sends the user back to the client using the callback URL. Hidden in the response is a unique authorization code for the client.
Step 5 — Client Exchange Code + Secret Key for Access Token
The client takes the authorization code it receives and makes another request to the server. This request includes the client’s secret key. When the server sees a valid authorization code and a trusted client secret key, it is certain that the client is who it claims. The server responds back with an access token.
Step 6 — Client Fetches Data from Server.
The access token from Step 5 is essentially another password into the user’s account on the server. The client includes the access token with every request so it can authenticate directly with the server.
Client Refresh Token (Optional)
A feature in OAuth 2 is the option to have access tokens expire. The lifespan of a token is set by the server.

Authorization

Authorization is the concept of limiting access. In Step 2, when the user allows the client access, buried in the fine print are the exact permissions the client is asking for. Those permission are called scope.

What makes scope powerful is that is client-based restrictions. OAuth scope allows one client to have permission X and another to have permission X and Y.




Comments

Popular posts from this blog

Handle radio button & Check Boxes in Selenium

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options. Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement class. We can select and deselect the radio buttons using the click() method of the WebElement class and check whether a radio button is selected or deselected using the isSelected() method. Before performing the click on the Radio buttons or check boxes we will have to verify follwing scenarios :- If Radio button or Checkbox is displayed on the webpage If Radio button or Checkbox is enabled on the webpage Check the default selection of the Radio button or Checkbox We use predefined methods present in selenium to handle check Box and Radio  button :- isDisplayed() isEnabled() isSelected() 1.  isDisplayed () this method returns a Boolean value, if it returns t

How To Send Report In Mail Using Java In Selenium Web driver

Today we will share code how to send any type of selenium report in mail using java. For sending the email using JavaMail API, you need to load the two jar files: mail.jar activation.jar package SeleniumNew; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*;  import javax.mail.internet.*;  public class SendEmail {     public static void main(String[] args)     {                 String host="smtp.gmail.com";          final String user="ashishxx@gmail.com";//change accordingly          final String password="XXX";//change accordingly                    /*String[] to={"AshishXX@gmail.com","Ashishxxx@gmail.com"};           String[] cc={};           String[] bcc={};         */           String to="ashishxx@gmail.com";//change accordingly                     //Get the session object            Properties props = new

Appium architecture (How Appium works internally)

Appium Architecture  A ppium is a cross-platform automation tool, API of it supports both OS (Android and iOS) test scripts.It is tested on simulators (iOS,), emulators (Android), and real devices (iOS, Android)   Appium is an HTTP server written in Node.js that creates and handles WebDriver sessions.The Appium web server follows the same approach as the Selenium WebDriver, which receives HTTP requests from client libraries through JSON and then handles those requests in different ways   JSON wire protocol The JSON wire protocol  ( JSONWP ) is a transport mechanism created by WebDriver developers. This wire protocol is a specific set of predefined, standardized endpoints exposed via a RESTful API.   Appium implements the Mobile JSONWP, the extension to the Selenium JSONWP, and it controls the different mobile device behaviors, such as installing/uninstalling apps over the session Let’s have a look at some of the endpoints from the API which are used to interact with