JMeter二次開發(2)-編寫 JSON Assertion 插件

本篇文章主要介紹如何對JMeter進行二次開發,添加本身所需的功能。這裏以Json驗證爲例進行說明。在web接口測試過程當中,JSON的應用已經很是廣泛,但原聲的JMeter並無提供Json及JsonPath的驗證,這裏以JSON格式驗證爲例進行JMeter二次開發簡單說明。git

 

準備工做:1)JMeter本地開發環境;2)gson.jar-用於作json數據有效性驗證(也能夠本身編寫驗證邏輯)github

 

具體步驟:web

1.引用gson.jarapache

2.添加JSONAssertion及JSONAssertionGuijson

 

2.1 src/components/org.apache.jmeter.assertions下,新建JSONAssertionapp

public class JSONAssertion extends AbstractTestElement implements Serializable, Assertion, ThreadListener {
    private static final Logger log = LoggingManager.getLoggerForClass();

    private static final long serialVersionUID = 240L;

    /**
     * Returns the result of the Assertion. Here it checks wether the Sample
     * took to long to be considered successful. If so an AssertionResult
     * containing a FailureMessage will be returned. Otherwise the returned
     * AssertionResult will reflect the success of the Sample.
     */
    @Override
    public AssertionResult getResult(SampleResult response) {
        // no error as default
        AssertionResult result = new AssertionResult(getName());
        String resultData = response.getResponseDataAsString();
       
        if (resultData.length()==0) {
            return result.setResultForNull();
        }else{
            Gson gson = new Gson();
            if(!gson.toJsonTree(resultData).isJsonObject()){
                log.debug("Cannot parse result content"); // may well happen
                result.setFailure(true);
                result.setFailureMessage("ResultData is not Json");
            }
        }
        return result;

    }

    @Override
    public void threadStarted() {
    }

    @Override
    public void threadFinished() {
    }
}

2.2 src/components/org.apache.jmeter.assertions.gui下,新建JSONAssertionGuiide

public class JSONAssertionGui extends AbstractAssertionGui {
   
    private static final long serialVersionUID = 240L;

    /**
     * The constructor.
     */
    public JSONAssertionGui() {
        init();
    }

    /**
     * Returns the label to be shown within the JTree-Component.
     */
    @Override
    public String getLabelResource() {
        return "json_assertion_title"; // $NON-NLS-1$
    }

    @Override
    public TestElement createTestElement() {
        JSONAssertion el = new JSONAssertion();
        modifyTestElement(el);
        return el;
    }

    /**
     * Modifies a given TestElement to mirror the data in the gui components.
     * 
     * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
     */
    @Override
    public void modifyTestElement(TestElement el) {
        configureTestElement(el);
    }

    /**
     * Inits the GUI.
     */
    private void init() {
        setLayout(new VerticalLayout(5, VerticalLayout.BOTH, VerticalLayout.TOP));
        setBorder(makeBorder());

        add(makeTitlePanel());
    }
}

 

3.配置properties文件測試

在src/core/org/apache/jmeter/resources/下的.properties文件中進行設置ui

 

 

4.Run As Java Application —>[New Driver]spa

5.JMeter運行後,新建項目進行驗證

 

 

添加Assertion Listener,查看運行結果

 

 

關於新加功能的加載過程:

controllser、samplser、listener、assertion等內容,並非在代碼中new出來的,而是在jmeter啓動時,經過掃描工做路徑下的class,分析class類型,反射生成到jmeter中的。因此,在寫完JSON Assertion的GUI以後,並不須要更改jmeter其餘內容。具體的掃描反射過程,在org.apache.jmeter.testbeans.gui.TestBeanGUI中進行。

 

 

完整代碼地址

https://github.com/buaawp/jmeter/tree/json_assertion

相關文章
相關標籤/搜索