之前我已经研究到让业务测试通过不同方式来获取我们工具需要的har文件,现在我们拿到了业务测试提供的har文件,我们首先要解析这些文件里存放的信息,特别是entries字段里的信息,在万能的github上果然搜出来一个工具包

地址

har

因为maven库里还没有这个jar包提供下载,你需要将源码下载到本地,打包后上传到自己公司的私有库里,供其他开发者下载

源码

主要的类为HarUtils.java,还有命令行下执行需要的2个类(HarCli.java,ViewHar.java),这两个类的主要作用请看我之前的文章Java命令行程序构建工具-airline ,最后剩下来的就是model包中的实体类了,一一对应了har文件中的信息实体。

HarUtils.java

/**** har - HAR file reader, writer and viewer* Copyright (c) 2014, Sandeep Gupta* * http://sangupta.com/projects/har* * Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* *      http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* */package com.sangupta.har;import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;import org.apache.commons.io.FileUtils;import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.sangupta.har.model.Har;
import com.sangupta.har.model.HarEntry;
import com.sangupta.har.model.HarPage;
import com.sangupta.jerry.util.AssertUtils;
import com.sangupta.jerry.util.CheckUtils;
import com.sangupta.jerry.util.GsonUtils;/*** Utility class for working with HAR files.* * @author sangupta**/
public class HarUtils {/*** Read the HAR file and create an {@link Har} model instance from the same.* * @param file*            the file to be read* * @return the {@link Har} instance* * @throws JsonSyntaxException*             if the JSON is not well formed* * @throws IOException*             if reading the file fails* * @throws IllegalArgumentException*             if the file does not exist, is a directory or is not a valid*             file*/public static Har read(File file) throws JsonSyntaxException, IOException {CheckUtils.checkFileExists(file);return GsonUtils.getGson().fromJson(FileUtils.readFileToString(file), Har.class);}public static Har read(String harJson) throws JsonSyntaxException, IOException {if(AssertUtils.isEmpty(harJson)) {throw new IllegalArgumentException("HAR Json cannot be null/empty");}return GsonUtils.getGson().fromJson(harJson, Har.class);}public static Har read(Reader harReader) throws JsonSyntaxException, IOException {if(harReader == null) {throw new IllegalArgumentException("HAR reader cannot be null");}return GsonUtils.getGson().fromJson(harReader, Har.class);}public static Har read(JsonElement jsonElement) throws JsonSyntaxException, IOException {if(jsonElement == null) {throw new IllegalArgumentException("HAR JsonElement cannot be null");}return GsonUtils.getGson().fromJson(jsonElement, Har.class);}/*** Connect references between page and entries so that they can be obtained as needed.* * @param har*/public static void connectReferences(Har har) {if(har == null) {throw new IllegalArgumentException("HAR object cannot be null");}if(har.log == null || AssertUtils.isEmpty(har.log.pages)) {// nothing to doreturn;}if(AssertUtils.isEmpty(har.log.entries)) {// no har entry - initialize empty listfor(HarPage page : har.log.pages) {page.entries = new ArrayList<HarEntry>();}return;}for(HarPage page : har.log.pages) {String pageID = page.id;List<HarEntry> entries = new ArrayList<HarEntry>();for(HarEntry entry : har.log.entries) {if(pageID.equals(entry.pageref)) {entries.add(entry);}}// sort these based on start dateCollections.sort(entries);// set the parent referencepage.entries = entries;}}
}

提供了如下几个方法获得Har对象:

Method 解释
read(File) 以har文件为参数,生成har对象
read(JsonElement) 以JsonElement对象为参数
read(Reader) 从字节流中获得har对象
read(String) 从字符串中获取har对象
connectReferences(Har har) 将har文件中pages和enties字段中的page信息关联起来

实例

public class HarParserTest {@Testpublic void parserFile() throws Exception {File file = new File("/Users/wuxian/Desktop/interface/www.baidu.com.har");Har har = HarUtils.read(file);System.out.println("HarParserTest.parserFile() : " + har.toString());}}

输出信息:

HarParserTest.parserFile() : Har [log=HarLog [version=1.2, creator=HarCreator [name=WebInspector, version=537.36, comment=null], browser=null, pages=[HarPage [startedDateTime=2016-01-18T08:50:23.913Z, id=page_2, title=http://www.baidu.com/baidu.html?from=noscript, pageTimings=HarPageTiming [onContentLoad=82.36399999987043, onLoad=97.73299999869778, comment=null], comment=null]], entries=[HarEntry [pageref=page_2, startedDateTime=2016-01-18T08:50:23.913Z, time=10.068999999930384, request=GET http://www.baidu.com/baidu.html?from=noscript HTTP/1.1, response=HTTP 200 (OK), cache=com.sangupta.har.model.HarCache@46b44bc2, timings=HarTiming [blocked=0.663999999233056, dns=-1.0, connect=-1.0, send=0.10800000018207301, wait=7.102999999915481, receive=2.1940000005997735, ssl=-1.0, comment=null], serverIPAddress=null, connection=31193, comment=null], HarEntry [pageref=page_2, startedDateTime=2016-01-18T08:50:23.971Z, time=9.560000000419677, request=GET http://www.baidu.com/img/bd_logo1.png HTTP/1.1, response=HTTP 200 (OK), cache=com.sangupta.har.model.HarCache@66d9d1d1, timings=HarTiming [blocked=1.37800000084098, dns=-1.0, connect=-1.0, send=0.09800000043469992, wait=6.0219999995752, receive=2.0619999995687976, ssl=-1.0, comment=null], serverIPAddress=null, connection=31193, comment=null], HarEntry [pageref=page_2, startedDateTime=2016-01-18T08:50:23.971Z, time=12.46499999979278, request=GET http://www.baidu.com/img/baidu_jgylogo3.gif HTTP/1.1, response=HTTP 200 (OK), cache=com.sangupta.har.model.HarCache@665e2517, timings=HarTiming [blocked=0.743999999031075, dns=2.062999999907335, connect=3.28100000115228, send=0.11999999878752998, wait=5.31500000033698, receive=0.9420000005775808, ssl=-1.0, comment=null], serverIPAddress=null, connection=32311, comment=null], HarEntry [pageref=page_2, startedDateTime=2016-01-18T08:50:23.972Z, time=15.82599999892409, request=GET http://www.baidu.com/img/gs.gif HTTP/1.1, response=HTTP 200 (OK), cache=com.sangupta.har.model.HarCache@2ed53d82, timings=HarTiming [blocked=0.84199999946577, dns=1.9300000003568099, connect=3.2869999995455204, send=0.08600000001024011, wait=7.40299999961276, receive=2.277999999932989, ssl=-1.0, comment=null], serverIPAddress=null, connection=32312, comment=null]], comment=null]]

总结

我们得到Har信息,可以做的就很多了,我们通过har文件,解析出来所有的url和参数,这样就可以得到一批接口信息。那么实际能做什么呢,静待后续。

接口测试-解析har文件相关推荐

  1. Python 解析har 文件将域名分类导出

    前言 作为程序员平时主要是使用 shadowsocks 作为代理工具的.shadowsocks 有个很明显的优点儿就是可以设置白名单和黑名单.白名单是会走shadowsocks的自动代理模式. 遇到的 ...

  2. python中利用lxml模块解析xml文件报错XMLSyntaxError: Opening and ending tag mismatch

    今天在代码中第一次使用lxml解析xml文件时出错了, XMLSyntaxError: Opening and ending tag mismatch: keyEffectiveDate line 2 ...

  3. java代码使用http请求解压zip包并解析xml_Javascript 是如何解析 Excel 文件的?

    最近要做一个导入导出 Excel 的功能,上一次做这个功能的时候,还是用的 Java Apache POI,这是一个用 Java 编写的免费开源的跨平台的 Java API,能够对 Microsoft ...

  4. java xml中的冒号_Java jdom解析xml文件带冒号的属性

    Java jdom解析xml文件带冒号的属性 如果xml文件解析带了冒号的属性,一般都是要特别处理,这里是命名空间,N年前遇到过一次忘记记录,后来也忘了,这次再记录下. 解决了,记录下,分享给大家,百 ...

  5. 编程模板-R语言脚本写作:最简单的统计与绘图,包安装、命令行参数解析、文件读取、表格和矢量图输出

    写在前面 个人认为:是否能熟悉使用Shell(项目流程搭建)+R(数据统计与可视化)+Perl/Python等(胶水语言,数据格式转换,软件间衔接)三门语言是一位合格生物信息工程师的标准. 之前分享过 ...

  6. java 解析 csv 文件

    文章分类:JavaEye 一.貌似有bug,不行用 二.或 三. 的方法 Java代码   import java.io.BufferedReader; import java.io.FileInpu ...

  7. 用正则表达式和java解析csv文件

    用正则表达式和java解析csv文件 作者:弹着钢琴设计  来源:博客园  发布时间:2009-06-15 18:31  阅读:337 次  原文链接   [收藏]   在解析csv文件之前,先来看看 ...

  8. Java性能优化:正确的解析JSON文件

    为什么80%的码农都做不了架构师?>>>    数据收集服务平均1小时OOM(java.lang.OutOfMemoryError: GC overhead limit exceed ...

  9. XML专题:使用NSXMLParser解析xml文件

    使用NSXMLParser解析xml文件 1. 设置委托对象,开始解析      NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dat ...

  10. python解析json_python解析json文件

    概念 序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON.XML等.反序列化就是从存储区域(JSON,XML)读取反序列化对象的状态 ...

最新文章

  1. 盒子端 CSS 动画性能提升研究
  2. 树莓派:交叉编译,以及带wiringPi库的如何交叉编译
  3. php 获取返回值,求助 如何获取php socket 返回值
  4. OJ1062: 最大公约数(C语言)
  5. UI设计实用素材|功能性图标ICON的作用
  6. Git push大文件失败的处理
  7. 无线网卡802.11n、 Intel 5100 AGN
  8. 面向对象程序设计之封装性、继承性、多态性
  9. Oliver运维管理系统之一庐山真面目
  10. 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置***帧以及动画创建!【二】...
  11. Mac OS 下创建txt文本文件
  12. 腾讯翻译君在线翻译怎么翻译整个文件_希腊语怎么翻译?教你两个超实用的翻译方法...
  13. 批量tracert脚本
  14. 跨域及处理跨域的三种方式
  15. 海康威视监控插件使用步骤
  16. 如何在微信添加定位服务器,如何在微信添加店铺名称地址定位位置
  17. 学习爬虫的感想和心得
  18. python 矩阵求转置、行列式、迹、求逆
  19. 算法基础:k最近邻算法
  20. 关于戴尔电脑物理内存(灵越7590)

热门文章

  1. 局域网文件快传 暂记
  2. Centos7完全卸载MySQL 安装 启动
  3. 软件详细设计文档模板
  4. (软考中级--信息安全工程师)三、密码学基本理论
  5. 转速测试软件app,实时显示行车速度app手机版-手机实时行车速度显示app(gps车速表)v1.6最新版_新绿资源网...
  6. Webpack常用插件总结
  7. c语言冒险游戏代码大全,C语言简易文字冒险游戏源代码.doc
  8. jdk api 1.8中文手册
  9. 基于Spring Boot的简易版新闻管理系统
  10. avast高级版许可文件_明道云私有部署版已上架腾讯云镜像市场