Reputation: 95
How can I generate random emails using Selenium with Java?
I was looking here in StackOverflow but I haven't found the answer to this.
I have tried with this, but it didn't help.
public class registerClass{
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = " ";
driver.get(baseUrl);
driver.manage().window().maximize();
driver.findElement(By.id("cboxClose")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div[1]/div[2]/a[1]")).click();
driver.findElement(By.id("register.firstName")).sendKeys("Karla");
driver.findElement(By.id("register.paternalLastName")).sendKeys("Perez");
driver.findElement(By.id("register.maternalLastName")).sendKeys("castro");
driver.findElement(By.id("register.email")).sendKeys("[email protected]");
//driver.close();
}
}
Upvotes: 2
Views: 48274
Reputation: 817
You need random string generator. This answer I stole from here.
protected String getSaltString() {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 10) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
return salt.toString();
}
Call it as getSaltString()+"@gmail.com"
in you code
Upvotes: 14
Reputation: 1868
You can create a method for generating the unique id
public static String getUniqueId() {
return String.format("%s_%s", UUID.randomUUID().toString().substring(0, 5), System.currentTimeMillis() / 1000);
}
And then use this method with the hostname which you need
public static String generateRandomEmail() {
return String.format("%s@%s", getUniqueId(), "yourHostName.com");
}
Another solution:
Add dependency for javafaker.Faker
https://github.com/DiUS/java-faker
import com.github.javafaker.Faker;
public static String randomEmail() {
Faker faker = new Faker();
return faker.internet().emailAddress();
}
Upvotes: 6
Reputation: 116412
Here's a way to do it in Kotlin:
object EmailGenerator {
private const val ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyz" + "1234567890" + "_-."
@Suppress("SpellCheckingInspection")
fun generateRandomEmail(@IntRange(from = 1) localEmailLength: Int, host: String = "gmail.com"): String {
val firstLetter = RandomStringUtils.random(1, 'a'.toInt(), 'z'.toInt(), false, false)
val temp = if (localEmailLength == 1) "" else RandomStringUtils.random(localEmailLength - 1, ALLOWED_CHARS)
return "$firstLetter$temp@$host"
}
}
Gradle file :
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation 'org.apache.commons:commons-lang3:3.7'
Upvotes: 0
Reputation: 990
This is my solution for the random email generator.
//randomestring() will return string of 8 chars
import org.apache.commons.lang3.RandomStringUtils;
public String randomestring()
{
String generatedstring=RandomStringUtils.randomAlphabetic(8);
return(generatedstring);
}
//Usage
String email=randomestring()+"@gmail.com";
//For Random Number generation
////randomeNum() will return string of 4 digits
public static String randomeNum() {
String generatedString2 = RandomStringUtils.randomNumeric(4);
return (generatedString2);
}
Upvotes: 1
Reputation: 588
If you don't mind adding a library, Generex is great for test data. https://github.com/mifmif/Generex
Add this to your pom.xml if you are using maven, otherwise check the link above for other options.
<dependency>
<groupId>com.github.mifmif</groupId>
<artifactId>generex</artifactId>
<version>1.0.2</version>
</dependency>
Then:
// we have to escape @ for some reason, otherwise we get StackOverflowError
String regex = "\\w{10}\\@gmail\\.com"
driver.findElement(By.id("emailAddressInput"))
.sendText(new Generex(regex).random());
It uses a regular expression to specify the format for the random generation. The regex above is generate 10 random word characters, append @gmail.com. If you want a longer username, change the number 10.
If you want to generate a random mobile number for say, Zimbabwe (where I live):
String regex = "2637(1|3|7|8)\\d{7}";
This library has saved me so many hours.
Upvotes: 0
Reputation: 12838
You can also use MockNeat. A simple example the library would be:
String email = mock.emails().val();
// Possible Output: [email protected]
Or if you want to generate emails from specific domains:
String corpEmail = mock.emails().domain("startup.io").val();
// Possible Output: [email protected]
Upvotes: 1
Reputation: 914
Try this method
/**
* @author mbn
* @Date 05/10/2018
* @Purpose This method will generate a random integer
* @param length --> the length of the random emails we want to generate
* @return method will return a random email String
*/
public static String generateRandomEmail(int length) {
log.info("Generating a Random email String");
String allowedChars = "abcdefghijklmnopqrstuvwxyz" + "1234567890" + "_-.";
String email = "";
String temp = RandomStringUtils.random(length, allowedChars);
email = temp.substring(0, temp.length() - 9) + "@testdata.com";
return email;
}
Upvotes: 1