DEV Community

Cover image for Selenium Java Dropdowns, Checkboxes, Calendar, Autocomplete
Anne Quinkenstein
Anne Quinkenstein

Posted on • Edited on

Selenium Java Dropdowns, Checkboxes, Calendar, Autocomplete

Table of Contents

  1. Dropdowns
  2. static Dropdown with Select
  3. Dropdown by ID
  4. autocomplete Option
  5. Checkboxes
  6. check is element is enabled, selected or displayed
  7. calendar

Dropdowns

static Dropdown with Select

WebElement staticDropdown = driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));

Select dropdown = new Select(staticDropdown);

dropdown.selectByIndex(3);        Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"USD");

dropdown.selectByVisibleText("AED");        Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"AED");

dropdown.selectByValue("INR");     Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"INR");
Enter fullscreen mode Exit fullscreen mode

Dropdown by ID

//find element to click to open Dropdown
driver.findElement(By.id("divpaxinfo")).click();
//add an wait
Thread.sleep(2000L);
// click 6 times on a button
for (int i = 1; i < 6; i++){
            driver.findElement(By.id("hrefIncAdt")).click();
        }
Assert.assertEquals(driver.findElement(By.id("spanAudlt")).getText(), "6"); 
driver.findElement(By.id("btnclosepaxoption")).click();
Enter fullscreen mode Exit fullscreen mode

autocomplete Option

clicking on a special text

// start input in input field
driver.findElement(By.id("autosuggest")).sendKeys("ind");
Thread.sleep(3000);
//get the list of suggestet inputs 
List <WebElement> options = driver.findElements(By.cssSelector("li.ui-menu-item a"));
//loop through list of inputs an click specific Text + break out the loop
for ( WebElement option : options ) {
  if (option.getText().equalsIgnoreCase("India")) {  
     option.click();
     break;
   }
}
Enter fullscreen mode Exit fullscreen mode

moving the arrows up and down

WebElement destination=driver.findElement(By.id("hp-widget__sTo"));
        destination.clear();
        destination.sendKeys("DEL");
        Thread.sleep(2000);
        destination.sendKeys(Keys.ARROW_DOWN);
        destination.sendKeys(Keys.ENTER);
Enter fullscreen mode Exit fullscreen mode

Checkboxes

Assert.assertFalse(driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).isSelected());

driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).click();   

Assert.assertTrue(driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).isSelected());
Enter fullscreen mode Exit fullscreen mode

count checkboxes

System.out.println(driver.findElements(By.cssSelector("#discount-checkbox input[type='checkbox']")).size());
        Assert.assertEquals(driver.findElements(By.cssSelector("#discount-checkbox input[type='checkbox']")).size(), 5);
Enter fullscreen mode Exit fullscreen mode

check is element is enabled, selected or displayed

isEnabled(); 
//This method checks if an element is enabled. Returns true if enabled, else false for disabled.
isSelected(); 
// This method checks if element is selected (radio button,checkbox, and so on). It returns true if selected, else false for deselected
isDisplayed();  
//This method checks if element is displayed. In this recipe, we will use some of these methods to check the status and handle
Enter fullscreen mode Exit fullscreen mode

check, if opacity is reduced (nudging to other buttons)

System.out.println(driver.findElement(By.id("Div1")).getAttribute("style").contains("opacity: 0.5"));
driver.findElement(By.id("ctl00_mainContent_rbtnl_Trip_1")).click();
System.out.println(driver.findElement(By.id("Div1")).isEnabled());

 if (driver.findElement(By.id("Div1")).getAttribute("style").contains("opacity: 1")) {
   System.out.println("Calender enabled");
   Assert.assertTrue(true);
   } else {
   Assert.assertTrue(false)
   }

Enter fullscreen mode Exit fullscreen mode

calendar

Click on the calendar button and check how the today date stands out to click or collect all dates in a list + click a specific one:

driver.findElement(By.id("travel_date")).click();

//Grab common attribute of dates and put it into list and iterate
List<WebElement> days = driver.findElements(By.xpath("//td[@class='day']"));

        for (int i = 0; i < days.size(); i ++){
           if( days.get(i).getText().equalsIgnoreCase("23"))
            {
               days.get(i).click();
            }
        }
Enter fullscreen mode Exit fullscreen mode

more on calendar picking

Top comments (0)