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 ...
随机推荐
- 杂项-PIN:百科
ylbtech-杂项-PIN:百科 个人身份识别码(英语:Personal identification number,缩写为 PIN),又译为用户个人识别号码,常被称为PIN码(PIN number ...
- CircleCI 前端自动部署
概述 现在很多前端库都用到了 CircleCI 进行自动部署,比如Vue,React,作为一个前端我觉得还是有必要实操一下 CircleCI 的,总体来说还是挺简单的,我把过程和体会记录下来,供以后开 ...
- 【MM系列】SAP MM模块-MIGO收货后自动打印收货单
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-MIGO收货后自动 ...
- LeetCode.867-转置矩阵(Transpose Matrix)
这是悦乐书的第332次更新,第356篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第202题(顺位题号是867).给定矩阵A,返回A的转置. 矩阵的转置是在其主对角线上翻转 ...
- 25. Reverse Nodes in k-Group[H]k个一组翻转链表
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- finereport 带多参数查询
1.sql语句 ${,""," and dt.货主地区='"+comboBox0+"'")} ${,""," ...
- Canvas入门01-基础知识
定义一个canvas,直接在Html中使用canvas便签即可. <!DOCTYPE html> <html lang="en"> <head> ...
- Linux-Maven部署
一.Maven是什么 二.Maven部署 1.环境信息: (1)centos7.3 (2)jdk1.8 (3)maven3.5.3 2.安装jdk (1)下载地址[http://www.oracle. ...
- Balanced Binary Tree(平衡二叉树)
来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...
- 【7.24校内交流赛】T1&T2
T1: 一个脑洞很大的题,将输入的所有数异或起来输出就好了: (话说我为什么这么喜欢用异或啊) #include<bits/stdc++.h> using namespace std; i ...