jmeter-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Ohrstrom <johrst...@hotmail.com>
Subject Re: how to create a customm assertion
Date Thu, 11 Sep 2014 02:22:40 GMT
I've never made an assertion, so I could be wrong.  But I think the
general rule of thumb is that you have a gui element and a back end
element (or perhaps it's more a strict design constraint than a rule of
thumb).  You only appear to have the GUI.  What you return in
createTestElement() doesn't implement the Assertion interface. Although
you have the only function from that interface defined here
(getResult( ... )), I'm guessing that Jmeter never calls the GUI
element's getResult, rather the backend element.

My suggestion is make that backend class, and have it implement the
Assertion interface. Also use logging to try to debug.   

I'm guessing Jmeter calls it somehow like this:

	//get a test element from the GUI
TestElement te = new TestAssertion.createTestElement(); 
	//use that TestElement's getResult function 
te.getResult(someSampleIJustMade);

On Wed, 2014-09-10 at 23:59 +0000, Sankaran, Nambi wrote:
> Hi 
> 
> I followed the tutorial provided in the jmeter website.
> I have questions on building custom assertions,
> 
> 1) The custom assertion is not getting invoked during execution. Why?
> 2) The custom assertion is not persisted in the while saving the project. 
>     The JMX file doesn't have the configuration for the custom assertion.
>     How to save the configuration?
> 
> For reference I have attached the Assertion class.
> 
> Thanks
> Nambi
> 
> =============================================================================
> 
> package com.testers.jmeter.assertionlib;
> 
> import java.awt.BorderLayout;
> 
> import javax.swing.BorderFactory;
> import javax.swing.Box;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JTextField;
> 
> import org.apache.jmeter.assertions.AssertionResult;
> import org.apache.jmeter.assertions.gui.AbstractAssertionGui;
> import org.apache.jmeter.extractor.RegexExtractor;
> import org.apache.jmeter.gui.util.HorizontalPanel;
> import org.apache.jmeter.gui.util.VerticalPanel;
> import org.apache.jmeter.samplers.SampleResult;
> import org.apache.jmeter.testelement.TestElement;
> import org.apache.jmeter.testelement.property.BooleanProperty;
> 
> public class TestAssertion extends AbstractAssertionGui {
> 
> 	    private static final long serialVersionUID = 1397205492925472603L;
> 	    private JTextField oldVersionText;
> 	    private JTextField newVersionText;
> 		
> 		public TestAssertion(){
> 			super();
> 			init();
> 		}
> 		
> 		protected void init(){
> 	        setLayout(new BorderLayout());
> 	        setBorder(makeBorder());
> 
> 	        Box box = Box.createVerticalBox();
> 	        box.add(makeTitlePanel());
> 	        box.add(createScopePanel(true));
> 	        box.add( makeSourcePanel() );
> 	        add(box, BorderLayout.NORTH);
> 		}
> 		
> 	    private JPanel makeSourcePanel() {
> 	        JPanel panel = new VerticalPanel();
> 	        panel.setBorder(BorderFactory.createTitledBorder("Text Input")); //$NON-NLS-1$
> 
> 	        JPanel oldPanel = new HorizontalPanel();
> 	        JLabel label = new JLabel();
> 	        label.setText("Old Version: Enter text to match");
> 	        label.setSize(100, 10);
> 	        oldVersionText = new JTextField(1);
> 	        oldVersionText.setSize(100, 10);
> 	        
> 	        oldPanel.add(label);
> 	        oldPanel.add( oldVersionText);
> 	        
> 	        JPanel newPanel = new HorizontalPanel();
> 	        JLabel newLabel = new JLabel();
> 	        newLabel.setText("New Version: Enter Text To Match");
> 	        newLabel.setSize(100, 10);
> 	        newVersionText = new JTextField(1);
> 	        newVersionText.setSize(100, 10);
> 	        
> 	        newPanel.add(newLabel);
> 	        newPanel.add(newVersionText);
> 	        
> 	        panel.add(oldPanel);
> 	        panel.add(newPanel);
> 	        
> 	        //panel.add(newVersionText, BorderLayout.WEST);
> 
> 	        return panel;
> 	    }
> 
> 		public AssertionResult getResult(SampleResult result) {
> 			result.getResponseDataAsString();
> 			
> 			AssertionResult assertion = new AssertionResult("TEST");
> 			return assertion;
> 		}
> 
> 		public TestElement createTestElement() {
> 			RegexExtractor extractor =new RegexExtractor();
> 			modifyTestElement(extractor);
> 			return extractor;
> 		}
> 
> 		public String getStaticLabel(){
> 			return getLabelResource();
> 		}
> 		
> 		public String getLabelResource() {
> 			return "TEST  Assertion";
> 		}
> 		
> 		public void configure( TestElement el){
> 			super.configure(el);
> 		}
> 
> 		public void modifyTestElement(TestElement elem) {
> 			super.configure(elem);
> 			
> 			elem.setProperty( new BooleanProperty(RegexExtractor.USE_HDRS, true) );
> 			elem.setProperty("Number", 4);
> 			elem.setName("Test Assertion");
> 			
> 			if( elem instanceof RegexExtractor ){
> 				RegexExtractor regex = ( RegexExtractor) elem;
> 				regex.setRefName("Name");
> 				regex.setRegex("");
> 				regex.setTemplate("");
> 				regex.setDefaultValue("gege");
> 			}
> 		}
> 
> 	}
> 
> 



Mime
View raw message