Selenium WebDriver Java Framework Course Limited Time Offer for $50

Selenium WebDriver Java Framework Course Limited Time Offer for $50


Test API With Selenium Java

Many web applications provide API for external clients to use in their internal applications. When testing APIs, QA engineers need to verify that what UI displayed should be equal to the API response for the same request input.  In this article, we use the Open Weather API as an example to test the web response for this url ",uk"   should be the same as the API response. The code is written in Java. 

Step 1: create a Java project as shown below.  In the Build Path, add Selenium and JUnit libraries; download apache jar file from the link "" and add the file as External JARS. 

Step 2: write the code in class. 

package com.seleniummaster.apitest;

import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WeatherApiTest {
	private WebDriver driver;
	private String baseUrl;
	public void setUp() throws Exception {
		  driver = new FirefoxDriver();
		    baseUrl = "";
		    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

	public void tearDown() throws Exception {

	public void test() throws ClientProtocolException, IOException {
	 WebElement webElement=driver.findElement(By.tagName("pre"));
	 WeatherApiResponse weatherApiResponse=new WeatherApiResponse();
	 String ExpectedString=weatherApiResponse.GetResponse();


Step 3: write the code in the class

package com.seleniummaster.apitest;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class WeatherApiResponse {
	private final String USER_AGENT="Mozilla/5.0";
	public String GetResponse() throws ClientProtocolException, IOException
		StringBuffer result=new StringBuffer();
		HttpClient client=new DefaultHttpClient();
		String url="";
		HttpGet request=new HttpGet(url);
		HttpResponse response=client.execute(request);
		int responseCode=response.getStatusLine().getStatusCode();
		System.out.println("Response Code: " + responseCode);
			System.out.println("Get Response is Successfull");
			BufferedReader reader=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line="";
		return result.toString();
		catch(Exception ex)
		result.append("Get Response Failed");
		return result.toString();


Step 4: run the file as JUnit Test. The test passed. Below is the console output

Response Code: 200
Get Response is Successfull
{"coord":{"lon":-0.13,"lat":51.51},"sys":{"type":1,"id":5093,"message":0.0202,"country":"GB","sunrise":1411451341,"sunset":1411494984},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"}],"base":"cmc stations","main":{"temp":282.35,"pressure":1024,"humidity":87,"temp_min":280.93,"temp_max":284.15},"wind":{"speed":1.33,"deg":208.502},"clouds":{"all":8},"dt":1411431497,"id":2643743,"name":"London","cod":200}