Skip to main content

How to generate data using Faker Library and Beanshell in JMeter

Many times you face the situation when you need the flexibility to generate data in JMeter, sometimes the data might be complex, as in the case when you want to generate phone numbers in specific format, or ID numbers in specific format.
To use the power of Java along with the simplicity of JMeter, fortunately, JMeter has the wonderful feature of Beanshell where you can call Java libraries.
In the below example i will demonstrate how you can do that.


  • Download the jar from https://mvnrepository.com/artifact/com.github.javafaker/javafaker
  • Add the jar in your JMeter lib folder.
  • Download the YML library from https://mvnrepository.com/artifact/org.yaml/snakeyaml
  • Add the jar in your JMeter lib folder.



















  • Restart JMeter
  • Add a Beanshell Sampler.


  • Write the follwoing code:
  • //In the code below I am getting a random Phone Number 
  • import com.github.javafaker.Faker;
  • Faker fakeData = new Faker();
  • String phoneNo = fakeData.phoneNumber().phoneNumber();
  • print(phoneNo);



  • Output will be printed to JMeter console.


  • You can assign the phoneNo to any variable and use it in any subsequent requests.
  • Download the sample from  https://github.com/agnasser56/JMeterDataGeneration.git

Comments

  1. It's worth mentioning that when one is trying to use faker methods such as 'regexify' (guessing others like bothify/numerify too) or any other that use this underneath, such as zipcodes for en uk locale - there's more dependencies needed:

    Based on dependencies from java faker 1.0.2

    com.mifmif.common.regex
    https://mvnrepository.com/artifact/com.github.mifmif/generex/1.0.2

    dk.brics.automaton
    https://mvnrepository.com/artifact/dk.brics.automaton/automaton/1.11-8

    Errors thrown in jsr223 scripts or beanshell aren't really specific, if some other method faker method just fails without a clear reason its likely that other dependencies are needed

    This comes up in google as first - maybe the next guy saves a few hours of his life

    Cheers,
    Andrzej

    ReplyDelete
  2. hi Andrzej , could you please give an example for how to pass these variables to Webdriver Sampler ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Second video in the advanced JMeter topics, Many of us use JMeter for performance testing, while the tool offers plenty of wonderful resources and plugins, a point of order is required when the project grows large, you need a framework to organize things and to ease the maintenance mission, check this youtube video https://www.youtube.com/watch?v=8kLWRdjnopw This project describes how to create a reusable JMeter script that takes into consideration the following aspects: 1- Re-usability 2- Maintainability 3- Extension We have used the following components to achieve our target: 1- Simple controller 2- Transaction controller 3- Module controller 4- Test fragment You can find the code on Github https://github.com/agnasser56/JMeterA...
GEAR UP FOLKS, Selenium 4 is here!!! What's new in Selenium 4: Selenium 4 is fully W3C compliant: This means, in simple terms, that there is no need to install drivers for different types of browsers anymore. To understand how this works, lets go back a little bit in time to the days of Selenium RC where the interaction with web browsers was done through injecting Javascript functions in the browser and use the injection javascript functions to drive the AUT.  What happened afterwards starting from Selenium 2 was a bit different. Each browser provided its own APIs that supports the automation. Hence, you will find Chrome browser, Geko driver for firefox and similar drivers for other types of browsers. The new thing in Selenium 4 is that it is fully compliant with W3C which means that no need to install any external web drivers, CHEERS!! Selenium IDE is back.  After deprecating Selenium IDE somewhere back in August 2018, it is coming again in Selenium 4 with the en...