如何创建一个随机的字母表字符串并获取文本

时间:2016-08-04 12:29:18

标签: java selenium-webdriver

我在selenium webDriver中搜索生成字符串字符串并生成文本以断言它的方法。 我正在尝试创建一个随机名称的客户。但是,我必须搜索新创建的客户并单击文本以输入更多信息。

3 个答案:

答案 0 :(得分:5)

您可以使用apache RandomStringUtils api中的commonlang

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

答案 1 :(得分:2)

您可以生成随机字母数字: -

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

答案 2 :(得分:-3)

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