Can you please help me?? 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. Ive tried with this but it didnt help
Can you please help me?? 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. Ive tried with this but it didnt help
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));
}
String saltStr = salt.toString();
return saltStr;
}
Call it as getSaltString()+"@gmail.com" in you code
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;
}
You can also use MockNeat. A simple example the library would be:
String email = mock.emails().val();
// Possible Output: icedvida@yahoo.com
Or if you want to generate emails from specific domains:
String corpEmail = mock.emails().domain("startup.io").val();
// Possible Output: tiptoplunge@startup.io
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);
}
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 hostname which you need
public static String generateMailinatorEmail() {
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();
}
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.
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'