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.

Goal:

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

Reference:

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.

Setting Up IDE:

Lets first set up our IDE with all the dependencies.

  • Create a simple Maven project
  • Add below dependency for creating a custom function – add other dependencies as you need.

Creating Custom Config Element:

  • Lets create a simple java class to read the property file.
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{
 
}
  • TestBean is a marker interface to tell JMeter to make a Test Bean Gui for the class.
  • We need to read the property file before the test plan gets executed. So we need to implement the corresponding interface – TestStateListener.
  • Before implementing the actual logic in the above class, lets create the GUI class for our plugin.
  • Name of this GUI class should be [ComponentName]BeanInfo.java  in the same package.
  • We are going to have only one field in the GUI – File Path – which should contain actual file path of the Property file to be read.
  • File Path – is basically a display name in the GUI.
  • By default – the field should be blank if it is not set already.
  • There should be a property file in the same package which contains the display name, short description etc
  • Name of this property file should be [ComponentName]Resources.properties
  # 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
  • At this point, the package will look like this.

  • Now, lets get back to our PropertyReader.java to implement the actual logic to read the property file.
  • Lets add the public getter and setter for each field in the GUI.
  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;
  }
  }
view rawPropertyReader.java hosted with ❤ by GitHub
  • Now – it is time for us to add the logic to read the property file.
  • We need to read the file before test starts.
  • So, Add the below logic to read the file in the ‘testStarted’ method.
  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;
  }
   
  }
view rawPropertyReader.java hosted with ❤ by GitHub

Export:

  • Now export this package as a jar file or mvn clean package command will create the jar file.
  • Place the jar in JMETER_HOME/lib/ext folder
  • Restart JMeter
  • You should be able to see the custom plugin we had created – Property File Reader – under Test Plan -> Config Element
  • Add the config element into the test plan & It will look as shown below.

Testing our Plugin:

  • Add the config element we had created. Set the property file path.

  • Add a BeanShell Sampler to print the property values.

  • Run the test – check the log

  • Our property file was read only once for each test run
  • It prints the values of the property.

Exercise:

Create a custom config element which uploads the JMeter result (.jtl) file to Amazon S3 bucket once the test finishes.

Hint:

  • Create a simple java utility class to upload a given file to Amazon S3. You can find some examples here.
  • Now follow the same approach I had explained here to the read property file.
  • Add the logic in the ‘testEnded’ method to call the utility to upload the file in Amazon s3.

Summary:

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的更多相关文章

  1. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  2. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  3. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

  4. ASP.NET MVC- VIEW Creating Custom HTML Helpers Part 2

    The goal of this tutorial is to demonstrate how you can create custom HTML Helpers     that you can ...

  5. log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [File] to set object on [TF.Log.FileAppender]

    难受,香菇. 大概研究了两个多小时,搜了很多资料都没有很完美的答案,最后突然脑子就一闪一闪,才弄明白咋回事. log4net:ERROR XmlHierarchyConfigurator: Canno ...

  6. Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer

    在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...

  7. CocoaPods did not set the base configuration of your project because your project already has a custom config set.

    今天在封装自己的消息推送SDK的时候,pod install 的时候,突然报这个错误,解决方式如下: $ pod install Analyzing dependencies Downloading ...

  8. Drag & Drop and File Reader

    参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...

  9. Creating Custom Connector Sending Claims with SharePoint 2013

    from:http://blogs.msdn.com/b/security_trimming_in_sharepoint_2013/archive/2012/10/29/creating-custom ...

随机推荐

  1. E比昨天更多的棒棒糖(Easy+Hrad)(华师网络赛)(DP||母函数||背包优化)

    Time limit per test: 2.0 seconds Memory limit: 512 megabytes 唐纳德先生的某女性朋友最近与唐纳德先生同居.该女性朋友携带一 baby.该 b ...

  2. SPOJ8093Sevenk Love Oimaster(广义后缀自动机)

    Oimaster and sevenk love each other.     But recently,sevenk heard that a girl named ChuYuXun was da ...

  3. Sentry的要点

    1.Apache的Build 在研究Sentry的时候,发现没有bin.jar,只能手工编辑工程,但是编辑发现很多jar包有问题:在访问官网的时候发现左侧菜单中有一项是builds,点开后(https ...

  4. 2.2synchronized同步语句块

    使用synchronized虽然能够避免不同步的现象出现,但是也会出现弊端,比如代码执行时间过长,那么其他线程就必须等待该线程执行完毕释放锁之后才能拿到锁. 面对这种问题可以使用同步代码块来解决. 2 ...

  5. python为类定义构造函数

    用python进行OO编程时, 经常会用到类的构造函数来初始化一些变量. class FileData:     def __init__(self, data, name, type):       ...

  6. poj 1517 u Calculate e(精度控制+水题)

    一.Description A simple mathematical formula for e is e=Σ0<=i<=n1/i! where n is allowed to go t ...

  7. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  8. Python图片识别——人工智能篇

     一.安装pytesseract和PIL PIL全称:Python Imaging Library,python图像处理库,这个库支持多种文件格式,并提供了强大的图像处理和图形处理能力. 由于PIL仅 ...

  9. 批量创建10个系统帐号tianda01-tianda10并设置密码

    #.添加用户 useradd tianda01 #.非交互式给密码 echo "pass"|passwd --stdin tianda #.- 加0思路 ()..} () #随机密 ...

  10. arm-linux-3.4.2移植for2440

    ----------------------------2440 上内核3.4.2移植------------------------ PS:因wifi项目中无wifi驱动,需新内核. 1.首先在内核 ...