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. Flutter工程目录

    1 目录结构 当使用flutter create myapp 创建纯flutter项目后,会自动生成初始化代码. 需要注意一下几个文件夹 2 资源 像图片.视频.文字等这些资源文件,在 Flutter ...

  2. JUnit@Before失效

    在学习Shiro的过程中需要使用到JUnit@Before注解配合测试,但是无论如何,@Before下面的方法都不按照预期的执行,困扰良久,后来各种百度终于找到根源,今分享于诸公,望能解诸公之急: J ...

  3. msyql 计划任务 备份数据库

    用计划任务备份数据库 把exam库备份到家(home)目录下 [root@izuf66j5nlb2arg99viiuwz /]# mysqldump -u root -p exam > ~/ex ...

  4. 中国MOOC_零基础学Java语言_第4周 循环控制

    4.1 for循环 Tips for loops 如果有固定次数,用for 如果必须执行一次,用do_while 其他情况用while 4.2 循环控制 break和continue 在循环前可以放一 ...

  5. java 发送 http post 和 get 请求(利用unirest)

    调用服务器端的接口在前端调用,但是我们也会经常遇到在服务器后端调用接口的情况,网上的例子大部分都是用 jdk 原生的 URL realUrl = new URL(url); URLConnection ...

  6. laravel 添加后台登陆守护器

    后台不能在一个浏览器登陆,下面简单配置下即可解决这个问题. 设置路由如下: <?php /** * 后台路由,从Illuminate\Routing\Router控制器的auth()方法中复制过 ...

  7. ES6标准入门 第五章:正则的扩展

    1.RegExp 构造函数 ES5的缺陷: ES5中构造函数的参数有两种情况: (1)参数是字符串,这时第二个参数表示正则表达式的的修饰符(flag). var reg = new RegExp( & ...

  8. Go语言入门篇-复合数据类型

    复合数据类型 主要讨论四种类型——数组.slice.map和结构体 数组和结构体聚合类型   --数组和结构体都是有固定内存大小的数据结构 --数组:由同构的元素组成——每个数组元素都是完全相同的类型 ...

  9. Tomcat 一台机器运行多个Tomcat

    转 https://www.cnblogs.com/andy1234/p/8866588.html 在一台Win10 PC 上面同时开启两个Tomcat系统为例. 1. 硬件环境 2. 到Tomcat ...

  10. [BZOJ 3456]城市规划(cdq分治+FFT)

    [BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...