6

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;
@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");
             } 
           }
         }  }}

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">
<option value="">Month</option>
<option value="01">JAN</option>
<option value="02">FEB</option>
<option value="03">MAR</option>
<option value="04">APR</option>
<option value="05">MAY</option>
<option value="06">JUN</option>
<option value="07">JUL</option>
<option value="08">AUG</option>
<option value="09">SEP</option>
<option value="10">OCT</option>
<option value="11">NOV</option>
<option value="12">DEC</option>
9

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;

@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");

    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<WebElement> 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<WebElement> 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
    }
}
  • Hello Paul, it's worked as per my expectation. THANKS MUCH.. And Paul if possible can you let me know how to compare two strings (If you know any other way or method).. – QA4it Feb 25 '14 at 16:18
  • Could you accept the answer then please. As for comparing two strings, what are you trying to compare in them? Are they supposed to be an exact match? What are you attempting to compare them against etc etc – Paul Muir Feb 25 '14 at 16:34
  • I accepted the above code and "thanks" for your time. My scenario is same as above (Ex: Stored all the dropdown values in a string and comparing this string with dropdown values in the application and if matched then pass if not fail.) – QA4it Feb 25 '14 at 16:41
  • If it is in a List, I would do a for (WebElement el:we) { if (!values.contains(el.getText()) { return 0; } } return 1; and then check if the returned value is 0 or 1, if 0 it failed, if 1 it passed. This is just a quick example of a way to go about it. Also, I would build a custom function that can be reused multiple times for such comparisons If you give an example I can assist better. – Paul Muir Feb 25 '14 at 16:52
  • Paul, I am very sorry :( I do not have any other scenario except this. – QA4it Feb 25 '14 at 17:23
0
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();
        loop:  
        for(WebElement we:options)  
        {  
         for (int i=0; i<exp.length; i++){
             if (we.getText().equals(exp[i])){
             System.out.println("Matched");
             break loop;
             } 
          }
        }  
      }
    }
  • In this cases once if loop get executed then it will break the enhance for loop – Pesh Mar 25 '17 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.