Writing Automation Script for List box

Writing Automation Script for List box

In this tutorial, we are going to learn about writing automation script for List box in Selenium 3.0. List boxes are designed to allow selection of multiple options unlike drop down list box & radio buttons.

This tutorial is chapter 19B of complete Selenium 3.0 Tutorials, being developed by Techcanvass. These tutorials are categorized into Java, Selenium, TestNG, Automation Framework and Grid.

Objective of this tutorial is to demonstrate:

  • How to write test cases for unit testing of List boxes?
  • What are key usage scenario for multi-select list boxes

Web page used for this Tutorial

We are going to use http://techcanvass.com/Examples/multi-select.html for this example. This is a simple web page with multi-select list box.

Multi Select List Box

There are two ways in which we can select the desired value from a multi-select list box as listed below:

  • selectByValue()
  • selectByIndex()

Method I: using selectByValue()

As discussed previously, we need to instantiate the multi-select list box before selecting a particular value. As an example, let’s select the value Opel from the multi-select list box.

Selecting Value from List Box

ID of the multi-select box is “multiselect” and we will use it to instantiate the list box.

Select listbox = new Select(driver.findElement(By.id(“multiselect”)));

We then use selectByValue() method to select a particular value from the multi-select list box. The code is as shown below:

listbox.selectByValue(“opel”);

Since we can select multiple values from the list box, we can simple calling selectByValue() multiple times.

Method II: using selectByIndex()

The selecyByIndex() works almost in the similar way like selectByValue(). The difference is that it uses index of the value to be selected. Using the same example as above, we will select “opel” and “audi” values from the multi-select list box.

The index starts from 0 in multi-select list box. This is true for even drop down list box as indicated in the previous article:

Read Also: Writing automation script for Dropdown list box

We can select multiple values using index values as shown below:

Select listbox = new Select(driver.findElement(By.id(“multiselect”)));
listbox.selectByIndex(0);

You can also select multiple values by using the method selectByIndex() multiple times.

Finding the selected value in the multi-select list box

How can you find the selected value in the multi-select list box ?

We have a getFirstSelectedOption() method in selenium for getting the selected value from the drop down list box. This method returns a WebElement object. The code below shows the usage:

WebElement option = listbox.getFirstSelectedOption();

We can also print the selected value by using getText() method as shown below:

System.out.println(option.getText()); //prints selected option

As multi-select list box has the multi-select feature, we can use another method to print all the selected options as shown below:

//Listing down all the selected options
List<WebElement> options = listbox.getAllSelectedOptions();
for (WebElement option2 : options) {
System.out.println(option2.getText()); //Prints “Option”, followed by “Not Option”
}

 

Listing down all the option

We can list down all the options or values of the multi-select list box by using the following piece of code:

//Listing down all the options
List<WebElement> options = listbox.getOptions();
for (WebElement option : options) {
System.out.println(option.getText());
}

We have covered the most important methods for the usage in the case of a multi-select list box. You can explore other methods.

Complete code

We present the complete code for selecting multiple options (“opel” and “audi”) and printing them.

package basicSeleniumScripts;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class MultiselectListBox {

public static void main(String[] args) throws InterruptedException {

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);
WebDriver driver;
driver = new ChromeDriver();

driver.get(“http://techcanvass.com/Examples/multi-select.html”);
Select listbox = new Select(driver.findElement(By.id(“multiselect”)));
listbox.selectByValue(“opel”);
listbox.selectByValue(“audi”);
Thread.sleep(5000);

//prints first option which is selected
WebElement option = listbox.getFirstSelectedOption();
System.out.println(option.getText());

System.out.println(“—Showing all the selected options—-“);

//Listing down all the selected options
List<WebElement> options = listbox.getAllSelectedOptions();
for (WebElement option2 : options) {
System.out.println(option2.getText()); //Prints “Option”, followed by “Not Option”
}

//Quitting the web driver
driver.quit();

}

}

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course

Selenium 3.0 Training with Java

Selenium Training Course

We are going to launch DevOps Certification training very soon, watch out this space

Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *