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.
Here structure as you might begin, and make sure you can leave the block
while
, and not keep it in an infinite loop.You can get a random element of an array or List using
Random
Or for a List
You can try
Random
orMath.random
.The following is a simple example:
or
I think what you are asking for here is just basically picking a random element for 4 elements. One way of doing this would be adding those elements to an array.
Then you can get a random number in Java with
Here you can replace max and min with ur minimum number of elements and maximum number of elements. In this case its 0 to 3.
After getting this number, you can use it to index into the array of the elements.
Hope this answer helps.