为难忘信息编写测试 - 随机字符(从10中选择3)

时间:2016-02-09 10:11:30

标签: java selenium selenium-webdriver

我正在尝试编写一个脚本来登录需要通过令人难忘的单词进行身份验证的应用程序,但是该单词长度为10位,随机选择3个字符,如下所示:

touchstart

以下是标签的代码:

Please enter characters 1, 2 and 8 from your memorable information then click on the continue button.


Character 1 :
Character 2 :
Character 8 :

使用selenium WebDriver JAVA,如何实现?

因此,需要IF标签= 1然后从下拉列表中选择A.我知道密码,不知道页面加载时会询问哪些字符,因为每次都会改变,所以需要添加逻辑。

非常感谢。

1 个答案:

答案 0 :(得分:1)

也许您无法控制此页面,但通常网站使用密码而不是此...验证安全性的奇怪方法。如果您可以输入完整的密码,则无需从整个"令人难忘的单词中识别单个字符。"总之...

我会做类似下面的事情。你知道这个令人难忘的单词,你可以找到标签中的索引,如"字符1","字符2"和"字符8" ..在此case是1,2,8。现在你从令人难忘的单词中得到字符1,2和8,并将它们放入相关的下拉列表中。

public class Sample
{
    public static void main(String[] args)
    {
        WebDriver driver = new FirefoxDriver();
        String memorableWord = "memorable1";

        // set up the locators for the labels that contain the information on which characters will be needed from the memorable word
        By char1 = By.cssSelector("label[for='frmentermemorableinformation1:strEnterMemorableInformation_memInfo1']");
        By char2 = By.cssSelector("label[for='frmentermemorableinformation2:strEnterMemorableInformation_memInfo2']");
        By char3 = By.cssSelector("label[for='frmentermemorableinformation3:strEnterMemorableInformation_memInfo3']");

        // get the indices from the labels
        int index1 = getNumber(driver.findElement(char1).getText().trim());
        int index2 = getNumber(driver.findElement(char2).getText().trim());
        int index3 = getNumber(driver.findElement(char3).getText().trim());

        // from your description, it sounds like the letters are being chosen from a SELECT so I used that in the code below
        // I don't know how to find the SELECT since that HTML was not provided so I've used sample locators below
        // basically it grabs the SELECT and then chooses the letter based on it's position in the memorable word
        new Select(driver.findElement(By.id("test1"))).selectByVisibleText(memorableWord.substring(index1, index1 + 1));
        new Select(driver.findElement(By.id("test2"))).selectByVisibleText(memorableWord.substring(index2, index2 + 1));
        new Select(driver.findElement(By.id("test3"))).selectByVisibleText(memorableWord.substring(index3, index3 + 1));
    }

    public static int getNumber(String label)
    {
        // takes "Character 1", splits it into two parts, takes the second part (the number) and converts it to an int and returns it
        return Integer.valueOf(label.split(" ")[1]);
    }
}