148. Using getAllSelectedOptions( ) to get all the selected options in Multi-Selection Box field






getAllSelectedOptions( )  command is used to retrieve the list of options that are currently selected in the Multi-Selection Box field.

Suppose if more than one option is selected in the Multi-Selection Box field, we can use getAllSelectedOptions( ) command to retrieve those selected options.

Test Description:

Implement the below steps:

1. Open http://compendiumdev.co.uk/selenium/basic_html_form.html page
2. Deselect all the list options from the Multi-Selection Box field using deselectAll( ) command
3. Clear the Text Area field using clear( ) command
4. Select three list options 'Selection Item 1', 'Selection Item 2' and 'Selection Item 3' using selectByVisibleText( ) command
5. Retrieve the list options selected in step 4 using getAllSelectedOptions( ) command
6. Extract the label texts of the retrieved list options using getText() command.
7. Enter the extracted label texts into the 'Text Area Comment' text area field using sendKeys( ) command

Also look into the comments provided on the below screenshot to understand the Test Description:




Lets Implement the Test on Eclipse IDE:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project67' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package67' under the newly created project.
4. Create a Java Class file say 'Class67' under the newly created package as shown below:




Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:



2. Create a test method 'getAllSelectedOptionsDemo()'  as shown below:



3. Create a Select Class object for Multi-Selection Box field as shown below:



4. Deselect all the list options in the Multi-Selection Box field as shown below:



5. Select the list options 'Selection Item1', 'Selection Item 2' and 'Selection Item 3' from the Multi-Selection Box field as shown below:



6. Clear the Text Area field as shown below:



7. Retrieve the list options that are currently selected in the Multi-Selection Box field using getAllSelectedOptions( ) and assign them to List<WebElement> type variable as shown below:




8. Using for loop for extracting label text of  each and every retrieved list option one by one and entering the extracted label text into Text Area field for confirmation:





9. Save and Run the 'Class67.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has retrieved all the list options that are selected  in the multi-selection box field, extracted their label text and entered the extracted label texts into the Text Area field as shown in the below video:

Watch the below video:

Click here to watch the video.

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.


Please comment below to feedback or ask questions.

Using getOptions( ) to get all the options available in Drop Down field  will be explained in the next post.

Followers

Labels