Extending JMeter – Creating Custom Config Element – Property File Reader

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

view rawPropertyReaderResources.properties hosted with ? by GitHub

  • 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!

原文地址:https://www.cnblogs.com/a00ium/p/10381261.html

时间: 2024-10-10 13:11:56

Extending JMeter – Creating Custom Config Element – Property File Reader的相关文章

【IOS笔记】Creating Custom Content View Controllers

Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of your app. You use them to present your app’s unique content. All apps need at least one custom content view controller. Complex apps divide the workl

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 properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, references to other beans in th

wxWidgets 2.8.x & 3.0.x project property file (.props in MSVC++)

the following files are the wxWidgets 2.8.x project property file: <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Impo

Drag &amp; Drop and File Reader

参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/details/25295899 http://www.tutorialspoint.com/html5/html5_drag_drop.htm 本篇只作为个人参考 FIle Reader method abort()  停止读 readAsText(file,"utf-8") , 第2参数是指

C# - CSV file reader

// -------------------------------------------------------------------------------------------------------------------- // <summary> // Defines the CSVFileReader type. // </summary> // ----------------------------------------------------------

管理后台-第一部分:Creating custom sections in Umbraco 7(翻译文档)

在Umbraco上每个部分都可以被称为一个应用程序,所以这些部分和应用程序基本上是一样的.我们首先要做的事情是需要创建应用程序.在这个例子中,我不会去摆弄xml文件或是数据库——我将使用类来创建我的内容. 我需要做的第一件事就是创建一个类,实现Application接口,这样我们的Umbraco将会初始化这个类. [Application("CustomSection", "CustomSection","icon-car", 15)] publ

Creating Custom Helper Methods 创建自定义辅助器方法----辅助器方法 ------ 精通ASP.NET MVC 5

创建内联的辅助器方法 和 拓展方法 好像类似的功能. 不过写在前台更直观

JMeter - 如何在多个测试环境中运行多个线程组

概述: 作为性能测试的一部分,我不得不为我们的应用程序提供各种用例/业务工作流程的性能测试脚本.当我设计我的性能测试脚本时,我将确保我有本文中提到的可重用测试脚本. JMeter - 如何创建可重用和模块化测试脚本 如果您尚未阅读本文,我会请您先阅读本文,然后继续阅读本文!作为上述文章的一部分,您可以了解如何正确设计性能测试脚本/测试计划. 在高级别,我在"测试片段"下维护了一个可重复使用的"测试脚本"模块.然后我使用Module Controller调用特定模块来

&lt;邮件服务postfix+mysql&gt;MAIL第二篇

环境:本服务是建立在第一篇的基础之上的,最好搭建好第一篇 玩此服务的前提是你的系统装好了msql和postfix服务. Postfix+mysql主要是把邮件服务的发与mysql结合使用.当然mysql要是一直是在命令行下使用也不是很方便对吧,下面我们来看linux图形化下mysql的使用: 下面的软件包. phpMyAdmin-2.11.3-all-languages.tar.gz是以php的格式结合Apache通过网页的形式管理mysql. 既然是使用网页管理mysql,当然很定得将这个软件