Search in sources :

Example 6 with AssertionResult

use of org.apache.jmeter.assertions.AssertionResult in project jmeter by apache.

the class XPathExtractor method process.

/**
     * Do the job - extract value from (X)HTML response using XPath Query.
     * Return value as variable defined by REFNAME. Returns DEFAULT value
     * if not found.
     */
@Override
public void process() {
    JMeterContext context = getThreadContext();
    final SampleResult previousResult = context.getPreviousResult();
    if (previousResult == null) {
        return;
    }
    JMeterVariables vars = context.getVariables();
    String refName = getRefName();
    vars.put(refName, getDefaultValue());
    final String matchNR = concat(refName, REF_MATCH_NR);
    // number of previous matches
    int prevCount = 0;
    try {
        prevCount = Integer.parseInt(vars.get(matchNR));
    } catch (NumberFormatException e) {
    // ignored
    }
    // In case parse fails // $NON-NLS-1$
    vars.put(matchNR, "0");
    // In case parse fails // $NON-NLS-1$
    vars.remove(concat(refName, "1"));
    int matchNumber = getMatchNumber();
    List<String> matches = new ArrayList<>();
    try {
        if (isScopeVariable()) {
            String inputString = vars.get(getVariableName());
            if (inputString != null) {
                if (inputString.length() > 0) {
                    Document d = parseResponse(inputString);
                    getValuesForXPath(d, getXPathQuery(), matches, matchNumber);
                }
            } else {
                if (log.isWarnEnabled()) {
                    log.warn("No variable '{}' found to process by XPathExtractor '{}', skipping processing", getVariableName(), getName());
                }
            }
        } else {
            List<SampleResult> samples = getSampleList(previousResult);
            for (SampleResult res : samples) {
                Document d = parseResponse(res.getResponseDataAsString());
                getValuesForXPath(d, getXPathQuery(), matches, matchNumber);
            }
        }
        final int matchCount = matches.size();
        vars.put(matchNR, String.valueOf(matchCount));
        if (matchCount > 0) {
            String value = matches.get(0);
            if (value != null) {
                vars.put(refName, value);
            }
            for (int i = 0; i < matchCount; i++) {
                value = matches.get(i);
                if (value != null) {
                    vars.put(concat(refName, i + 1), matches.get(i));
                }
            }
        }
        // Just in case
        vars.remove(concat(refName, matchCount + 1));
        // Clear any other remaining variables
        for (int i = matchCount + 2; i <= prevCount; i++) {
            vars.remove(concat(refName, i));
        }
    } catch (IOException e) {
        // e.g. DTD not reachable
        log.error("IOException on ({})", getXPathQuery(), e);
        AssertionResult ass = new AssertionResult(getName());
        ass.setError(true);
        ass.setFailureMessage(new StringBuilder("IOException: ").append(e.getLocalizedMessage()).toString());
        previousResult.addAssertionResult(ass);
        previousResult.setSuccessful(false);
    } catch (ParserConfigurationException e) {
        // Should not happen
        final String errrorMessage = "ParserConfigurationException while processing (" + getXPathQuery() + ")";
        log.error(errrorMessage, e);
        throw new JMeterError(errrorMessage, e);
    } catch (SAXException e) {
        // Can happen for bad input document
        if (log.isWarnEnabled()) {
            log.warn("SAXException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
        }
        // Should this also fail the sample?
        addAssertionFailure(previousResult, e, false);
    } catch (TransformerException e) {
        // Can happen for incorrect XPath expression
        if (log.isWarnEnabled()) {
            log.warn("TransformerException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
        }
        addAssertionFailure(previousResult, e, false);
    } catch (TidyException e) {
        // Will already have been logged by XPathUtil
        // fail the sample
        addAssertionFailure(previousResult, e, true);
    }
}
Also used : AssertionResult(org.apache.jmeter.assertions.AssertionResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) TidyException(org.apache.jmeter.util.TidyException) SAXException(org.xml.sax.SAXException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterError(org.apache.jorphan.util.JMeterError) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 7 with AssertionResult

use of org.apache.jmeter.assertions.AssertionResult in project jmeter by apache.

the class AssertionVisualizer method getAssertionResult.

private String getAssertionResult(SampleResult res) {
    if (res != null) {
        StringBuilder display = new StringBuilder();
        AssertionResult[] assertionResults = res.getAssertionResults();
        for (AssertionResult item : assertionResults) {
            if (item.isFailure() || item.isError()) {
                // $NON-NLS-1$
                display.append("\n\t");
                // $NON-NLS-1$
                display.append(item.getName() != null ? item.getName() + " : " : "");
                display.append(item.getFailureMessage());
            }
        }
        return display.toString();
    }
    return "";
}
Also used : AssertionResult(org.apache.jmeter.assertions.AssertionResult)

Example 8 with AssertionResult

use of org.apache.jmeter.assertions.AssertionResult in project jmeter by apache.

the class SamplerResultTab method setupTabPane.

@Override
@SuppressWarnings("boxing")
public void setupTabPane() {
    // Clear all data before display a new
    this.clearData();
    StyledDocument statsDoc = stats.getStyledDocument();
    try {
        if (userObject instanceof SampleResult) {
            sampleResult = (SampleResult) userObject;
            // We are displaying a SampleResult
            setupTabPaneForSampleResult();
            requestPanel.setSamplerResult(sampleResult);
            final String samplerClass = sampleResult.getClass().getName();
            String typeResult = samplerClass.substring(1 + samplerClass.lastIndexOf('.'));
            StringBuilder statsBuff = new StringBuilder(200);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_thread_name")).append(sampleResult.getThreadName()).append(NL);
            String startTime = dateFormat.format(new Date(sampleResult.getStartTime()));
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_sample_start")).append(startTime).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_load_time")).append(sampleResult.getTime()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_connect_time")).append(sampleResult.getConnectTime()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_latency")).append(sampleResult.getLatency()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_size_in_bytes")).append(sampleResult.getBytesAsLong()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_sent_bytes")).append(sampleResult.getSentBytes()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_size_headers_in_bytes")).append(sampleResult.getHeadersSize()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_size_body_in_bytes")).append(sampleResult.getBodySizeAsLong()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_sample_count")).append(sampleResult.getSampleCount()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_error_count")).append(sampleResult.getErrorCount()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_datatype")).append(sampleResult.getDataType()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
            // reset for reuse
            statsBuff.setLength(0);
            String responseCode = sampleResult.getResponseCode();
            int responseLevel = 0;
            if (responseCode != null) {
                try {
                    responseLevel = Integer.parseInt(responseCode) / 100;
                } catch (NumberFormatException numberFormatException) {
                // no need to change the foreground color
                }
            }
            Style style = null;
            switch(responseLevel) {
                case 3:
                    style = statsDoc.getStyle(STYLE_REDIRECT);
                    break;
                case 4:
                    style = statsDoc.getStyle(STYLE_CLIENT_ERROR);
                    break;
                case 5:
                    style = statsDoc.getStyle(STYLE_SERVER_ERROR);
                    break;
                default:
                    // quieten Findbugs
                    break;
            }
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_response_code")).append(responseCode).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), style);
            // reset for reuse
            statsBuff.setLength(0);
            // response message label
            String responseMsgStr = sampleResult.getResponseMessage();
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_response_message")).append(responseMsgStr).append(NL);
            statsBuff.append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_response_headers")).append(NL);
            statsBuff.append(sampleResult.getResponseHeaders()).append(NL);
            statsBuff.append(NL);
            //$NON-NLS-1$ $NON-NLS-2$
            statsBuff.append(typeResult + " " + JMeterUtils.getResString("view_results_fields")).append(NL);
            //$NON-NLS-1$
            statsBuff.append("ContentType: ").append(sampleResult.getContentType()).append(NL);
            //$NON-NLS-1$
            statsBuff.append("DataEncoding: ").append(sampleResult.getDataEncodingNoDefault()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
            // NOSONAR Help gc
            statsBuff = null;
            // Tabbed results: fill table
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_thread_name"), sampleResult.getThreadName()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_sample_start"), startTime));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_load_time"), sampleResult.getTime()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_connect_time"), sampleResult.getConnectTime()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_latency"), sampleResult.getLatency()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_size_in_bytes"), sampleResult.getBytesAsLong()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_sent_bytes"), sampleResult.getSentBytes()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_size_headers_in_bytes"), sampleResult.getHeadersSize()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_size_body_in_bytes"), sampleResult.getBodySizeAsLong()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_sample_count"), sampleResult.getSampleCount()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_error_count"), sampleResult.getErrorCount()));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_response_code"), responseCode));
            //$NON-NLS-1$
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel("view_results_response_message"), responseMsgStr));
            // Parsed response headers
            LinkedHashMap<String, String> lhm = JMeterUtils.parseHeaders(sampleResult.getResponseHeaders());
            Set<Entry<String, String>> keySet = lhm.entrySet();
            for (Entry<String, String> entry : keySet) {
                resHeadersModel.addRow(new RowResult(entry.getKey(), entry.getValue()));
            }
            // Fields table
            //$NON-NLS-1$
            resFieldsModel.addRow(new RowResult("Type Result ", typeResult));
            //not sure needs I18N?
            //$NON-NLS-1$
            resFieldsModel.addRow(new RowResult("ContentType", sampleResult.getContentType()));
            //$NON-NLS-1$
            resFieldsModel.addRow(new RowResult("DataEncoding", sampleResult.getDataEncodingNoDefault()));
            // Reset search
            if (activateSearchExtension) {
                searchTextExtension.resetTextToFind();
            }
        } else if (userObject instanceof AssertionResult) {
            assertionResult = (AssertionResult) userObject;
            // We are displaying an AssertionResult
            setupTabPaneForAssertionResult();
            StringBuilder statsBuff = new StringBuilder(100);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_assertion_error")).append(assertionResult.isError()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_assertion_failure")).append(assertionResult.isFailure()).append(NL);
            //$NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_assertion_failure_message")).append(assertionResult.getFailureMessage()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
        }
        stats.setCaretPosition(1);
    } catch (BadLocationException exc) {
        stats.setText(exc.getLocalizedMessage());
    }
}
Also used : AssertionResult(org.apache.jmeter.assertions.AssertionResult) StyledDocument(javax.swing.text.StyledDocument) Date(java.util.Date) Entry(java.util.Map.Entry) Style(javax.swing.text.Style) SampleResult(org.apache.jmeter.samplers.SampleResult) BadLocationException(javax.swing.text.BadLocationException)

Example 9 with AssertionResult

use of org.apache.jmeter.assertions.AssertionResult in project jmeter by apache.

the class ViewResultsFullVisualizer method addSubResults.

private void addSubResults(DefaultMutableTreeNode currNode, SampleResult res) {
    SampleResult[] subResults = res.getSubResults();
    int leafIndex = 0;
    for (SampleResult child : subResults) {
        log.debug("updateGui1 : child sample result - {}", child);
        DefaultMutableTreeNode leafNode = new SearchableTreeNode(child, treeModel);
        treeModel.insertNodeInto(leafNode, currNode, leafIndex++);
        addSubResults(leafNode, child);
        // Add any assertion that failed as children of the sample node
        AssertionResult[] assertionResults = child.getAssertionResults();
        int assertionIndex = leafNode.getChildCount();
        for (AssertionResult item : assertionResults) {
            if (item.isFailure() || item.isError()) {
                DefaultMutableTreeNode assertionNode = new SearchableTreeNode(item, treeModel);
                treeModel.insertNodeInto(assertionNode, leafNode, assertionIndex++);
            }
        }
    }
}
Also used : AssertionResult(org.apache.jmeter.assertions.AssertionResult) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) SampleResult(org.apache.jmeter.samplers.SampleResult)

Aggregations

AssertionResult (org.apache.jmeter.assertions.AssertionResult)9 SampleResult (org.apache.jmeter.samplers.SampleResult)6 Date (java.util.Date)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 JMeterError (org.apache.jorphan.util.JMeterError)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 BadLocationException (javax.swing.text.BadLocationException)1 Style (javax.swing.text.Style)1 StyledDocument (javax.swing.text.StyledDocument)1 TreePath (javax.swing.tree.TreePath)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 Assertion (org.apache.jmeter.assertions.Assertion)1 SampleSaveConfiguration (org.apache.jmeter.samplers.SampleSaveConfiguration)1 StatisticalSampleResult (org.apache.jmeter.samplers.StatisticalSampleResult)1 AbstractScopedAssertion (org.apache.jmeter.testelement.AbstractScopedAssertion)1 JMeterContext (org.apache.jmeter.threads.JMeterContext)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1