java - While working with Selenium webdriver, why do we use linked list for gathering links or dropdown contents with mutliple matches? -


example code this, (this interview question asked me recently)

list linkelements = driver.findelements(by.tagname("a"));

list represents ordered list of objects, meaning can access elements of list in specific order, , index too. can add same element more once list. list allows null elements , can have many null objects in list

you result in particular order 1 one. allow add duplicates. our result can have duplicates need in automation , if requirement different , not need duplicates can use other collection type.if use set not allow duplicates , it's unorder presentation of object.

we use list because when using findelements() instead of findelement() expecting locator return more 1 element(not in every case or scenario). it's practice use list our data saved in list in ordered manner can use them properly.

generally using list in below manner:-

 list<webelement> alloptions = dropdown.findelements(by."our locator");     ( webelement we: alloptions) {          dropdown.sendkeys( keys.down ); //simulate visual movement         sleep(250);                if ( we.gettext().contains( text ) ) select.selectbyvisibletext("value1");     } 

you can in many ways refer below:-

https://sqa.stackexchange.com/questions/8029/how-to-iterate-a-list-of-webelements-and-print-the-values-one-by-one-to-perform

here more detail version identify when use list:-

http://java67.blogspot.in/2013/01/difference-between-set-list-and-map-in-java.html

adding , accessing elements

to add elements list call add() method. method inherited collection interface. here few examples:

list lista = new arraylist();  lista.add("element 1"); lista.add("element 2"); lista.add("element 3"); 

you can access them via index below:-

string element0 = lista.get(0); string element1 = lista.get(1); string element3 = lista.get(2); 

system.out.println(element0+" "+element1+" "+element3);

hope :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -