Sunday 6 August 2017

WebService Automation Using Java



WebService Automation For REST


Webservice automation with java is simple, You have to convert json response in pojo and verify it using java, below are step for 1 of login request for REST
 


1. create Eclipse MVN Prject
2. Add rest assured mvn dependancies (restassured)
3. Add json dependancies in pom
3. Create folder structure like below



4. service package- create service login.java

Copy  below code for loginREquest

public class Service {
      
       /**
        * This API will perform login operation
        * @param loginId
        * @param loginSource
        * @param password
        * @return
        */
       public Response login(String loginId, String loginSource,String password){
              try {
                     LoginPojo loginpojo = new LoginPojo();
                     Login login = new Login();
                     login.setLoginId(loginId);
                     login.setLoginSource(loginSource);
                     login.setPassword(password);
                     loginpojo.setLogin(login);
                     JSONObject jsonObj = new JSONObject(loginpojo);
                     RequestSpecification requestSpecification = RestAssured.given();
                     requestSpecification.headers("content-type","application/json");
                     requestSpecification.body(jsonObj.toString());
                     Response reponse = requestSpecification.post(URLBuilder.loginurl);
                    
                     return reponse;
              } catch (Exception e) {
                     e.printStackTrace();
              }
              return null;
             
       }

5. create(register) user on below URL
6. Add same url in URLBuilder.java
public class URLBuilder {
       public final static  String loginurls = "http://localhost:8080/Retail_App_Rest/retail/admin/login";
      
}

7. Take below code in loginApi.java

public class LoginApiTest {
       Service service;
       Response responseData;
       @Test
       public void loginApiTest(){
               service = new Service();
       responseData= service.login("new user created in step 5", "APP", "step 5 Password");
       System.out.println(responseData.asString());
      
             
       }

8. Run Login API, and take response from console

9. convert json to pojo from below site, use below setting which mentioned in right




10. Add response pojo code in ResponsePojoLogin.java
- You ll get 3 classes, You need to create 3 .java in  same package, only change Example.java as LoginPojoResponse

8. change loginapi.java with below code and verify response

public class LoginApiTest {
       Service service;
       Response responseData;
       @Test
       public void loginApiTest(){
               service = new Service();
       responseData= service.login("new user created in step 5", "APP", "step 5 Password");
       System.out.println(responseData.asString());
       Gson gson = new Gson();
       LoginResponsePojo data = gson.fromJson(responseData.asString(), LoginResponsePojo.class);
      
       CustomerWrapper getCustomerWrapper = data.getCustomerWrapper();
       System.out.println(getCustomerWrapper.getResponseCode());
       Customers getCustomers=getCustomerWrapper.getCustomers();
       System.out.println(getCustomers.getFirstName());
       Assert.assertEquals(getCustomers.getLoginId(),"new user created in step 5");
             


No comments:

Post a Comment