java 如何创建随机字母串并获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38767387/
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
how to create a random string of alphabets and get text
提问by Raj
I am searching for methods to generate a string of alphabets and generate text to assert it, in selenium webDriver. i am trying to create a customer with random name. however, I have to search for the newly created customer and click on the text to enter further info.
我正在寻找在 selenium webDriver 中生成一串字母并生成文本来断言它的方法。我正在尝试创建一个具有随机名称的客户。但是,我必须搜索新创建的客户并单击文本以输入更多信息。
采纳答案by Saurabh Gaur
You can generate random alphanumeric number as :-
您可以生成随机字母数字为:-
import java.util.UUID;
String uuid = UUID.randomUUID().toString();
//Now this uuid enter to your text box
driver.findElement(By.id("text box id")).sendKeys(uuid);
回答by noscreenname
You can use RandomStringUtilsfrom apache commonlangapi.
您可以使用RandomStringUtilsApache的commonlangAPI。
// random string of length 8 composed of alphabetic characters
String s = RandomStringUtils.randomAlphabetic(8);
// random string of length 8 composed of alphabetic characters and numbers
String s = RandomStringUtils.randomAlphanumeric(8);
// random string of length 8 composed only of lettes a, b, and c
String alphabet = "abc";
String s = RandomStringUtils.random(8, alphabet);
回答by Nani
public static String randomString(int intValue) throws Exception {
char c[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int randomPosition;
String randomString = "";
for (int i = 0; i < intValue; i++) {
randomPosition = generateRandomIntIntRange(0, 51);
randomString = randomString + c[randomPosition];
}
System.out.println(randomString);
return randomString;
}