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...
Today I was faced by an odd behavior that is: when opening the page in chrome it was displayed in a larger resolution than the normal one which led to some tabs in the page being invisible. I figured out a solution to this weird issue by doing a java script hack as follows: Create a Javascript executer. WebDriver driver = new ChromeDriver (); JavascriptExecutor jsExecuter; = ( JavascriptExecutor )  driver ;     Execute this java script command against your web driver  js . executeScript ( " document.body.style.zoom='90%'" ); //Do the required actions in the tabs After all is done, don't forget to reset the browser to its default settings. js . executeScript ( " document.body.style.zoom='100%'" );