How to automate web-services/API’s using Selenium Webdriver

I want to automate web services using Selenium Webdriver for one of my application. I have to pass parameters in json/xml formats and read the response in json/xml format only. For now Developer has provided me UI for web services but how can I achieve this without UI ?

shrutig Default Asked on March 30, 2015 in Selenium WebDriver.
Add Comment
3 Answer(s)

As Virender sir already said no support from webdriver to test webservice as it is functional web tool

To start up with you can see below example, currently json parser in this example is not working, but this way u can start up

just remember you should go through java.net.* library which helps in this. This program helps in sending HTTP get request to server and get json response and we can parse it using json parser, currently this prg throws exception for parsing ,but this prg can send the request to server with i/p in query string and return json in java string,but I m sure you can do R&D over it to extend this prg.

As mentioned u r i/o to service is json, so as per my understanding it is REST service

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.*;

public class NetClientGet {

public static void main(String[] args) {

try {

URL url = new URL(“http://api.openweathermap.org/data/2.5/weather?id=2172797″);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“Accept”, “application/json”);

if (conn.getResponseCode() != 200) {
throw new RuntimeException(“Failed : HTTP error code : ”
+ conn.getResponseCode());
}

Scanner scan = new Scanner(url.openStream());
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();

System.out.println(“str : ” + str);

JSONObject obj = new JSONObject(str);
String coord_lon = obj.getJSONObject(“coord”).getString(“lon”);
String coord_lat = obj.getJSONObject(“coord”).getString(“lat”);
System.out.println(“coord.lon” + coord_lon);
System.out.println(“coord.lat” + coord_lat);

JSONArray arr = obj.getJSONArray(“weather”);
for (int i = 0; i < arr.length(); i++) {
//System.out.println(“coord.lat” + coord_lat);
String post_id = arr.getJSONObject(i).getString(“description”);

}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

amitjain Supporter Answered on March 31, 2015.

Amit thanks for the wonderful reply.

Keep up the good work!!!

on March 31, 2015.

Thanks Amit for your inputs, I am also using Json Object in my code. I thought there could be some functions available in selenium like SOAPUI Tools for automation but SOAPUI uses groovy scripting and java .
I was trying to integrate functional + web services automation testing in a single Automation Framework.

on March 31, 2015.
Add Comment

Shruti,

WebDrivers have nothing to do with external WebServices. You have to use core java to handle your API requests, probably you are looking HTTPRequest related classes in Java. It depends on what type of API are you trying to Test.

Virender

Virender Singh Professor Answered on March 31, 2015.
Add Comment
DebbyStone Train Answered on September 27, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.