NewIncredible offer for our exclusive subscribers!Read More
December 22, 2024
Tech Uncategorized

How to generate realistic and random data for testing in selenium Java automation ,By using faker library.

  • September 2, 2024
  • 3 min read
  • 316 Views
How to generate realistic and random data for testing in selenium Java  automation ,By using faker library.

Import faker library through maven repository.

 Maven – Maven is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency, and documentation. 

  • Visit https://mvnrepository.com/   
  • Search  Java faker dependency.

Dependency – Maven dependencies are used to manage project dependencies in a Maven-based Java project. They are specified in the pom.xml file of your project. 

  • Copy the java faker dependency.
  • <!– https://mvnrepository.com/artifact/com.github.javafaker/javafaker –>
    <dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>1.0.2</version>
    </dependency>
  • Add the above faker dependency into the pom.xml file of the project. 
  • Import faker package in your selenium IDE.  import com.github.javafaker.Faker;
  • Create the instance of the faker  Faker faker = new Faker();

package pages;

import com.github.javafaker.Faker;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FakerClass {

public static void main(String[] args) {

// TODO Auto-generated method stub
System.setProperty("Webdriver.chromedriver","C:\\Users\\pc\\Downloads\\chromedriver-win64 (1).zip\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://practice.expandtesting.com/inputs");
Faker faker = new Faker();
int fakeNumber = faker.number().numberBetween(1, 100);
String randomnumber = Integer.toString(fakeNumber);
String fakeText = faker.artist().name();
String Password = faker.internet().password();
Date randomFutureDate = faker.date().future(1825, TimeUnit.DAYS);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-DD-YYYY");
String Calendardate = dateFormat.format(randomFutureDate);

WebElement Number = driver.findElement(By.xpath("//input[@id='input-number']"));
Number.sendKeys(randomnumber);

WebElement Text = driver.findElement(By.xpath("//input[@id='input-text']"));
Text.sendKeys(fakeText);

WebElement Password1 = driver.findElement(By.xpath("//input[@id='input-password']"));
Password1.sendKeys(Password);

WebElement Calendar = driver.findElement(By.xpath("//input[@id='input-date']"));
Calendar.sendKeys(Calendardate);

WebElement Display = driver.findElement(By.xpath("//button[@id='btn-display-inputs']"));
Display.click();

}

}

//You can add more input fields as per your requirement.

//Compile and run the test as usual. The Faker library will generate realistic test data each time the test is executed.                            

Output:- 

Benefits of faker package in selenium

  • Improved Test Coverage: By using a variety of inputs, you can more effectively test how your application handles different kinds of data, including edge cases.
  • Quick Test Setup: Faker reduces the time spent on preparing test data. This is particularly beneficial when setting up tests that require a large amount of varied data, such as performance or load tests.
  • Real-World Testing: With realistic and varied data, you can simulate real-world scenarios more effectively. This is crucial for ensuring that your application will behave as expected when used by actual users.
About Author

pratyush

Leave a Reply

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