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.
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
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:
ReplyDeleteBased 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
hi Andrzej , could you please give an example for how to pass these variables to Webdriver Sampler ?
ReplyDelete