Search in sources :

Example 11 with Sampler

use of org.apache.jmeter.samplers.Sampler 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 12 with Sampler

use of org.apache.jmeter.samplers.Sampler 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)

Example 13 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class RegExUserParameters method process.

@Override
public void process() {
    if (log.isDebugEnabled()) {
        //$NON-NLS-1$
        log.debug(Thread.currentThread().getName() + " Running up named: " + getName());
    }
    Sampler entry = getThreadContext().getCurrentSampler();
    if (!(entry instanceof HTTPSamplerBase)) {
        return;
    }
    Map<String, String> paramMap = buildParamsMap();
    if (paramMap == null || paramMap.isEmpty()) {
        log.info("RegExUserParameters element:" + getName() + " => Referenced RegExp was not found, no parameter will be changed");
        return;
    }
    HTTPSamplerBase sampler = (HTTPSamplerBase) entry;
    for (JMeterProperty jMeterProperty : sampler.getArguments()) {
        Argument arg = (Argument) jMeterProperty.getObjectValue();
        String oldValue = arg.getValue();
        // if parameter name exists in http request
        // then change its value with value obtained with regular expression
        String val = paramMap.get(arg.getName());
        if (val != null) {
            arg.setValue(val);
        }
        if (log.isDebugEnabled()) {
            log.debug("RegExUserParameters element:" + getName() + " => changed parameter: " + arg.getName() + " = " + arg.getValue() + ", was:" + oldValue);
        }
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) Sampler(org.apache.jmeter.samplers.Sampler) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Example 14 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class URLRewritingModifier method process.

@Override
public void process() {
    JMeterContext ctx = getThreadContext();
    Sampler sampler = ctx.getCurrentSampler();
    if (!(sampler instanceof HTTPSamplerBase)) {
        // Ignore non-HTTP samplers
        return;
    }
    SampleResult responseText = ctx.getPreviousResult();
    if (responseText == null) {
        return;
    }
    initRegex(getArgumentName());
    String text = responseText.getResponseDataAsString();
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String value = "";
    if (isPathExtension() && isPathExtensionNoEquals() && isPathExtensionNoQuestionmark()) {
        if (matcher.contains(text, pathExtensionNoEqualsNoQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoQuestionmark()
    isPathExtension() && isPathExtensionNoEquals()) {
        if (matcher.contains(text, pathExtensionNoEqualsQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoEquals()
    isPathExtension() && isPathExtensionNoQuestionmark()) {
        if (matcher.contains(text, pathExtensionEqualsNoQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else if (// && !isPathExtensionNoEquals() && !isPathExtensionNoQuestionmark()
    isPathExtension()) {
        if (matcher.contains(text, pathExtensionEqualsQuestionmarkRegexp)) {
            MatchResult result = matcher.getMatch();
            value = result.group(1);
        }
    } else // if ! isPathExtension()
    {
        if (matcher.contains(text, parameterRegexp)) {
            MatchResult result = matcher.getMatch();
            for (int i = 1; i < result.groups(); i++) {
                value = result.group(i);
                if (value != null) {
                    break;
                }
            }
        }
    }
    // Bug 15025 - save session value across samplers
    if (shouldCache()) {
        if (value == null || value.length() == 0) {
            value = savedValue;
        } else {
            savedValue = value;
        }
    }
    modify((HTTPSamplerBase) sampler, value);
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Example 15 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TestSwitchController method nextName.

// Get next sample and its name
private String nextName(GenericController c) {
    Sampler s = c.next();
    String n;
    if (s == null) {
        return null;
    }
    n = s.getName();
    return n;
}
Also used : TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler)

Aggregations

Sampler (org.apache.jmeter.samplers.Sampler)33 SampleResult (org.apache.jmeter.samplers.SampleResult)11 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)9 JMeterContext (org.apache.jmeter.threads.JMeterContext)9 Test (org.junit.Test)9 TransactionSampler (org.apache.jmeter.control.TransactionSampler)6 DebugSampler (org.apache.jmeter.sampler.DebugSampler)6 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)5 Controller (org.apache.jmeter.control.Controller)3 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterStopTestException (org.apache.jorphan.util.JMeterStopTestException)3 JMeterStopTestNowException (org.apache.jorphan.util.JMeterStopTestNowException)3 JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)3 Logger (org.slf4j.Logger)3 Properties (java.util.Properties)2 Argument (org.apache.jmeter.config.Argument)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 TestElement (org.apache.jmeter.testelement.TestElement)2 Cache (com.github.benmanes.caffeine.cache.Cache)1