Selenium Java Dropdowns, Checkboxes, Calendar, Autocomplete
Tags: testing, testautomation, selenium
Published: 2021-02-05 21:35:52

## Table of Contents
1. [Dropdowns](#dropdowns)
2. [static Dropdown with Select](#static-dropdown-with-select)
3. [Dropdown by ID](#dropdown-by-id)
4. [autocomplete Option](#autocomplete-option)
5. [Checkboxes](#checkboxes)
6. [check is element is enabled, selected or displayed](#check-is-element-is-enabled-selected-or-displayed)
7. [calendar](#calendar)

#Dropdowns
## static Dropdown with Select
```java
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");
``` 
##Dropdown by ID
        
```java
//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();
```
## autocomplete Option
clicking on a special text
```java      
// 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;
   }
}
```
moving the arrows up and down
```java
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);
```
# Checkboxes
    
```java
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());
``` 
count checkboxes
```java 
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);
``` 
# check is element is enabled, selected or displayed
```java
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
```
check, if opacity is reduced (nudging to other buttons)

```java
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)
   }

```
#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: 

```java
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();
            }
        }
```
[more on calendar picking](https://thetesterscorner.com/select-date-from-calendar-in-selenium/)