頻道欄目
首頁 > 資訊 > Java > 正文

接口測試-解析har文件

16-01-20        來源:[db:作者]  
收藏   我要投稿

之前我已經研究到讓業務測試通過不同方式來獲取我們工具需要的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 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;
        }
    }
}

提供了如下幾個方法獲得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和參數,這樣就可以得到一批接口信息。那么實際能做什么呢,靜待后續。

 
 
相關TAG標簽
上一篇:臺積電:絕大多數7nm客戶都會轉向6nm_IT新聞_博客園
下一篇:最后一頁
相關文章
圖文推薦

關于我們 | 聯系我們 | 廣告服務 | 投資合作 | 版權申明 | 在線幫助 | 網站地圖 | 作品發布 | Vip技術培訓 | 舉報中心

版權所有: 紅黑聯盟--致力于做實用的IT技術學習網站

美女MM131爽爽爽毛片