Friday 17 April 2020

Rest web service automation

Web service automation for REST api with java.
There are 4 types of methods.
Get- this dont need body
Post,Put,Delete- required body
For this you need to create java project and then add below mentioned jars in project.
Create basic java project, below is automation code syntax explanation
Given - Input(parameter, header, body)
When- Resource and HTTP method
Then -verification
All Input,resource, method has to come from developer before web service automation, its called contract.
suppose url  is http://dummy.restapiexample.com/api/v1/employees, as per contract we can distribute as ,
 base url-http://dummy.restapiexample.com/api/v1
 resource-/employees
Below is code you need to use as request in eclipse, 
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

import files.Payload;
import io.restassured.RestAssured;
import io.restassured.filter.session.SessionFilter;
import io.restassured.path.json.JsonPath;

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
RestAssured.baseURI="http://dummy.restapiexample.com/api/v1";

//given-input ,when-submit api-Resource and HTTP Method, then -validate response 
//log()- for use to see details
System.out.println("----------GetDetails-----------------");
//in given method we use queryParam too , it include with "?" in url
//header is json most of times, for some time we have session ids in header, for that SessionFilter class is also avilable
//for post,put delete we need to add "body", for get body is not present
String response=given().header("Content-Type", "application/json")
.when().get("/employees")
.then().log().all().assertThat().statusCode(200)
.extract().response().asString();
System.out.println("response is :" + response);
JsonPath js =new JsonPath(response);
String emp= js.get("data[0].employee_name");
//in response data field is come up with array, so used array concept for 1st emp
System.out.println("1st emp is :"+ emp);
}
}

 Below is response,

So above is GET method, so its without Body. In Given , we use queryParam too , it include with "?" in url which is not present in above example.
Path parameter- use to give specific data from response,Query Parameter- after path parameter, it drill down and give as per your required data, both present in given section
For attachment , we use multipart method in given section, which not used in above example.
relaxHTTPSValidation in given code is important for real world example.



header is json most of times, for some time we have session ids in header, for that SessionFilter class is also available, which is not needed in above example


Sunday 12 April 2020

TestNg installation

Open Eclipse
Click on help
Install new Software
Enter below url,
http://dl.bintray.com/testng-team/testng-eclipse-release/
Click on manage
Click on continue,

If you get any error click on continue