Search in sources :

Example 26 with JMeterVariables

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

the class HtmlExtractor method process.

/**
 * Parses the response data using CSS/JQuery expressions and saving the results
 * into variables for use later in the test.
 *
 * @see org.apache.jmeter.processor.PostProcessor#process()
 */
@Override
public void process() {
    JMeterContext context = getThreadContext();
    SampleResult previousResult = context.getPreviousResult();
    if (previousResult == null) {
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("HtmlExtractor {}: processing result", getName());
    }
    // Fetch some variables
    JMeterVariables vars = context.getVariables();
    String refName = getRefName();
    String expression = getExpression();
    String attribute = getAttribute();
    int matchNumber = getMatchNumber();
    final String defaultValue = getDefaultValue();
    if (defaultValue.length() > 0 || isEmptyDefaultValue()) {
        // Only replace default if it is provided or empty default value is explicitly requested
        vars.put(refName, defaultValue);
    }
    try {
        List<String> matches = extractMatchingStrings(vars, expression, attribute, matchNumber, previousResult);
        int prevCount = 0;
        String prevString = vars.get(refName + REF_MATCH_NR);
        if (prevString != null) {
            // ensure old value is not left defined
            vars.remove(refName + REF_MATCH_NR);
            try {
                prevCount = Integer.parseInt(prevString);
            } catch (NumberFormatException nfe) {
                if (log.isWarnEnabled()) {
                    log.warn("{}: Could not parse number: '{}'.", getName(), prevString);
                }
            }
        }
        // Number of refName_n variable sets to keep
        int matchCount = 0;
        String match;
        if (matchNumber >= 0) {
            // Original match behaviour
            match = getCorrectMatch(matches, matchNumber);
            if (match != null) {
                vars.put(refName, match);
            }
        } else // < 0 means we save all the matches
        {
            matchCount = matches.size();
            // Save the count
            vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));
            for (int i = 1; i <= matchCount; i++) {
                match = getCorrectMatch(matches, i);
                if (match != null) {
                    final String refNameN = refName + UNDERSCORE + i;
                    vars.put(refNameN, match);
                }
            }
        }
        // Remove any left-over variables
        for (int i = matchCount + 1; i <= prevCount; i++) {
            final String refNameN = refName + UNDERSCORE + i;
            vars.remove(refNameN);
        }
    } catch (RuntimeException e) {
        if (log.isWarnEnabled()) {
            log.warn("{}: Error while generating result. {}", getName(), e.toString());
        }
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult)

Example 27 with JMeterVariables

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

the class TestCookieManagerThreadIteration method setUp.

@BeforeEach
public void setUp() {
    jmctx = JMeterContextService.getContext();
    jmvars = new JMeterVariables();
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 28 with JMeterVariables

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

the class TestAuthManagerThreadIteration method setUp.

@BeforeEach
public void setUp() {
    jmctx = JMeterContextService.getContext();
    jmvars = new JMeterVariables();
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with JMeterVariables

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

the class TestCacheManagerThreadIteration method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.cacheManager = new CacheManager();
    this.currentTimeInGMT = makeDate(new Date());
    this.url = new URL(LOCAL_HOST);
    this.sampleResultOK = getSampleResultWithSpecifiedResponseCode("200");
    this.httpMethod = new HttpPostStub();
    this.httpResponse = new HttpResponseStub();
    this.httpMethod.setURI(this.url.toURI());
    jmctx = JMeterContextService.getContext();
    jmvars = new JMeterVariables();
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Date(java.util.Date) URL(java.net.URL) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with JMeterVariables

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

the class TestValueReplacer method setUp.

@BeforeEach
public void setUp() {
    variables = new TestPlan();
    variables.addParameter("server", "jakarta.apache.org");
    variables.addParameter("username", "jack");
    // The following used to be jacks_password, but the Arguments class uses
    // HashMap for which the order is not defined.
    variables.addParameter("password", "his_password");
    variables.addParameter("normal_regex", "Hello .*");
    variables.addParameter("bounded_regex", "(<.*>)");
    JMeterVariables vars = new JMeterVariables();
    vars.put("server", "jakarta.apache.org");
    JMeterContextService.getContext().setVariables(vars);
    JMeterContextService.getContext().setSamplingStarted(true);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) TestPlan(org.apache.jmeter.testelement.TestPlan) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

JMeterVariables (org.apache.jmeter.threads.JMeterVariables)193 SampleResult (org.apache.jmeter.samplers.SampleResult)71 JMeterContext (org.apache.jmeter.threads.JMeterContext)64 BeforeEach (org.junit.jupiter.api.BeforeEach)46 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)27 Test (org.junit.jupiter.api.Test)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 Test (org.junit.Test)16 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)11 Before (org.junit.Before)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)6 TestPlan (org.apache.jmeter.testelement.TestPlan)6 HashMap (java.util.HashMap)5 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)5 URL (java.net.URL)4 Arguments (org.apache.jmeter.config.Arguments)4 DebugSampler (org.apache.jmeter.sampler.DebugSampler)4