Selenium WebDriver – How To Generate Test Data Using JFairy

Overview:

One of the challenges in software testing is to come up with test data to more or less look like real data. Often test automation engineers overlook the importance of test data in their test script. For example, They tend to write scripts like entering firstname1, lastname1 etc for the first name and last name fields. Even though this is OK for debugging purposes, You might miss some potential defects when you run the automation test scripts against the actual application under test. So, I would suggest you to use test data generation library to produce data at random which looks like real data.

Lets look at a Java library – JFairy – which could generate test data for our selenium test automation scripts.

JFairy:

It is a very simple library to produce test data at random which looks like real data. More info is here.

Include below maven dependency in your pom file.

<dependency>
    <groupId>io.codearte.jfairy</groupId>
    <artifactId>jfairy</artifactId>
    <version>0.5.9</version>
</dependency>

Examples:

  • To generate random person objects which could give us name, address, telephone number, age, DOB, email, passport number etc
//first create Fairy object. By default - Locale is English
Fairy fairy = Fairy.create();

//Create person object
Person person = fairy.person();

person.getFullName();
person.getAddress();
person.getAge();
person.getEmail();
person.getDateOfBirth();
person.getPassportNumber();
person.getCompanyEmail();
person.getNationalIdentityCardNumber();

When I tried to run the above code in a loop 3 times, It produces unique data every time which looks like real data.

Christopher Robles
106 Stillwell Avenue APT 171
Washington 29693
97
robles@yahoo.com
1920-06-20T23:52:14.933-05:00
wFNmlgnox
christopher.robles@furbainc.biz
787-29-0099
------------------------------------------------------
Nathaniel Barnett
93 Tabor Court APT 249
New York 46780
97
barnett@gmail.com
1920-10-13T13:20:39.103-05:00
1tIaHKoze
nathaniel.barnett@nonos.eu
059-17-1981
------------------------------------------------------
Aubree Mcintyre
178 Aster Court
Miami 61999
91
aubreemcintyre@yahoo.com
1926-11-15T04:59:48.768-06:00
C8cDSoSHH
aubree.mcintyre@datastorecompany.biz
151-63-1547
------------------------------------------------------

Locale Support:

JFairy not only supports English names – but also supports few other locales.  For example, below fairy object produces the test data based on the locale setting.

Fairy frFairy = Fairy.create(Locale.forLanguageTag("fr"));
Fairy svFairy = Fairy.create(Locale.forLanguageTag("sv"));
Fairy plFairy = Fairy.create(Locale.forLanguageTag("pl"));

For example, running above code for Fr locale,

Hector Verdier
Place de l'Église, 41
15284 Anglet
52
hectorverdier@orange.fr
1965-08-29T19:02:27.909-05:00
DRYDOdR9Q
hector.verdier@olgroupeeurl.gouv.fr
66634487-E

Like Person object, Fairy can also create below objects & produce test data.

  • Person (See the example above)
  • Credit Card (CC Vendor, Expiry etc)
  • Network (IP address and random URLs)
  • Company (name, email, domain etc)
  • Date (Random dates in past, future, range)
  • Text (Random word, text, paragraph etc)
fairy.networkProducer().ipAddress();
fairy.textProducer().latinWord();
fairy.creditCard().getVendor();
fairy.company().getUrl();

produces below output.

12.205.88.19
pede consequat aliquet
Visa
http://www.quisuc.biz

Summary:

JFairy is a simple and small library. These above objects will be fine for most of our use cases to produce test data for your test automation scripts.

 

Happy Testing & Subscribe 🙂

 

Share This:

1 thought on “Selenium WebDriver – How To Generate Test Data Using JFairy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.