0

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

karlaA
  • 75
  • 1
  • 1
  • 3

7 Answers7

12

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

a_a
  • 688
  • 1
  • 9
  • 15
1

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;
}
mbn217
  • 802
  • 6
  • 13
1

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
Andrei Ciobanu
  • 12,164
  • 22
  • 80
  • 116
1

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);
     }
1

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();
    }
Norayr Sargsyan
  • 1,386
  • 1
  • 9
  • 23
0

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.

paulo.bing
  • 80
  • 9
Gavin S
  • 569
  • 9
  • 20
  • 1
    Executing your email regex as an example (which lacks a semi-colon at the end of first line btw), it causes a StackOverflowError. I was able to solve it by adding a \\ (double backslash) before the @ – paulo.bing Feb 08 '19 at 12:12
0

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'
android developer
  • 112,189
  • 137
  • 688
  • 1,196