JMeter is one of the best open source tools in the Test Automation Community. It comes with all the possible extensions to come up with our test scripts quickly. To make our life even more easier, It also lets us to come up with our own plugins by implementing few interfaces. In this article, I would like to show you how we can create a custom plugin for JMeter – A Property File Reader. I had already shared this utility in this article. However I had NOT explained how it was implemented in the article.
Aim of this article is to help you to come up with your own plugin in case of any unique requirements where JMeter’s existing plugins do not help.
Our goal here is to create a property file which contains our test input parameters & create our config element which should appear in the below selection (as shown below) & read the given property file.
Once we added our plugin in our test, Our plugin should be able to read the property file and the test can directly use these properties in our test plan as shown here.
${__P(prop1)} will print value1
${__P(prop2)} will print value2
I will refer to this ConfigTestElement which has to be extended to implement our ‘Property File Reader’ plugin & this JMeter tutorial to get some idea.
Lets first set up our IDE with all the dependencies.
1
2
3
4
5
6
|
import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.testbeans.TestBean; public class PropertyReader extends ConfigTestElement implements TestBean{ } |
# display name of the Configuration Element | |
displayName=Property File Reader | |
# We have only one field called - propFilePath | |
# For each field - define the displayName and short Description. | |
propFilePath.displayName=File Path | |
propFilePath.shortDescription=Absolute path of the property file to be read |
public class PropertyReader extends ConfigTestElement implements TestBean, TestStateListener { | |
private static final Logger log = LoggingManager.getLoggerForClass(); | |
private String propFilePath; | |
public PropertyReader() { | |
super(); | |
} | |
public void testEnded() { | |
// TODO Auto-generated method stub | |
} | |
public void testEnded(String arg0) { | |
// TODO Auto-generated method stub | |
} | |
public void testStarted() { | |
// TODO Auto-generated method stub | |
} | |
public void testStarted(String arg0) { | |
// TODO Auto-generated method stub | |
} | |
/** | |
* @return the file path | |
*/ | |
public String getPropFilePath() { | |
return this.propFilePath; | |
} | |
/** | |
* @param propFilePath the file path to read | |
*/ | |
public void setPropFilePath(String propFilePath) { | |
this.propFilePath = propFilePath; | |
} | |
} |
public class PropertyReader extends ConfigTestElement implements TestBean, TestStateListener { | |
private static final Logger log = LoggingManager.getLoggerForClass(); | |
private String propFilePath; | |
public PropertyReader() { | |
super(); | |
} | |
public void testEnded() { | |
// TODO Auto-generated method stub | |
} | |
public void testEnded(String arg0) { | |
// TODO Auto-generated method stub | |
} | |
public void testStarted() { | |
if (StringUtils.isNotEmpty(getPropFilePath())) { | |
try { | |
Path path = Paths.get(getPropFilePath()); | |
if (!path.isAbsolute()) | |
path = Paths.get(FileServer.getFileServer().getBaseDir(), path.toString()); | |
JMeterUtils.getJMeterProperties().load(new FileInputStream(path.toString())); | |
log.info("Property file reader - loading the properties from " + path); | |
} catch (FileNotFoundException e) { | |
log.error(e.getMessage()); | |
} catch (IOException e) { | |
log.error(e.getMessage()); | |
} | |
} | |
} | |
public void testStarted(String arg0) { | |
testStarted(); | |
} | |
/** | |
* @return the file path | |
*/ | |
public String getPropFilePath() { | |
return this.propFilePath; | |
} | |
/** | |
* @param propFilePath the file path to read | |
*/ | |
public void setPropFilePath(String propFilePath) { | |
this.propFilePath = propFilePath; | |
} | |
} |
Create a custom config element which uploads the JMeter result (.jtl) file to Amazon S3 bucket once the test finishes.
Hint:
Our Property File Reader works as expected. How can we use this PropertyFileReader efficiently!?
I had already explained this in this article for the proper use of PropertyFileReader. I would request you to read the article and provide your feedback!
Extending JMeter – Creating Custom Config Element – Property File Reader
原文:https://www.cnblogs.com/a00ium/p/10381261.html