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. 前端Node项目发布流程

    最近在做前端的发布流程,发布流程的主要实现以下几个方面: 构建:包括JavaScript.css.html等的压缩,以及版本控制,利用md5生成版本号替换文件引用,实现长缓存策略. 发布:输出新版本的 ...

  2. Linux内核中的cmpxchg函数

    http://www.longene.org/forum/viewtopic.php?t=2216 前几天,为了这个函数花了好多时间,由于参考的资料有误,一直都没有看明白,直到google之后,总算搞 ...

  3. python学习之文件读写操作

    open函数 在使用文件之前,需要先打开,即使用open函数 如: files=open("文件路径","操作方式") 解释如下: (1.files:为文件对象 ...

  4. 【MM系列】SAP MM模块-收货自动创建采购订单

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-收货自动创建采购订 ...

  5. 添加linux中svn的用户和密码

    1:首先找到svn路径 find / -iname "svn" 一般找到svn路径之后就可以找到配置文件位置啦 svn/svnrepos/jgcp/conf 2:进入目录之后修改a ...

  6. 指定pom文件jdk版本

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  7. [转帖]CBO和RBO

    http://www.itpub.net/thread-263395-1-1.html 参数不能随便改啊.. optimizer_features_enable('8.1.7') ORACLE 提供了 ...

  8. C++ 线性表实现

    List.h #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h&g ...

  9. 通过编写串口助手工具学习MFC过程——(五)添加CheckBox复选框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  10. FTP连接不上的解决方法

    1.注意内网IP和外网IP 2.检查ftp服务是否启动 (面板首页即可看到) 3.检查防火墙20端口 ftp 21端口及被动端口39000 - 40000是否放行 (如是腾讯云/阿里云等还需检查安全组 ...