解析har文件

最后更新于:2022-04-01 09:43:35

> 之前我已经研究到让业务测试通过不同方式来获取我们工具需要的har文件,现在我们拿到了业务测试提供的har文件,我们首先要解析这些文件里存放的信息,特别是`entries`字段里的信息,在万能的`github`上果然搜出来一个工具包 ## 地址 [har](https://github.com/DoctorQ/har) 因为maven库里还没有这个jar包提供下载,你需要将源码下载到本地,打包后上传到自己公司的私有库里,供其他开发者下载 ## 源码 ![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-23_56cbdb1a11a04.jpg "") 主要的类为`HarUtils.java`,还有命令行下执行需要的2个类(`HarCli.java`,`ViewHar.java`),这两个类的主要作用请看我之前的文章[Java命令行程序构建工具-airline ](http://blog.csdn.net/itfootball/article/details/50541960),最后剩下来的就是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 do return; } if(AssertUtils.isEmpty(har.log.entries)) { // no har entry - initialize empty list for(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 date Collections.sort(entries); // set the parent reference page.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 { @Test public 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和参数,这样就可以得到一批接口信息。那么实际能做什么呢,静待后续。
';