简体   繁体   中英

Writing test for memorable information - random characters (selection off 3 from 10)

I'm trying to write a script to login to an application that requires authentication via a memorable word, however the word is 10 digits long and 3 characters are selected at random as per the following:

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


Character 1 :
Character 2 :
Character 8 :

Below is code for the label:

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

Using selenium WebDriver JAVA, how could this be achieved?

So,need a IF label = 1 then select A from the dropdown. I know the password, just not what characters will be asked on page load, as these will change each time, so need logic added.

Thanks a lot.

Maybe you don't have control over this page but generally sites use a password instead of this ... odd way of verifying security. If you can enter the entire password, there's no need to identify individual characters from the entire "memorable word." Anyway...

I would do something like the below. You know the memorable word and you can find the indices from the labels like "Character 1", "Character 2", and "Character 8".. which in this case are 1, 2, 8. Now you get characters 1, 2, and 8 from the memorable word and place them into the relevant dropdowns.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM