Table of Contents
- Dropdowns
- static Dropdown with Select
- Dropdown by ID
- autocomplete Option
- Checkboxes
- check is element is enabled, selected or displayed
- 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");
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();
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;
}
}
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);
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());
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);
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
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)
}
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();
}
}
Top comments (0)