http://seleniummaster.com/sitecontent/index.php/component/banners/click/6

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 "org.jbundle.util.osgi.wrapped.org.apache.http.client-4.1.2.jar" and add the file as External JARS.

Step 2: write the code in WeatherApiTest.java class.

package com.seleniummaster.apitest;

import java.io.IOException;
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; @SuppressWarnings("deprecation")
public class WeatherApiTest {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://openweathermap.org/current";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} @After
public void tearDown() throws Exception {
driver.close();
driver.quit();
} @Test
public void test() throws ClientProtocolException, IOException {
driver.get(baseUrl);
driver.navigate().to("http://api.openweathermap.org/data/2.5/weather?q=London");
WebElement webElement=driver.findElement(By.tagName("pre"));
WeatherApiResponse weatherApiResponse=new WeatherApiResponse();
String ExpectedString=weatherApiResponse.GetResponse();
Assert.assertTrue(webElement.getText().equals(ExpectedString));
} }

Step 3: write the code in the WeatherApiResponse.java class

package com.seleniummaster.apitest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 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="http://api.openweathermap.org/data/2.5/weather?q=London";
HttpGet request=new HttpGet(url);
request.addHeader("User-Agent",USER_AGENT);
HttpResponse response=client.execute(request);
int responseCode=response.getStatusLine().getStatusCode();
System.out.println("Response Code: " + responseCode);
try{
if(responseCode==200) {
System.out.println("Get Response is Successfull");
BufferedReader reader=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line="";
while((line=reader.readLine())!=null)
{
result.append(line);
System.out.println(result.toString());
}
}
return result.toString(); }
catch(Exception ex)
{
result.append("Get Response Failed");
return result.toString();
} }
}

Step 4: run the file WeatherApiTest.java 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}

seleniummaster的更多相关文章

  1. Send Email in Robot Framework Python Using Gmail

    转载自:http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-fra ...

随机推荐

  1. Windows2008 r2“Web服务器HTTP头信息泄露”漏洞修复

    一.漏洞名称 漏洞名称 漏洞摘要 修复建议 Web服务器HTTP头信息泄露 远程Web服务器通过HTTP头公开信息. 修改Web服务器的HTTP头以不公开有关底层Web服务器的详细信息. 说明:在ii ...

  2. 1450:【例 3】Knight Moves

    1450:[例 3]Knight Moves  题解 这道题可以用双向宽度搜索优化(总介绍在  BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...

  3. 网站模板-Layui:Layui

    ylbtech-网站模板-Layui:百科 layui,是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的 ...

  4. Selenium学习之==>常见面试题

    转自:http://www.imdsx.cn/ 一.selenium中如何判断元素是否存在? expected_conditions模块提供了多种校验方式,我常用的一种是presence_of_ele ...

  5. Linux iptables 防火墙常用规则

    iptables 安装 yum install iptables iptables 规则清除 iptables -F iptables -X iptables -Z 开放指定的端口允许本地回环接口(即 ...

  6. 【ABAP系列】SAP ABAP MRKO增强

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP MRKO增强 ...

  7. 【MM系列】SAP 关于更改物料的价格控制类型

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 关于更改物料的价格控制类型 ...

  8. cocos2dx基础篇(21) 进度条CCProgressTimer

    [3.x] (1)去掉 "CC" (2)CCProgressTimerType 改为强枚举 ProgressTimer::Type:: // RADIAL //扇形进度计时器 BA ...

  9. python每日一练:0005题

    第 0005 题: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. import cv2 import os def resize(path,sizeX,size ...

  10. 区块链开源实现hyperledger fabric架构详解

    hyperledger fabric是区块链中联盟链的优秀实现,主要代码由IBM.Intel.各大银行等贡献,目前v1.1版的kafka共识方式可达到1000/s次的吞吐量.本文中我们依次讨论:区块链 ...