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

喜夏-厌秋 提交于 2020-06-28 06:20:19

问题


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)

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/44628447/selenium-asserting-the-presence-of-multiple-keywords-of-an-array-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!