Search in sources :

Example 11 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class BSFTestElement method initManager.

protected void initManager(BSFManager mgr) throws BSFException {
    final String label = getName();
    final String fileName = getFilename();
    final String scriptParameters = getParameters();
    // Use actual class name for log
    final Logger logger = LoggerFactory.getLogger(getClass());
    // $NON-NLS-1$
    mgr.declareBean("log", logger, Logger.class);
    // $NON-NLS-1$
    mgr.declareBean("Label", label, String.class);
    // $NON-NLS-1$
    mgr.declareBean("FileName", fileName, String.class);
    // $NON-NLS-1$
    mgr.declareBean("Parameters", scriptParameters, String.class);
    //$NON-NLS-1$
    String[] args = JOrphanUtils.split(scriptParameters, " ");
    //$NON-NLS-1$
    mgr.declareBean("args", args, args.getClass());
    // Add variables for access to context and variables
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    Properties props = JMeterUtils.getJMeterProperties();
    // $NON-NLS-1$
    mgr.declareBean("ctx", jmctx, jmctx.getClass());
    // $NON-NLS-1$
    mgr.declareBean("vars", vars, vars.getClass());
    // $NON-NLS-1$
    mgr.declareBean("props", props, props.getClass());
    // For use in debugging:
    // $NON-NLS-1$
    mgr.declareBean("OUT", System.out, PrintStream.class);
    // Most subclasses will need these:
    Sampler sampler = jmctx.getCurrentSampler();
    mgr.declareBean("sampler", sampler, Sampler.class);
    SampleResult prev = jmctx.getPreviousResult();
    mgr.declareBean("prev", prev, SampleResult.class);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Logger(org.slf4j.Logger) Properties(java.util.Properties)

Example 12 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class BeanShellPreProcessor method process.

@Override
public void process() {
    final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
    if (bshInterpreter == null) {
        log.error("BeanShell not found");
        return;
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    Sampler sam = jmctx.getCurrentSampler();
    try {
        // Add variables for access to context and variables
        //$NON-NLS-1$
        bshInterpreter.set("sampler", sam);
        processFileOrScript(bshInterpreter);
    } catch (JMeterException e) {
        if (log.isWarnEnabled()) {
            log.warn("Problem in BeanShell script. {}", e.toString());
        }
    }
}
Also used : JMeterException(org.apache.jorphan.util.JMeterException) JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter)

Example 13 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestResultAction method setUp.

@Before
public void setUp() {
    JMeterContext jmctx = JMeterContextService.getContext();
    resultAction = new ResultAction();
    resultAction.setThreadContext(jmctx);
    JMeterVariables vars = new JMeterVariables();
    jmctx.setVariables(vars);
    sampleResult = new SampleResult();
    sampleResult.setResponseData(data, null);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) ResultAction(org.apache.jmeter.reporters.ResultAction) Before(org.junit.Before)

Example 14 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestResultSaver method setUp.

@Before
public void setUp() {
    JMeterContext jmctx = JMeterContextService.getContext();
    resultSaver = new ResultSaver();
    resultSaver.setThreadContext(jmctx);
    jmctx.setVariables(vars);
    sampleResult = new SampleResult();
    sampleResult.setResponseData(data, null);
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Before(org.junit.Before)

Example 15 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class AnchorModifier method process.

/**
     * Modifies an Entry object based on HTML response text.
     */
@Override
public void process() {
    JMeterContext context = getThreadContext();
    Sampler sam = context.getCurrentSampler();
    SampleResult res = context.getPreviousResult();
    HTTPSamplerBase sampler;
    HTTPSampleResult result;
    if (!(sam instanceof HTTPSamplerBase) || !(res instanceof HTTPSampleResult)) {
        log.info("Can't apply HTML Link Parser when the previous" + " sampler run is not an HTTP Request.");
        return;
    } else {
        sampler = (HTTPSamplerBase) sam;
        result = (HTTPSampleResult) res;
    }
    List<HTTPSamplerBase> potentialLinks = new ArrayList<>();
    String responseText = result.getResponseDataAsString();
    // $NON-NLS-1$
    int index = responseText.indexOf('<');
    if (index == -1) {
        index = 0;
    }
    if (log.isDebugEnabled()) {
        log.debug("Check for matches against: " + sampler.toString());
    }
    Document html = (Document) HtmlParsingUtils.getDOM(responseText.substring(index));
    addAnchorUrls(html, result, sampler, potentialLinks);
    addFormUrls(html, result, sampler, potentialLinks);
    addFramesetUrls(html, result, sampler, potentialLinks);
    if (!potentialLinks.isEmpty()) {
        HTTPSamplerBase url = potentialLinks.get(ThreadLocalRandom.current().nextInt(potentialLinks.size()));
        if (log.isDebugEnabled()) {
            log.debug("Selected: " + url.toString());
        }
        sampler.setDomain(url.getDomain());
        sampler.setPath(url.getPath());
        if (url.getMethod().equals(HTTPConstants.POST)) {
            for (JMeterProperty jMeterProperty : sampler.getArguments()) {
                Argument arg = (Argument) jMeterProperty.getObjectValue();
                modifyArgument(arg, url.getArguments());
            }
        } else {
            sampler.setArguments(url.getArguments());
        // config.parseArguments(url.getQueryString());
        }
        sampler.setProtocol(url.getProtocol());
    } else {
        log.debug("No matches found");
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) JMeterContext(org.apache.jmeter.threads.JMeterContext) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) Sampler(org.apache.jmeter.samplers.Sampler) ArrayList(java.util.ArrayList) SampleResult(org.apache.jmeter.samplers.SampleResult) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) Document(org.w3c.dom.Document) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)47 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)33 SampleResult (org.apache.jmeter.samplers.SampleResult)21 Test (org.junit.Test)11 Sampler (org.apache.jmeter.samplers.Sampler)9 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 Before (org.junit.Before)8 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)7 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)2 MatchResult (org.apache.oro.text.regex.MatchResult)2