size( ) when used with List<WebElements> type variable, retrieves the count of Web Elements stored in the List<WebElements> type variable.
In this post, lets use size( ) to count the number of list options available in a Multi-Selection Box field.
Go through the comments mentioned on the below screenshot to understand the Test Description:
Lets Implement the Test on Eclipse IDE:
1. Create a new Java Project say 'WebDriver-Project73' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package73' under the newly created project.
4. Create a Java Class file say 'Class73' under the newly created package as shown below:
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 'countMultiSelectionBoxListOptions( )' as shown below:
3. Create Select Class object for Multi-Selection Box field as shown below:
4. Retrieve all the list options from the Multi-Selection Box field using getOptions( ) command and assign them to the List<WebElement> type variable as shown below:
5. Use size( ) command with the List<WebElement> type variable for retrieving the count of list options available in the Multi-Selection Box field and assign the retrieved count to an integer variable say 'list_options_count' as shown below:
6. Enter the value of the integer variable which holds the count of the list options available in the Multi-Selection Box field into the Username text box field as shown below:
7. Save and Run the 'Class73.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has enter the count of the list options available in the Multi-Selection Box field into the Username text box field as shown below:
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.
Locating elements using HTML Tag Names will be explained in the next post.