Tuesday, 11 February 2020

API Test automation using Http client

Steps for creating a Test automation framework using Http client 
 
1.Create a new maven project in the Eclipse
2.Open the pom.xml file
3.Add ‘httpclient’ library from maven
4.Add ‘httpcore’ library from maven
5.Add‘json’ api from maven repository
6.Add 'testing' library
7.Create a TestBase in the Base package
8.Create a new package Config
9.Create a config.properties file in the and add the base url and serviceURL in the configuration file
12.Create a constructor in base clas
13.Read the properties file in the constructor


public class TestBase {
    public static Properties property;
   
    public TestBase() {
        try
        {
            property = new Properties();
            FileInputStream ip = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/com/qa/Config/Config.properties");
            property.load(ip);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}



14.Create a new package client
15.Create a class in the package RestClient
16.Create a Get method inside the class

public class RestClient {
   
    public void get(String url) throws ClientProtocolException, IOException {
       
        //Get Method
        CloseableHttpClient httpClient=HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(url);
        CloseableHttpResponse httpResponse=httpClient.execute(httpGet);
       
        //Status Code
        int statusCode=httpResponse.getStatusLine().getStatusCode();
        System.out.println("Status code is : "+statusCode);
       
        //JSON Response
        String responseString=EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
        JSONObject responseJson=new JSONObject(responseString);
        System.out.println("Response JSON is "+responseJson);
       
        //Headers
        Header[] headerArray=httpResponse.getAllHeaders();
        HashMap<String, String> allHeaders=new HashMap<String, String>();
       
        for(Header header:headerArray) {
            allHeaders.put(header.getName(), header.getValue());
        }
       
        System.out.println("Headers are :"+allHeaders);
    }

}

16.Create a new package tests under src/test/Java
17.Create a class GetAPITest

    It should exted the base class and create the methods like below:

public class GetAPITest extends TestBase{
    TestBase testBase;
    String serviceURL;
    String apiURL;
    String URI;
   
    @BeforeTest
    public void setUp() throws ClientProtocolException, IOException {
        testBase=new TestBase();
        serviceURL=property.getProperty("url");
        apiURL=property.getProperty("serviceurl");
        URI=serviceURL+apiURL;
    }

    @Test
    public void Test() throws ClientProtocolException, IOException {
        RestClient restClient=new RestClient();
        restClient.get(URI);
    }

}




18.Run the test method in the class GetAPITest

Test should be passed and the output will printed in the console.


No comments:

Post a Comment