Automation_User
Automation_User

Reputation: 69

Selenium-Asserting the presence of multiple keywords of an array in a List

Trying to check the presence of multiple keywords in a list.

List<String> array=new ArrayList<String>();
array.add("Sam Mayers test");
array.add("");
array.add("value");
array.add("");
array.add("");
array.add("");
array.add("District 49");
array.add("Till the last test");
array.add("Dawn Strings");
array.add("Lists value");
array.add("");
array.add("");
array.add("Total Hits Lists");
array.add("Values");


Boolean found = false;

        List<String> keywords_tobe_checked = new ArrayList<>();
        keywords_tobe_checked.add("Lists");
        keywords_tobe_checked.add("value");
        keywords_tobe_checked.add("Mayers");
        List<String> dummylist = new ArrayList<>();

        for (int i = 0; i < array.size(); i++) {
            String val = array.get(i);
            for (int j = 0; j < keywords_tobe_checked.size(); j++) {
            if (val.indexOf(keywords_tobe_checked.get(j)) >= 0) {
                dummylist.add(val);
            }
            }
        }
        System.out.println(dummylist);

        Assert.assertTrue(dummylist.containsAll(keywords_tobe_checked), "Its not correct");

My above code is failing on the asserttrue condition and giving me an incorrect result.

Is there a way i could pass this assertion by the condition that all the keywords mentioned in the "keywords_tobe_checked" are appearing in the "dummylist"?

Updated Code tried:

List<String> array=new ArrayList<String>();
        array.add("Sam Mayers test");
        array.add("");
        array.add("value");
        array.add("");
        array.add("");
        array.add("");
        array.add("District 49");
        array.add("Till the last test");
        array.add("Dawn Strings");
        array.add("Lists value");
        array.add("");
        array.add("");
        array.add("Total Hits Lists");
        array.add("Values");



        List<String> keywords_tobe_checked = new ArrayList<>();
        keywords_tobe_checked.add("Lists");
        keywords_tobe_checked.add("value");
        keywords_tobe_checked.add("Mayers");

        System.out.println(array);
        System.out.println(keywords_tobe_checked);
        for (String keyword : keywords_tobe_checked) {
            Boolean found = false;  
            for (String value : array) {
                if (value.contains(keyword)) {
                    found = true;
                    break;
                }
                Assert.assertTrue(found, "The keyword " + keyword + " not in the list");
            }

        }


 Content of the Array------------       [Sam Mayers test, , value, , , , District 49, Till the last test, Dawn Strings, Lists value, , , Total Hits Lists, Values]
  Content of the Keywords to be checked -----------------  [Lists, value, Mayers]
    Exception in thread "main" java.lang.AssertionError: The keyword Lists not in the list expected [true] but found [false]
        at org.testng.Assert.fail(Assert.java:94)
        at org.testng.Assert.failNotEquals(Assert.java:496)
        at org.testng.Assert.assertTrue(Assert.java:42)

Upvotes: 1

Views: 1852

Answers (2)

Guy
Guy

Reputation: 50819

When checking if List contains String the result will be true only when there is an exact match

array.contains("Lists"); // false

array.contains("Lists value"); // true

To check partial match you need to use the Assert.assertTrue inside the loop

for (String keyword : keywords_tobe_checked) {
    boolean hasKeyword = false;
    for (String value : array) {
        if (value.contains(keyword)) {
            hasKeyword = true;
            break;
        }
    }

    Assert.assertTrue(hasKeyword, "The keyword " + keyword + " is in the list");
}

Upvotes: 2

Fenio
Fenio

Reputation: 3625

What you are doing in your code is not correct due to values in Array List.

While using containsAll method you are looking for SPECIFIC String in Array List.

Example: The 1st list has object Sam Mayers test. The 2nd list has object Mayers.

This is why you have AssertionError. Sam Mayers test and Mayers are totally two different objects.

Upvotes: 0

Related Questions