//Get all rows (tr tags)
List<WebElement> rows = table.findElements(By.tagName("tr"));
//Print data from each row (Data from each td tag)
for (WebElement row : rows) {
List<WebElement> cols = row.findElements(By.tagName("td")); for (WebElement col : cols) {
System.out.print(col.getText() + "\t");
}
System.out.println();
}
}
In many cases we want to use different dates (today's date / future date/ past date) with different formats.
Example:
Task due date you want to fill 5 days from current date with dd-MM-yyyy format.
You want to add today's date automatically to some field with MMM/dd/yy format.
I'm just writing a java method which will take two arguments period (no of days) & format of date.
For Getting today's date pass period as 0 , for getting future date pass period as positive and negative number for getting past date.
<>
Logic
public String getDate(int period,String format)
{
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat(format);
currentDate.add(Calendar.DAY_OF_MONTH, period);
String date = formatter.format(currentDate.getTime());
return date;
}
//Parameters :
//period : no of days gap between the current and desired date
//format : Format of the date (Ex : dd/MM/yyyy . yyyy MMM dd)
In web application we see many drop down lists for many input fields (Ex : gender, age, country..etc). This drop down option is different from normal text/numeric input field. It has separate tag <select></select>in html.
In automation while filling most of the forms we need to fill/select the drop down values also. For achieving this WebDriver has separate class called Select.
In this post we will see what are all different method available in Select class.
Select an Option
Available methods for selecting an option are
selectByIndex(int index)
selectByValue(java.lang.String value)
selectByVisibleText(java.lang.String text)
selectByIndex(int index)
Select the option at the given index.
Usage :
new Select(driver.findElement(By.id("city"))).selectByIndex(2);
In above example it will select the Hyderabad because it is in index 2.
selectByValue(java.lang.String value)
Select all options that have a value matching the argument. Usage :
new Select(driver.findElement(By.id("city"))).selectByValue("Op3");
In above example it will select the Bangalore based on the value attribute of that option.
selectByVisibleText(java.lang.String text)
Select all options that display text matching the argument. Usage :
new Select(driver.findElement(By.id("city"))).selectByVisiableText("Chennai");
In above example it will select the Chennai based on the visible text.
De-select an option
Available methods for de-selecting an option(s) are,
deselectAll()
deselectByIndex(int index)
deselectByValue(java.lang.String value)
deselectByVisibleText(java.lang.String text)
deselectAll()
Clear all selected entries.
deselectByIndex(int index)
Deselect the option at the given index.
deselectByValue(java.lang.String value)
Deselect all options that have a value matching the argument.
deselectByVisibleText(java.lang.String text)
Deselect all options that display text matching the argument.
Getting all options
Some times we may in need to get all the options available in drop down list in that case below method will be useful.
getOptions();
getOptions()
It will return All options belonging to this select tag Usage :
List<WebElement> allCities=new Select(driver.findElement(By.id("city"))).getOptions();
for(WebElement city:allCities)
{
System.out.println(city.getText()); //It will return the text of each option
System.out.println(city.getAttribute("value")); //it will return the value attribute of each option
}
Get Selected Option(s)
If you want to verify whether the proper value got selected in particular drop down list you can make use of below methods.
getFirstSelectedOption();
getAllSelectedOptions() ;
getFirstSelectedOption();
The first selected option in this select tag (or the currently selected option in a normal select)
getAllSelectedOptions() ;
It will return List of All selected options belonging to this select tag. (This will be useful for multiselect picklist)
Handling multi select pick list same as normal drop down( single pick list).
For selecting both Hyderabad, Bangalore option you need to use one of the below logics.
new Select(driver.findElement(By.id("city"))).selectByIndex(2);
new Select(driver.findElement(By.id("city"))).selectByIndex(3); Or
new Select(driver.findElement(By.id("city"))).selectByvalue("Op2");
new Select(driver.findElement(By.id("city"))).selectByvalue("Op3"); Or
new Select(driver.findElement(By.id("city"))).selectByVisiableText("Hyderabad");
new Select(driver.findElement(By.id("city"))).selectByVisiableText("Bangalore");
I hope you understand WebDriver Select class usage in automation.