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



  • Below is my code and html code, please let me know how to compare values. When i executed i am getting same result (Mateched) for multiple times. But i want to compare and display result as "Matched" (Only for one time).

    public class Ex1 {
    private WebDriver d;
    @yajahira 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 options = select.getOptions();  
            for(WebElement we:options)  
            {  
             for (int i=0; i

    Output:

    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched
    Mateched 
    

    Html:

    select id="date_mon" onfocus="validateSignupForm(3)" name="date_mon">
    
    
    
    
    
    
    
    
    
    
    
    
    
    


  • The reason you are getting so many different matched is because you print it as you go through the loop. My personal suggestion, I like the KISS method, change the code to something like the following:

    public class Ex1 {
    
    private WebDriver d;
    
    @yajahira 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");
    
        int count = 0;
        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 options = select.getOptions();
        for (WebElement we : options) {
            for (int i = 0; i < exp.length; i++) {
                if (we.getText().equals(exp[i])) {
                    count++;
                }
            }
        }
        if (count == exp.length) {
            System.out.println("matched");
        } else {
            System.out.println("Houston, we have a problem.");
        }
    }
    }
    

    This will just count the amount of matches and then if the total is the total count for the length.

    What I changed on your code:

    Added int count = 0;

    changed System.out.println("Matched"); to count++;

    added:

    if (count == exp.length) {
        System.out.println("matched");
    } else {
        System.out.println("Houston, we have a problem.");
    }
    

    Per the discussion in the comments, I created a function to perform this

    public boolean compareElements(String a,List b) {
        for (WebElement we:b) {
            if (a.equalsIgnoreCase(we.getText())) {
                return true;
            }
        }
        return false;
    }
    

    Example usage:

    for (String e:exp) {
        if (compareElements(e,we)) {
            //code for if it was found
        } else {
            //code if it's not found
        }
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2