java - 难忘信息的写作测试 - 随机字符(从 10 个中选择 3 个)

标签 java selenium selenium-webdriver

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

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


Character 1 :
Character 2 :
Character 8 :

下面是标签的代码:

<label for="frmentermemorableinformation1:strEnterMemorableInformation_memInfo1">Character 1 </label>

使用selenium WebDriver JAVA,如何实现?

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

非常感谢。

最佳答案

也许您无法控制此页面,但通常网站使用密码而不是这种......奇怪的方式来验证安全性。如果您可以输入整个密码,则无需识别整个“易记单词”中的单个字符。无论如何...

我会做类似下面的事情。您知道令人难忘的单词,并且可以从“字符 1”、“字符 2”和“字符 8”等标签中找到索引。在本例中为 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]);
    }
}

关于java - 难忘信息的写作测试 - 随机字符(从 10 个中选择 3 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35289242/

相关文章:

selenium - 通过 saucelabs 运行 testcafe 脚本时获取 "Failed to start Selenium listener."错误消息

html - 如何使用 VBA 从 Chrome 的下拉列表中选择一个选项?

python - 如何使用 webDriver(python)最大化 chrome 中的窗口

javascript - Key.chord 还是 ActionSequence?

java - AWS Java SDK - 使用区域方法时 Jackson 中的 NoSuchMethodError

java - 在 Spring Boot 中从 Native SQL 转换为 JPA 以返回 List

python - Selenium 等待元素

java - Selenium id=未找到电子邮件

java - 为什么当我将国际象棋项目作为可运行的 jar 文件运行时,我会得到一个奇怪的行为,而当我在 eclipse 中运行它时,一切都工作正常?

java - Hotmail SMTP 无法与 javamail 一起使用