Search in sources :

Example 51 with SampleResult

use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.

the class JSONPathExtractorTest method testReported2.

@Ignore
// FIXME: we need to solve this one day
@Test
public void testReported2() {
    System.out.println("process reported");
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData(json3.getBytes());
    context.setPreviousResult(res);
    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setVar("var");
    instance.setJsonPath("$.data[?(@.attr.value>0)][0].attr");
    instance.setDefaultValue("NOTFOUND");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertNotEquals("NOTFOUND", vars.get("var"));
    assertEquals("{value=1}", vars.get("var"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with SampleResult

use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.

the class JSONPathExtractorTest method testProcess_chinese.

@Test
public void testProcess_chinese() {
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    String chinese = "{\"carBrandName\":\"大众\"}";
    res.setResponseData(chinese.getBytes());
    context.setPreviousResult(res);
    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.setJsonPath("$.carBrandName");
    instance.process();
    JMeterVariables vars = context.getVariables();
// freaking "static final" DEFAULT_ENCODING field in SampleResult does not allow us to assert this
// assertEquals("大众", vars.get("test"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Test(org.junit.Test)

Example 53 with SampleResult

use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.

the class JSONPathExtractorTest method testProcess.

@Test
public void testProcess() {
    System.out.println("process");
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData(json.getBytes());
    context.setPreviousResult(res);
    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.setJsonPath("$.store.book[*].author");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Test(org.junit.Test)

Example 54 with SampleResult

use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.

the class OAuthSampler method sample.

@Override
public SampleResult sample() {
    HttpMethodBase httpMethod = null;
    HttpClient client = null;
    InputStream instream = null;
    SampleResult res = new SampleResult();
    try {
        res.setSuccessful(false);
        res.setResponseCode("000");
        res.setSampleLabel(getName());
        res.setURL(getUrl());
        res.setDataEncoding("UTF-8");
        res.setDataType("text/xml");
        res.setSamplerData(getRequestBody());
        res.setMonitor(isMonitor());
        res.sampleStart();
        String urlStr = getUrl().toString();
        log.debug("Start : sample " + urlStr);
        log.debug("method " + getMethod());
        httpMethod = createHttpMethod(getMethod(), urlStr);
        setDefaultRequestHeaders(httpMethod);
        client = setupConnection(getUrl(), httpMethod);
        if (httpMethod instanceof EntityEnclosingMethod) {
            ((EntityEnclosingMethod) httpMethod).setRequestEntity(new StringRequestEntity(getRequestBody(), "text/xml", "UTF-8"));
        }
        overrideHeaders(httpMethod, urlStr, getMethod());
        res.setRequestHeaders(getConnectionHeaders(httpMethod));
        int statusCode = -1;
        try {
            statusCode = client.executeMethod(httpMethod);
        } catch (RuntimeException e) {
            log.error("Exception when executing '" + httpMethod + "'", e);
            throw e;
        }
        instream = httpMethod.getResponseBodyAsStream();
        if (instream != null) {
            Header responseHeader = httpMethod.getResponseHeader(HTTPConstantsInterface.HEADER_CONTENT_ENCODING);
            if (responseHeader != null && HTTPConstantsInterface.ENCODING_GZIP.equals(responseHeader.getValue())) {
                instream = new GZIPInputStream(instream);
            }
            res.setResponseData(readResponse(res, instream, (int) httpMethod.getResponseContentLength()));
        }
        res.sampleEnd();
        res.setResponseCode(Integer.toString(statusCode));
        res.setSuccessful(isSuccessCode(statusCode));
        res.setResponseMessage(httpMethod.getStatusText());
        String ct = null;
        org.apache.commons.httpclient.Header h = httpMethod.getResponseHeader(HTTPConstantsInterface.HEADER_CONTENT_TYPE);
        if (h != null) {
            ct = h.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }
        String responseHeaders = getResponseHeaders(httpMethod);
        res.setResponseHeaders(responseHeaders);
        log.debug("End : sample");
        httpMethod.releaseConnection();
        return res;
    } catch (MalformedURLException e) {
        res.sampleEnd();
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } catch (IllegalArgumentException e) {
        res.sampleEnd();
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } catch (IOException e) {
        res.sampleEnd();
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } finally {
        JOrphanUtils.closeQuietly(instream);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
            return res;
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Header(org.apache.commons.httpclient.Header) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) SampleResult(org.apache.jmeter.samplers.SampleResult) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult)

Example 55 with SampleResult

use of org.apache.jmeter.samplers.SampleResult in project jmeter-plugins by undera.

the class WebDriverSampler method sample.

@Override
public SampleResult sample(Entry e) {
    if (getWebDriver() == null) {
        throw new IllegalArgumentException("Browser has not been configured.  Please ensure at least 1 WebDriverConfig is created for a ThreadGroup.");
    }
    SampleResult res;
    try {
        res = sampleResultClass.newInstance();
    } catch (InstantiationException e1) {
        LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
        res = new SampleResult();
    } catch (IllegalAccessException e1) {
        LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
        res = new SampleResult();
    }
    res.setSampleLabel(getName());
    res.setSamplerData(toString());
    res.setDataType(SampleResult.TEXT);
    res.setContentType("text/plain");
    res.setDataEncoding("UTF-8");
    res.setSuccessful(true);
    LOGGER.debug("Current thread name: '" + getThreadName() + "', has browser: '" + getWebDriver() + "'");
    try {
        final ScriptEngine scriptEngine = createScriptEngineWith(res);
        scriptEngine.eval(getScript());
        // setup the data in the SampleResult
        res.setResponseData(getWebDriver().getPageSource(), null);
        res.setURL(new URL(getWebDriver().getCurrentUrl()));
        if (StringUtils.isEmpty(res.getResponseCode())) {
            res.setResponseCode(res.isSuccessful() ? "200" : "500");
        }
        if (res.isSuccessful()) {
            res.setResponseMessageOK();
        }
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());
        res.setResponseMessage(ex.getMessage());
        res.setResponseData((ex.toString() + "\r\n" + JMeterPluginsUtils.getStackTrace(ex)).getBytes());
        res.setResponseCode("500");
        res.setSuccessful(false);
        if (res.getStartTime() == 0) {
            res.sampleStart();
        }
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
    }
    return res;
}
Also used : SampleResult(org.apache.jmeter.samplers.SampleResult) ScriptEngine(javax.script.ScriptEngine) URL(java.net.URL)

Aggregations

SampleResult (org.apache.jmeter.samplers.SampleResult)379 Test (org.junit.Test)83 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)71 Test (org.junit.jupiter.api.Test)59 JMeterContext (org.apache.jmeter.threads.JMeterContext)47 BeforeEach (org.junit.jupiter.api.BeforeEach)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 SampleEvent (org.apache.jmeter.samplers.SampleEvent)30 Sampler (org.apache.jmeter.samplers.Sampler)30 AssertionResult (org.apache.jmeter.assertions.AssertionResult)27 ArrayList (java.util.ArrayList)26 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)20 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)20 IOException (java.io.IOException)17 Arguments (org.apache.jmeter.config.Arguments)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)13 CorrectedResultCollector (kg.apc.jmeter.vizualizers.CorrectedResultCollector)12 URL (java.net.URL)9 File (java.io.File)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7