Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void randomDopdownChoice() {
- // Initialization of Webdriver driver is outside of this method
- // Instantiate random number generator
- Random random = new Random();
- // This is needed just for waits
- Actions actions = new Actions(driver);
- // Page for example
- driver.get("http://demo.guru99.com/test/newtours/reservation.php");
- // Instantiate element to manipulate select
- WebElement selectElement = (new WebDriverWait(driver, 10))
- .until(ExpectedConditions.presenceOfElementLocated(By.name("fromPort")));
- Select select = new Select(selectElement);
- // Find all select options and put them into list, then get list size
- List<WebElement> selectOptions = driver.findElements(By.cssSelector("select[name='fromPort'] option"));
- int number_of_options = selectOptions.size();
- // For demo purposes I select five random elements with 2 second pauses
- for (int j=0;j < 5; j++) {
- actions.pause(Duration.ofSeconds(2)).perform();
- select.selectByIndex(random.nextInt(number_of_options));
- }
- // Final pause
- actions.pause(Duration.ofSeconds(2)).perform();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement