Interface Test-parse the har File
Previously, I have studied how to obtain the har file required by our tool in different ways for business testing. Now we have obtained the har file provided by business testing, first, we need to parse the information stored in these files, especiallyentries
Field Information, In the omnipotentgithub
I found a toolkit.
Address
Har
Because this jar package is not available for download in the maven repository, You need to download the source code to your local computer, package it, and upload it to your own private repository for other developers to download.
Source code
The main class isHarUtils.java
, There are two classes required for execution under the command line (HarCli.java
,ViewHar.java
). For the main functions of these two classes, please refer to my previous article Java command line program build tool-airline. The last thing left is the entity classes in the model package, which correspond one by one.har
Object.
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
(); } return; } for(HarPage page : har.log.pages) { String pageID = page.id; List
entries = new ArrayList
(); 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; } }}
Provides the following methods to obtainHar
Object:
Method |
Explanation |
Read (File) |
Generate the har object with the har file as the parameter |
Read (JsonElement) |
Take the JsonElement object as the parameter |
Read (Reader) |
Get har object from byte stream |
Read (String) |
Get har object from string |
ConnectReferences (Har har) |
Associate pages in the pages and enties fields in the har File |
Instance
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()); }}
Output Information:
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]]
Summary
We get the Har information, and we can do a lot of work. We use the har file to parse all URLs and parameters so that we can get a batch of interface information. So what can we do? Wait for the future.