Saturday 15 September 2018

Why in Selenium findElements is made to return List and not Set

First of all let us see what are the difference between List and Set in Java.

Both are Interfaces extending Collections interface.

Some of the most used implementations of List in Java are ArrayList, LinkedList.
Some of the most used implementations of Set in Java are HashSet, LinkedHashSet, TreeSet.

These are the characteristics of List:
* Is a dynamic array.
* Any elements in the list can be controlled using index.
* Allows duplicate values.
* Allows null values.

These are the characteristics of Set:
* Is a dynamic array.
* There is no index to control elements in Set.
* Does not allow duplicate values. If duplicate entry added, will ignore it.
* Some implementations such as HashSet and LinkedHashSet allow null values, whereas some implementations like TreeSet do not allow null values.

Now, In Selenium why driver.findElements() made to return List and not Set. Is because, only List allows duplicate values and not Set. There is a possibility that when we driver.findElements() there could be duplicate elements returned and as well sometimes no elements returned (null). So list is the best possible return type for findElements method.

Similarly, why driver.getWindowHandles(); is made to return Set. Is because, every window will have unique window handle and cannot be duplicate and cannot be null.

That is the reason why driver.findElements()  is made to return List and driver.getWindowHandles(); is made to return Set. 

Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/

No comments:

Post a Comment

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...