seleniummaster
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的更多相关文章
- Send Email in Robot Framework Python Using Gmail
转载自:http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-fra ...
随机推荐
- python语言优势
与Java等语言比较起来,最大优点是语法很简洁,很多功能像octave和matlab,能够对数组或矩阵进行高效处理. 比如一个数组求和,这里只要一句话sum(a),Java等语言就需要循环.还有矩阵的 ...
- nslookup、dig、host
1.作用:查询域名对应的地址或者地址对应的域名 2.nslookup已经不建议使用,慢慢就会废弃,建议使用dig和host代替 3.格式 dig: dig @NameServer 域名 Type ho ...
- 为终端配置proxy
转自:https://my.oschina.net/u/818848/blog/677225?p=1 做开发的同学,应该都会经常接触终端,有些时候我们在终端会做一些网络操作,比如下载gradle包等, ...
- Eclipse 添加 UML Model插件
1.下载安装 ModelGson 下载链接:https://pan.baidu.com/s/1smIZApv 密码:mu5l eclipse安装ModelGson(注意不用解压ModelGson, ...
- Android:adb命令详解
什么是adb adb工具即Android Debug Bridge(安卓调试桥) tools.它就是一个命令行窗口,用于通过电脑端与模拟器或者真实设备交互
- unittest单元测试1
一个简单的单元测试例子#coding:utf-8from selenium import webdriverimport unittestclass Baidu(unittest.TestCase): ...
- Go语言入门篇-基本数据类型
一.程序实体与关键字 任何Go语言源码文件都由若干个程序实体组成的.在Go语言中,变量.常量.函数.结构体和接口被统称为“程序实体”,而它们的名字被统称为“标识符”. 标识符可以是任何Unicode编 ...
- mysql 恢复数据时中文乱码
mysql恢复数据时中文乱码,解决办法. 用source命令导入mysql数据库怎么设置中文编码 1.导出数据时指定编码在导出mysql sql执行文件的时候,指定一下编码格式: mysqldump ...
- [转帖]mysql数据库主从配置
mysql数据库主从配置 https://www.toutiao.com/i6680489302947791371/ 多做实验 其实挺简单的 很多东西 要提高自信 去折腾. 架构与我 2019-04- ...
- kubernetes(k8s)容器编排工具基础概念
Kubernetes (K8s): 中文社区:https://www.kubernetes.org.cn/replication-controller-kubernetes 官网:https://ku ...