Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.     public void randomDopdownChoice() {
  2.         // Initialization of Webdriver driver is outside of this method
  3.  
  4.         // Instantiate random number generator
  5.         Random random = new Random();
  6.         // This is needed just for waits
  7.         Actions actions = new Actions(driver);
  8.  
  9.         // Page for example
  10.         driver.get("http://demo.guru99.com/test/newtours/reservation.php");
  11.  
  12.         // Instantiate element to manipulate select
  13.         WebElement selectElement = (new WebDriverWait(driver, 10))
  14.                 .until(ExpectedConditions.presenceOfElementLocated(By.name("fromPort")));
  15.         Select select = new Select(selectElement);
  16.  
  17.         // Find all select options and put them into list, then get list size
  18.         List<WebElement> selectOptions = driver.findElements(By.cssSelector("select[name='fromPort'] option"));
  19.         int number_of_options = selectOptions.size();
  20.  
  21.         // For demo purposes I select five random elements with 2 second pauses
  22.         for (int j=0;j < 5; j++) {
  23.             actions.pause(Duration.ofSeconds(2)).perform();
  24.             select.selectByIndex(random.nextInt(number_of_options));
  25.         }
  26.  
  27.         // Final pause
  28.         actions.pause(Duration.ofSeconds(2)).perform();
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement