Use Of isMultiple() And deselectAll() In Selenium WebDriver With Example

Now you can easily select or deselect any specific option from select box or drop down as described in my earlier POST 1, POST 2 and POST 3. All these three posts will describe you different alternative ways of selecting options from list box or drop down. Now let me take you one step ahead to describe you the use of deselectAll() and isMultiple() methods in selenium webdriver.

deselectAll() Method
deselectAll() method is useful to remove selection from all selected options of select box. It will works with multiple select box when you need to remove all selections. Syntax for deselectAll() method is as bellow.
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectAll();

isMultiple() Method
isMultiple() method is useful to verify if targeted select box is multiple select box or not means we can select multiple options from that select box or not. It will return boolean (true or false) value. We can use it with if condition before working with select box. Syntax is as bellow.
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
boolean value = listbox.isMultiple();
System.out.print(value);

Bellow given example will show you use of isMultiple() and deselectAll() methods very clearly in selenium webdriver.
@Test
 public void test () throws InterruptedException 
 {  
  Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
  
  //To verify that specific select box supports multiple select or not.
  if(listbox.isMultiple())
  {
   System.out.print("Multiple selections is supported");
   listbox.selectByVisibleText("USA");
   listbox.selectByValue("Russia");
   listbox.selectByIndex(5);
   Thread.sleep(3000);
 
   //To deselect all selected options.
   listbox.deselectAll();
   Thread.sleep(2000);
  }
  else
  {
   System.out.print("Not supported multiple selections");
  }
 }

In above example, if condition will check that listbox is multiple select box or not? Used listbox.deselectAll(); to remove all selected options.

You can VIEW MORE SELENIUM WEBDRIVER BASIC COMMANDS.

No comments:

Post a Comment