View | Details | Raw Unified | Return to bug 60774
Collapse All | Expand All

(-)a/src/components/org/apache/jmeter/assertions/HTMLAssertion.java (+45 lines)
Lines 27-32 import java.io.Serializable; Link Here
27
import java.io.StringWriter;
27
import java.io.StringWriter;
28
import java.nio.charset.StandardCharsets;
28
import java.nio.charset.StandardCharsets;
29
import java.text.MessageFormat;
29
import java.text.MessageFormat;
30
import java.util.List;
30
31
31
import org.apache.jmeter.samplers.SampleResult;
32
import org.apache.jmeter.samplers.SampleResult;
32
import org.apache.jmeter.testelement.AbstractTestElement;
33
import org.apache.jmeter.testelement.AbstractTestElement;
Lines 34-39 import org.apache.jmeter.testelement.property.BooleanProperty; Link Here
34
import org.apache.jmeter.testelement.property.LongProperty;
35
import org.apache.jmeter.testelement.property.LongProperty;
35
import org.apache.jmeter.testelement.property.StringProperty;
36
import org.apache.jmeter.testelement.property.StringProperty;
36
import org.apache.jmeter.util.JMeterUtils;
37
import org.apache.jmeter.util.JMeterUtils;
38
import org.jsoup.parser.ParseError;
39
import org.jsoup.parser.Parser;
37
import org.slf4j.Logger;
40
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
41
import org.slf4j.LoggerFactory;
39
import org.w3c.tidy.Node;
42
import org.w3c.tidy.Node;
Lines 62-67 public class HTMLAssertion extends AbstractTestElement implements Serializable, Link Here
62
65
63
    public static final String FILENAME_KEY = "html_assertion_filename"; //$NON-NLS-1$
66
    public static final String FILENAME_KEY = "html_assertion_filename"; //$NON-NLS-1$
64
67
68
    public static final String USE_JSOUP = "html_assertion_use_jsoup";
69
65
    /**
70
    /**
66
     * 
71
     * 
67
     */
72
     */
Lines 87-92 public class HTMLAssertion extends AbstractTestElement implements Serializable, Link Here
87
92
88
        result.setFailure(false);
93
        result.setFailure(false);
89
94
95
        if (getUseJsoup()) {
96
            return checkWithJsoup(inResponse, result);
97
        } else {
98
            return checkWithJTidy(inResponse, result);
99
        }
100
    }
101
102
    private AssertionResult checkWithJTidy(SampleResult inResponse,
103
            AssertionResult result) {
90
        // create parser
104
        // create parser
91
        Tidy tidy = null;
105
        Tidy tidy = null;
92
        try {
106
        try {
Lines 167-172 public class HTMLAssertion extends AbstractTestElement implements Serializable, Link Here
167
        return result;
181
        return result;
168
    }
182
    }
169
183
184
    private AssertionResult checkWithJsoup(SampleResult inResponse,
185
            AssertionResult result) {
186
        try {
187
            Parser parser = Parser.htmlParser();
188
            parser.setTrackErrors((int) Math.max(getErrorThreshold(), getWarningThreshold()));
189
            parser.parseInput(inResponse.getResponseDataAsString(), "");
190
            final List<ParseError> errors = parser.getErrors();
191
            log.debug("HTML Errors: {}", errors);
192
            if (!errors.isEmpty() && errors.size() > Math
193
                    .min(getWarningThreshold(), getErrorThreshold())) {
194
                result.setFailure(true);
195
                result.setFailureMessage(String.format(
196
                        "Found at least %d errors while parsing HTML Document: %s",
197
                        errors.size(), errors));
198
            }
199
        } catch (Exception e) {
200
            result.setFailure(true);
201
            result.setFailureMessage("Can't parse document as HTML: " + e.getMessage());
202
            log.warn("Problem parsing the document as HTML", e);
203
        }
204
        return result;
205
    }
206
170
    /**
207
    /**
171
     * Writes the output of tidy to file.
208
     * Writes the output of tidy to file.
172
     * 
209
     * 
Lines 352-355 public class HTMLAssertion extends AbstractTestElement implements Serializable, Link Here
352
    public void setFilename(String inName) {
389
    public void setFilename(String inName) {
353
        setProperty(FILENAME_KEY, inName);
390
        setProperty(FILENAME_KEY, inName);
354
    }
391
    }
392
393
    public void setUseJsoup(boolean useJsoup) {
394
        setProperty(USE_JSOUP, useJsoup, false);
395
    }
396
397
    public boolean getUseJsoup() {
398
        return getPropertyAsBoolean(USE_JSOUP);
399
    }
355
}
400
}
(-)a/src/components/org/apache/jmeter/assertions/gui/HTMLAssertionGui.java (-2 / +29 lines)
Lines 28-33 import javax.swing.BorderFactory; Link Here
28
import javax.swing.ButtonGroup;
28
import javax.swing.ButtonGroup;
29
import javax.swing.JCheckBox;
29
import javax.swing.JCheckBox;
30
import javax.swing.JComboBox;
30
import javax.swing.JComboBox;
31
import javax.swing.JComponent;
31
import javax.swing.JLabel;
32
import javax.swing.JLabel;
32
import javax.swing.JOptionPane;
33
import javax.swing.JOptionPane;
33
import javax.swing.JPanel;
34
import javax.swing.JPanel;
Lines 74-79 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
74
75
75
    private FilePanel filePanel = null;
76
    private FilePanel filePanel = null;
76
77
78
    private JCheckBox useJsoup = null;
79
80
    private VerticalPanel formatPanel;
81
77
    /**
82
    /**
78
     * The constructor.
83
     * The constructor.
79
     */
84
     */
Lines 144-149 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
144
            ((HTMLAssertion) inElement).setXML();
149
            ((HTMLAssertion) inElement).setXML();
145
        }
150
        }
146
        ((HTMLAssertion) inElement).setFilename(filePanel.getFilename());
151
        ((HTMLAssertion) inElement).setFilename(filePanel.getFilename());
152
153
        ((HTMLAssertion) inElement).setUseJsoup(useJsoup.isSelected());
147
    }
154
    }
148
155
149
    /**
156
    /**
Lines 162-167 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
162
        warningThresholdField.setText("0"); //$NON-NLS-1$
169
        warningThresholdField.setText("0"); //$NON-NLS-1$
163
        filePanel.setFilename(""); //$NON-NLS-1$
170
        filePanel.setFilename(""); //$NON-NLS-1$
164
        errorsOnly.setSelected(false);
171
        errorsOnly.setSelected(false);
172
        useJsoup.setSelected(false);
165
    }
173
    }
166
174
167
    /**
175
    /**
Lines 192-197 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
192
            warningThresholdField.setEditable(true);
200
            warningThresholdField.setEditable(true);
193
        }
201
        }
194
        filePanel.setFilename(lAssertion.getFilename());
202
        filePanel.setFilename(lAssertion.getFilename());
203
        useJsoup.setSelected(lAssertion.getUseJsoup());
204
        setJsoupDependentFlags();
195
    }
205
    }
196
206
197
    /**
207
    /**
Lines 210-215 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
210
        VerticalPanel assertionPanel = new VerticalPanel();
220
        VerticalPanel assertionPanel = new VerticalPanel();
211
        assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tidy Settings"));
221
        assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tidy Settings"));
212
222
223
        useJsoup = new JCheckBox("Use Jsoup");
224
        useJsoup.addActionListener(this);
225
        assertionPanel.add(useJsoup);
226
213
        // doctype
227
        // doctype
214
        HorizontalPanel docTypePanel = new HorizontalPanel();
228
        HorizontalPanel docTypePanel = new HorizontalPanel();
215
        docTypeBox = new JComboBox<>(new String[] { "omit", "auto", "strict", "loose" });
229
        docTypeBox = new JComboBox<>(new String[] { "omit", "auto", "strict", "loose" });
Lines 220-226 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
220
        assertionPanel.add(docTypePanel);
234
        assertionPanel.add(docTypePanel);
221
235
222
        // format (HTML, XHTML, XML)
236
        // format (HTML, XHTML, XML)
223
        VerticalPanel formatPanel = new VerticalPanel();
237
        formatPanel = new VerticalPanel();
224
        formatPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Format"));
238
        formatPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Format"));
225
        htmlRadioButton = new JRadioButton("HTML", true); //$NON-NLS-1$
239
        htmlRadioButton = new JRadioButton("HTML", true); //$NON-NLS-1$
226
        xhtmlRadioButton = new JRadioButton("XHTML", false); //$NON-NLS-1$
240
        xhtmlRadioButton = new JRadioButton("XHTML", false); //$NON-NLS-1$
Lines 275-280 public class HTMLAssertionGui extends AbstractAssertionGui implements KeyListene Link Here
275
            warningThresholdField.setEnabled(true);
289
            warningThresholdField.setEnabled(true);
276
            warningThresholdField.setEditable(true);
290
            warningThresholdField.setEditable(true);
277
        }
291
        }
292
        setJsoupDependentFlags();
293
    }
294
295
    private void setJsoupDependentFlags() {
296
        boolean jsoupFlag = !useJsoup.isSelected();
297
        docTypeBox.setEditable(jsoupFlag);
298
        docTypeBox.setEnabled(jsoupFlag);
299
        filePanel.setEnabled(jsoupFlag);
300
        filePanel.enableFile(jsoupFlag);
301
        formatPanel.setEnabled(jsoupFlag);
302
        htmlRadioButton.setEnabled(jsoupFlag);
303
        xhtmlRadioButton.setEnabled(jsoupFlag);
304
        xmlRadioButton.setEnabled(jsoupFlag);
305
        errorsOnly.setEnabled(jsoupFlag);
278
    }
306
    }
279
307
280
    @Override
308
    @Override
281
- 

Return to bug 60774