0% found this document useful (0 votes)
24 views4 pages

Java Faker in Selenium 1722067816

Java faker classes

Uploaded by

8p66m44qmn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Java Faker in Selenium 1722067816

Java faker classes

Uploaded by

8p66m44qmn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

https://www.linkedin.

com/in/gayatri-mishra-freelancecoach/

Scenario -1 :

Provide the test data to the script using Faker.

Steps:

a. Launch rediffmail
b. Enter UserName
c. Enter Password

Note: Take the input using Faker - Java

Sample Code :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import com.github.javafaker.Faker;

public class LoginUsing_FakeData {

public static void main(String[] args) {


// TODO Auto-generated method stub
// Create a new instance of the ChromeDriver
WebDriver driver = new ChromeDriver();

// Navigate to the login page


driver.get("https://mail.rediff.com/cgi-bin/login.cgi");

// Use Java Faker to generate random data


Faker faker = new Faker();

// Generate random username and password


String unm = faker.name().username();
String pwd = faker.internet().password();

// Find the username and password input fields and enter the generated
// credentials
WebElement userid = driver.findElement(By.name("login"));
WebElement passcode = driver.findElement(By.name("passwd"));

userid.sendKeys(unm);
passcode.sendKeys(pwd);

driver.quit();
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

}
}

Parameterization using Java Faker:

Scenario -2 :

Provide the test data to the script using Faker.

Steps:

a. Launch rediffmail
b. Enter UserName
c. Enter Password
d. Clear and reenter with 3 different set of test data using Faker library

package co.content.linkedin.co.content.linkedin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import com.github.javafaker.Faker;

public class Login_Multiuser_faker {

// number of iterations
static int niter = 3;

public static void main(String[] args) throws InterruptedException {


// Create a new instance of the ChromeDriver
WebDriver driver = new ChromeDriver();

// Navigate to the login page


driver.get("https://mail.rediff.com/cgi-bin/login.cgi");

// Test login for multiple users


for (int i = 0; i < niter; i++) {
// Generate random credentials for each user
String username = generateRandomUsername();
String password = generateRandomPassword();

// Perform login with the current set of credentials


performLogin(driver, username, password);
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

// Method to generate a random username


private static String generateRandomUsername() {
return new Faker().name().username();
}

// Method to generate a random password


private static String generateRandomPassword() {
return new Faker().internet().password();
}

// Method to perform login with provided credentials


private static void performLogin(WebDriver driver, String username, String
password) throws InterruptedException {
// Find the username and password input fields and enter the provided
// credentials
WebElement userInput;
WebElement passwordInput;
userInput = driver.findElement(By.name("login"));
passwordInput = driver.findElement(By.name("passwd"));
userInput.sendKeys(username);
passwordInput.sendKeys(password);
userInput.clear();
passwordInput.clear();
Thread.sleep(2000);
}
}

Dependencies used in pom.xml:

<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version> <!-- Check for the latest version on Maven Central -->
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

<version>4.16.1</version>
</dependency>

You might also like