Showing posts with label Select. Show all posts
Showing posts with label Select. Show all posts

Friday, May 23, 2014

Selecting options from dropdown using partial text


    Selecting option(s) from dropdown is the one of the more frequent action while automating web application.

  I've written already detailed post for usage of Select class available in WebDriver. You can find that here.

 Here I'm going to write how to choose options using partial text or partial values.

HTML CODE
      <select id="city">
             <option value="City: Chennai">Chennai</option>
             <option value="City: Hyd">Hyderabad</option>
             <option value="City: Blore">Bangalore</option>
     </select>



Choose option using partial & full value

Select Chennai using its partial value.

driver.findElement(By.id("city")).findElement(By.cssSelector("option[value*='Chennai']")).click();

Select Chennai using its value

new Select(driver.findElement(By.id("city"))).selectByValue("City: Chennai");

Choose option using partial & full Text

Select Hyderabad using its partial text.

driver.findElement(By.id("city")).findElement(By.xpath("//option[contains(text(),'Hyd')]")).click();

Select Hyderabad using its text

new Select(driver.findElement(By.id("city"))).selectByVisibleText("Hyderabad");


Related Topics
Select options from dropdown
WebElement & Locators






Saturday, April 6, 2013

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



Thursday, April 12, 2012

How to switch control to pop-up window ?






Switching control to popup



If you want to do any operations in pop-up window you need to switch the control to pop-up window then do all your operations in that and finally close the pop-up window and again select the default (main ) window.


here is WebDriver logic to select Pop-up window


1 . Pop-up window has name/id


     driver.switchTo().window("<window name>");



2. Pop-up window doesn't have name / you don't want to hard code the window name then go for below logic.

Method-1


  • before opening pop-up get the main window handle.
             String mainWindowHandle=driver.getWindowHandle();
  • open the pop-up (click on element which causes open a new window)
             webElement.click();
  • try to get all available open window handles with below command. (the below command returns all window handles as Set)
            Set s = driver.getWindowHandles();
  • from that above set try get newly opened window and switch the control to that (pop-up window handle), as we already know the mainWindowHandle.
          Set s = driver.getWindowHandles();
Iterator ite = s.iterator();
while(ite.hasNext())
            {
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
}
  • Now control is in pop-up window, do all your operations in pop-up and close the pop-up window.
  • Select the default window again.
                 driver.switchTo().window( mainWindowHandle );









Method-2

// get all the window handles before the popup window appears
 Set beforePopup = driver.getWindowHandles();
     
// click the link which creates the popup window
driver.findElement(by).click();
  
// get all the window handles after the popup window appears
Set afterPopup = driver.getWindowHandles();
     
// remove all the handles from before the popup window appears
afterPopup.removeAll(beforePopup);

// there should be only one window handle left
if(afterPopup.size() == 1) {
          driver.switchTo().window((String)afterPopup.toArray()[0]);
 }