How to compare values from the list or from dropdown list in webdriver (Java)?

4,446 views
Skip to first unread message

Ravi Kumar

unread,
Feb 26, 2014, 5:46:42 AM2/26/14
to webd...@googlegroups.com
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"); 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"); } } } }}

darrell

unread,
Feb 27, 2014, 10:48:08 AM2/27/14
to webd...@googlegroups.com
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.

Chris Merrill

unread,
Feb 27, 2014, 11:06:10 AM2/27/14
to webd...@googlegroups.com
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
------------------------------------------------------------------------ -

Tulsi tester

unread,
Mar 2, 2014, 2:39:23 AM3/2/14
to webd...@googlegroups.com
Hi Ravi,

I would suggest the following code using list

First create a list of your expected items and then retrieve the list of all items in the dropdown and store them in a list. Now you can compare these two lists by using the following code.


        String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
        List<String> expList = new ArrayList<String>();
       
        for(int i=0;i<exp.length;i++){
            expList.add(exp[i]);
        }
       
        List<String> actList = new ArrayList<String>();
        Select se = new Select(driver.findElement(By.id("date_mon")));
       
        List<WebElement> options = se.getOptions();
       
        for(WebElement dd : options){
            actList.add(dd.getText());
        }   


//call the below method to compare to lists

    public void compareDropdown(List<String> expList,List<String> actList){
        //sorts both lists
        Collections.sort(expList);
        Collections.sort(actList);
        //first compares the size of both list, if true then compares each item in order,if false skips and display both are not same
        if(expList.size()==actList.size()){
            for(int i=0;i<expList.size();i++){
                //comparing each item in order
                if(expList.get(i).equals(actList.get(i))){
                    System.out.println("Matched");
                }else{
                    System.out.println("Not matched");
                }
            }
        }else{
            System.out.println("Drop down values are not same in number");

iniyavan ss

unread,
Sep 7, 2017, 1:04:54 AM9/7/17
to webdriver
Hi Tulsi,

The size comparison will be failed. Please check.

Regards,
Iniyavan S S
Reply all
Reply to author
Forward
0 new messages