Interface test-Parse har file

Source: Internet
Author: User

Before I had already researched to let the business test through different ways to obtain the Har file which our tool needs, now we have obtained the business test to provide the Har file, we first have to parse these files the information, especially in entries the field the information, in the omnipotent really did github search out a tool pack

Address

Har

Because there is no download for this jar in the MAVEN library, you need to download the source code locally and then upload it to your company's private library for download by other developers.

Source

The main class is HarUtils.java , there are 2 classes required by the command line ( HarCli.java , ViewHar.java ), the main role of these two classes please see my previous article Java command-line program build tool-airline, the last thing left is the model package in the entity class, one by one corresponding to the har The information entity in the file.

Harutils.java
/** * * Har-har file Reader, writer and viewer * Copyright (c), Sandeep Gupta * * http://sangupta.com/projects /har * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by app Licable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * Withou T warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *  */ PackageCom.sangupta.har;ImportJava.io.File;ImportJava.io.IOException;ImportJava.io.Reader;ImportJava.util.ArrayList;ImportJava.util.Collections;ImportJava.util.List;ImportOrg.apache.commons.io.FileUtils;ImportCom.google.gson.JsonElement;ImportCom.google.gson.JsonSyntaxException;ImportCom.sangupta.har.model.Har;ImportCom.sangupta.har.model.HarEntry;ImportCom.sangupta.har.model.HarPage;ImportCom.sangupta.jerry.util.AssertUtils;ImportCom.sangupta.jerry.util.CheckUtils;ImportCom.sangupta.jerry.util.GsonUtils;/** * Utility class for working with HAR files. * * @author sangupta * */ Public  class harutils {    /** * Read The har file and create a {@link HAR} model instance from the same.     * * @param file * The file to is read * * @return the {@link Har} instance * * @throws jsonsyntaxexception * If the JSON is not a well formed * * @throw     S IOException * If reading the file fails * * @throws illegalargumentexception * If the file does not exist, was a directory or is not a valid * file * *     Public StaticHarRead(File file)throwsJsonsyntaxexception, IOException {checkutils.checkfileexists (file);returnGsonutils.getgson (). Fromjson (fileutils.readfiletostring (file), har.class); } Public StaticHarRead(String Harjson)throwsJsonsyntaxexception, IOException {if(Assertutils.isempty (Harjson)) {Throw NewIllegalArgumentException ("HAR Json cannot be Null/empty"); }returnGsonutils.getgson (). Fromjson (Harjson, Har.class); } Public StaticHarRead(Reader Harreader)throwsJsonsyntaxexception, IOException {if(Harreader = =NULL) {Throw NewIllegalArgumentException ("HAR reader cannot be null"); }returnGsonutils.getgson (). Fromjson (Harreader, Har.class); } Public StaticHarRead(Jsonelement jsonelement)throwsJsonsyntaxexception, IOException {if(Jsonelement = =NULL) {Throw NewIllegalArgumentException ("HAR jsonelement cannot be null"); }returnGsonutils.getgson (). Fromjson (Jsonelement, Har.class); }/** * Connect references between page and entries so, they can be obtained as needed. * * @param har * *     Public Static void connectreferences(Har har) {if(Har = =NULL) {Throw NewIllegalArgumentException ("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 =NewArraylistreturn; } for(Harpage page:har.log.pages)            {String PageID = page.id; listNewArraylist 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; }    }}

The following methods are provided to obtain the Har object:

Method explain
Read (File) Create Har object with har file as parameter
Read (jsonelement) Take the Jsonelement object as a parameter
Read (Reader) Get the Har object from the byte stream
Read (String) Get the Har object from a string
Connectreferences (har har) Associating page information in the pages and Enties fields in the Har file
Instance
publicclass HarParserTest {    @Test    publicvoidparserFile() throws Exception {        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= .- on- -T08: -: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= .- on- -T08: -:23.913Z, time=10.068999999930384, Request=get http://www. Baidu. com/baidu. html? from=noscript http/1.1, Response=http $(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= .- on- -T08: -:23.971Z, time=9.560000000419677, Request=get http://www. Baidu. com/img/bd_logo1. PNGhttp/1.1, Response=http $(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= .- on- -T08: -:23.971Z, time=12.46499999979278, Request=get http://www. Baidu. com/img/baidu_jgylogo3. gifhttp/1.1, Response=http $(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= .- on- -T08: -:23.972Z, time=15.82599999892409, Request=get http://www. Baidu. com/img/gs. gifhttp/1.1, Response=http $(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]]
Summarize

We get the Har information, can do a lot of, we through the Har file, parse out all the URLs and parameters, so you can get a batch of interface information. So what can actually do, wait for the follow-up.

Interface test-Parse har file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.