java实现自动化测试接口访问(一)
2017-12-25 14:15
423 查看
一、前置准备:
PostMan
访问的网站:Github
访问的接口:
https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=1&per_page=100
实现访问:查找2017-11-27到 2017-12-01的100条数据
二、代码实现
1. 使用PostMan输入访问的接口,取得需要的字段,items,和items中repository的字段id,和full_name(可以自己获取想要的字段)
java类
(1)、CommitRepo
(2)、CommitItems
(3)、GithubCommitsResult
(4)、GithubTest测试类
(5)、GithubTest测试类代码修改
PostMan
访问的网站:Github
访问的接口:
https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=1&per_page=100
实现访问:查找2017-11-27到 2017-12-01的100条数据
二、代码实现
1. 使用PostMan输入访问的接口,取得需要的字段,items,和items中repository的字段id,和full_name(可以自己获取想要的字段)
java类
(1)、CommitRepo
public class CommitRepo { private String id; private String full_name; @Override public String toString() { return "CommitRepo [id=" + id + ", full_name=" + full_name + "]"; } public String getId() { return id; } public String getFull_name() { return full_name; } }
(2)、CommitItems
public class CommitItems { private CommitRepo repository;//名字要一样(repository) public CommitRepo getRepository() { return repository; } @Override public String toString() { return "CommitItems [repository=" + repository + "]"; } }
(3)、GithubCommitsResult
public class GithubCommitsResult { private CommitItems[] items; public CommitItems[] getItems() { return items; } @Override public String toString() { return "GithubCommitsResult [items=" + Arrays.toString(items) + "]"; } }
(4)、GithubTest测试类
public static void main(String[] args) { try { URL url = new URL("https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=10&per_page=100"); HttpsURLConnection https = (HttpsURLConnection) url.openConnection();// 打开连接 https.addRequestProperty("Accept","application/vnd.github.cloak-preview");// 设置请求属性 https.addRequestProperty("Authorization", "Basic "+up); https.setDoInput(true); InputStream is = https.getInputStream(); Gson gson = new GsonBuilder().create(); GithubCommitsResult gcr = gson.fromJson(new InputStreamReader(is),GithubCommitsResult.class); for(int i=0;i<gcr.getItems().length;i++){ System.out.println(gcr.getItems()[i].getRepository().getId()); } is.close(); https.disconnect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
(5)、GithubTest测试类代码修改
public static void main(String[] args) { String url="https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=1&per_page=100"; String ret=getGithubApiReply(url); Gson gson = new GsonBuilder().create(); GithubCommitsResult gcr = gson.fromJson(ret,GithubCommitsResult.class); System.out.println(gcr.getItems()[99].getRepository().getId()); }
public static String getGithubApiReply(String api_url) { String retstr = null; try { URL url = new URL(api_url); HttpsURLConnection https = (HttpsURLConnection) url .openConnection();// 打开连接 https.addRequestProperty("Accept", "application/vnd.github.cloak-preview");// 设置请求属性 https.setDoInput(true); InputStream is = https.getInputStream(); Reader reader = new InputStreamReader(is, "utf-8"); StringBuffer sb = new StringBuffer(); int c = -1; while ((c = reader.read()) != -1) { sb.append((char) c); } retstr = new String(sb); is.close(); https.disconnect(); } catch (IOException e) { e.printStackTrace(); } return retstr; }
相关文章推荐
- java实现自动化测试接口访问(四)--Postman参数化
- java实现自动化测试接口访问(三)--查询书籍的api接口
- java实现自动化测试接口访问(二)--Http登录授权接口实例
- 接口测试-自动化-Java实现-CommonClass
- 接口测试-自动化-Java实现-InterfaceTest
- 接口测试-自动化-Java实现-TestMain
- Java通过redis实现接口ip访问频率限制
- 接口测试-自动化-Java-总结
- 单元测试、自动化测试、接口测试过程中的Excel数据驱动(java实现)
- 【java编程思想--学习笔记(三)】访问控制-接口实现与类的访问权限
- Java访问https接口实现
- Ant+Jmeter+Jenkins实现接口测试自动化
- java实现之测试第三方接口是否能正确连接
- Java访问https接口实现
- java单元测试之如何实现异步接口的测试案例
- Jmeter实现接口自动化测试
- java中接口的定义与实现 Java中几种访问权限总结
- Java访问https接口实现
- Ant+Jmeter+Jenkins实现接口测试自动化
- 接口测试-自动化-Java-思路整理后