Saturday, April 20, 2013

Print table data






Locating table rows and cells
 Sometimes we need to iterate through out table and do some operations with that.

Example :

  1. Verify for expected values in table
  2. Sort  table columns in ascending & descending order and verify.
In above both the cases we need to get the table data to verify. Lets see how to get the data from table.
Steps
  • get table instance
  • using above table instance , get all table rows
  • iterate each row and get all columns values.
Logic


@Test
public void testTable()
{
          WebElement table = driver.findElement(By.id("tableId"));
       
          //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();
          }
}


Happy Coding :)


Regards,
SantoshSarma




Saturday, April 6, 2013

Get Date..!


In many cases we want to use different dates (today's date / future date/ past date) with different formats.

Example:

  1. Task due date you want to fill 5 days from current date with dd-MM-yyyy format.
  2. 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)

Output :
Current date & Time is :
No of days Format Output
0 "MM/dd/yyyy"
5 "MM/dd/yyyy"
-3 "MM/dd/yyyy"



Happy coding :)

Regards,
SantoshSarma

Choosing options from Dropdown - WebDriver






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.

Consider below example

                  HTML CODE
                                         <select id="city">
                                               <option value="Op1">Chennai</option>
                                               <option value="Op2">Hyderabad</option>
                                               <option value="Op3">Bangalore</option>
                                         </select>

Select an Option
Available methods for selecting an option are
  1. selectByIndex(int index)
  2. selectByValue(java.lang.String value)
  3. 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,

  1. deselectAll()
  2. deselectByIndex(int index)
  3. deselectByValue(java.lang.String value)
  4. 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.


  1. getFirstSelectedOption();
  2. 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

                 HTML CODE
                                         <select id="city" multiple>
                                               <option value="Op1">Chennai</option>
                                               <option value="Op2">Hyderabad</option>
                                               <option value="Op3">Bangalore</option>
                                         </select>


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.


Happy Selecting :)


Regards,
SantoshSarma