dimanche 23 août 2015

How i can get Random element on code in JAVA?

I would like to get a random element on my code. For example i have 4 elements on my code in JAVA. I trying to get second element and start run program on this place. Another time when I start my program would be a one element. How do i get this?

        first element :

    while ( true ) {

         driver.get( "example_of_page" );
            WebElement formEmail = driver.findElement(By.name("email"));
            formEmail.sendKeys(Email1);
            WebElement formPassword = driver.findElement(By.name("pass"));
        formPassword.sendKeys(Password2);
        WebElement formRemember = driver.findElement(By.name("persistent"));
        formRemember.sendKeys(" ");
        formPassword.submit();

        second element :


        driver.get("example_of_page");
        WebElement formEmail = driver.findElement(By.name("email"));
        formEmail.sendKeys(Email2);
        WebElement formPassword = driver.findElement(By.name("password"));
        formPassword.sendKeys(Password2);
        WebElement formRemember = driver.findElement(By.name("remember"));
        formRemember.sendKeys(" ");
        WebElement formSubmit = driver.findElement(By.name("login_button"));
        formSubmit.click();

    the third element :

     driver.get("example_of_page");
        WebElement formEmail = driver.findElement(By.name("email"));
        formEmail.sendKeys(Email3);
        WebElement formPassword = driver.findElement(By.name("password"));
        formPassword.sendKeys(Password3);
        WebElement formRemember = driver.findElement(By.name("remember"));
        formRemember.sendKeys(" ");
        WebElement formSubmit = driver.findElement(By.name("login_button"));
        formSubmit.click();

    the fourth  element :

      driver.get( "example_of_page" );
            WebElement formEmail = driver.findElement(By.name("email"));
            formEmail.sendKeys(Email4);
            WebElement formPassword = driver.findElement(By.name("pass"));
        formPassword.sendKeys(Password4);
        WebElement formRemember = driver.findElement(By.name("persistent"));
        formRemember.sendKeys(" ");
        formPassword.submit();

}

How i can named, define this element in loop to run random element and start code from this place ? I use loop on this function, element and this element must not repeated on one loop.




Aucun commentaire:

Enregistrer un commentaire