java 生成随机电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45841500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:56:17  来源:igfitidea点击:

Generate random emails

javaselenium

提问by karlaA

Can you please help me?? How can I generate random emails using Selenium with Java? ?

你能帮我么??如何使用 Selenium 和 Java 生成随机电子邮件??

I was looking here in stackoverflow but I haven't found the answer to this. Ive tried with this but it didnt help

我在 stackoverflow 中查看这里,但我还没有找到答案。我试过这个,但没有帮助

回答by a_a

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

getSaltString()+"@gmail.com"在你的代码中调用它

回答by Andrei Ciobanu

You can also use MockNeat. A simple example the library would be:

您也可以使用MockNeat。图书馆的一个简单例子是:

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]

回答by Atul Sharma

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

回答by android developer

Here's a way to do it in Kotlin:

这是在 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'

回答by Gavin S

If you don't mind adding a library, Generex is great for test data. https://github.com/mifmif/Generex

如果您不介意添加库,Geneex 非常适合测试数据。 https://github.com/mifmif/Generex

Add this to your pom.xml if you are using maven, otherwise check the link above for other options.

如果您使用的是 maven,请将其添加到您的 pom.xml 中,否则请查看上面的链接以获取其他选项。

    <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.

它使用正则表达式来指定随机生成的格式。上面的正则表达式是生成 10 个随机单词字符,附加 @gmail.com。如果您想要更长的用户名,请更改数字 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.

这个图书馆为我节省了很多时间。

回答by mbn217

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