The List contract in Java collections guarantees that items remain in the order
that were specified when the items were placed in the list. add() adds the item
to the end. add(item, index) inserts into the list at the given index. Future
iterations of the list will ALWAYS return the items in the same order.
However, I do not know if there is a guarantee of what order they are put in the
list by WebDriver. One would assume that they appear in the same order they
appear in the DOM (and the source, if relevant). But you know what they say
about assuming.
You could sort both lists and compare them that way. Or check the size of the
list and then search for all the items in it.
Chris
On 2/27/2014 10:48 AM, darrell wrote:
> Technically, the select.getOptions() returns an implementation of the List interface. I'm not
> positive but I believe there is no guarantee that all implementations of List will return the
> collection in a given order. To be safe, if order does not matter, I would get a List<WebElement>,
> place the getText() in a List<String> then use something like hamcrest to compare the two lists. If
> order mattered I would find the options, one at a time, and place them in an array. Then compare
> that array to the expected array, e.g.
>
> int numberOfOptions = d.findElements(By.cssSelector("#date_mon>option")).size();
> for(int i = 0; i < numberOfOptions; i++) {
> String locator = String.format("#date_mon>option:nth-of-type(%d)", i+1);
> String s = d.findElement(By.cssSelector(locator)).getText();
> // compare exp[i] with s
> }
>
> Not quite as efficient as your code but safer. Your call as to which is more important.
>
> On Wednesday, 26 February 2014 05:46:42 UTC-5, Ravi Kumar wrote:
>
> |Hi Friends,
>
> Below is my code and want to match String values with dropdown values from the application. Any other way to match
> dropdown values?. Please suggest.
>
> ||public class Ex1 {
> private WebDriver d;
> @Test
> public void testUntitled() throws Exception {
> d = new FirefoxDriver();
> d.get("
http://register.rediff.com/commonreg/index.php?redr=http://portfolio.rediff.com/money/jsp/loginnew.jsp?redr=home <
http://register.rediff.com/commonreg/index.php?redr=http://portfolio.rediff.com/money/jsp/loginnew.jsp?redr=home>");
>
> String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
> WebElement dropdown = d.findElement(By.id("date_mon"));
> Select select = new Select(dropdown);
>
> List<WebElement> options = select.getOptions();
> for(WebElement we:options)
> {
> for (int i=0; i<exp.length; i++){
> if (we.getText().equals(exp[i])){
> System.out.println("Matched");
> }
> }
> } }}|
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
>
webdriver+...@googlegroups.com.
> To post to this group, send email to
webd...@googlegroups.com.
> Visit this group at
http://groups.google.com/group/webdriver.
> For more options, visit
https://groups.google.com/groups/opt_out.
--
------------------------------------------------------------------------ -
Chris Merrill | Web Performance, Inc.
ch...@webperformance.com |
http://webperformance.com
919-433-1762 |
919-845-7601
Web Performance: Website Load Testing Software & Services
------------------------------------------------------------------------ -