2

How can I compare both list is equal Verify data is from Excel sheet. I need to validate both list are same and there is no additional element or missing element from the list. I don't require to sorting the list. And Print output CAGID Excel data = CAGID web list

    String[] Verify1 = Verify.split(",");
        for(String actualView1 : Verify1) {
            System.out.println("string" + actualView1);
        }

        List<WebElement> options = driver.findElements(By.xpath(SUMMARYFIELDS));
        for (WebElement ele : options) {
            System.out.println(ele.getText());
        }

Output String 

string CAGID
string GFPID
string IRU
string Control Unit
string Obligor Classification
string Obligor Limit Rating
string Obligor Risk Rating
string Commentary
string Credit Officer
string Risk Manager
string RCA Date
string RCA Extension Date

Output ele.getText()

CAGID
GFPID
IRU
Control Unit
Obligor Classification
Obligor Limit Rating
Obligor Risk Rating
Commentary
Credit Officer
Risk Manager
RCA Date
RCA Extension Date
ARD
  • 39
  • 1
  • 10

2 Answers2

2

If you don't bother about the order, then collect all text from WebElement to List<String>

List<String> text = options.stream().map(WebElement::getText).collect(Collectors.toList());

Then now just compare by using equals()

System.out.println(text.equals(Arrays.asList(verify1));  // use naming convention for variables 

Note This approach is case sensitive

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • yes this work but how can I print output CAGID Excel data = CAGID text. – ARD Feb 21 '19 at 17:01
  • do you wanna do this for each string @ARD? – Ryuzaki L Feb 21 '19 at 17:12
  • Then you have to iterate it and why do wanna do that @ARD? – Ryuzaki L Feb 21 '19 at 17:15
  • I need to do this as that will help me validate which item failed in test easy to find which is missing element from list or data – ARD Feb 21 '19 at 17:19
  • 1
    You should really be using JUnit or TestNG asserts. They will do all this work for you and when it fails, it will tell you what wasn't equal, e.g. [How to assert that lists are equal with testng?](https://stackoverflow.com/questions/33480936/how-to-assert-that-lists-are-equal-with-testng) – JeffC Feb 21 '19 at 19:06
1

Not really elegant but I thinks it's works

        String[] Verify1 = Verify.split(",");
        List<WebElement> options = driver.findElements(By.xpath(SUMMARYFIELDS));
        boolean isOK = true;    

        for (WebElement ele : options) {
            int i = Arrays.asList(Verify1).indexOf("string "+ele.getText())
            if(i==-1){
                 isOK=false;
            }
            else{
                 System.out.println(Verify1[i]" Excel data = "+ ele.getText()+" web list");
            }
        }
Ehcnalb
  • 466
  • 4
  • 16