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>
asked Feb 25, 2014 at 13:39

2 Answers 2

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
 }
}
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Feb 25, 2014 at 14:49
7
  • 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).. Commented Feb 25, 2014 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 Commented Feb 25, 2014 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.) Commented Feb 25, 2014 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. Commented Feb 25, 2014 at 16:52
  • Paul, I am very sorry :( I do not have any other scenario except this. Commented Feb 25, 2014 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;
 } 
 }
 } 
 }
 }
the_coder
7541 gold badge4 silver badges13 bronze badges
answered Mar 25, 2017 at 7:16
1
  • In this cases once if loop get executed then it will break the enhance for loop Commented Mar 25, 2017 at 7:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.