Home > OS >  Selenium - search within a List<String> with another String[] or List<String>
Selenium - search within a List<String> with another String[] or List<String>

Time:06-24

I'm a newbie when it comes to Selenium automation testing. Here's my problem

public void verifyItemsAreDisplayed(String columnname, String[] columntosearch) {
       boolean ispresent = false;
       List<string> colitems = getRowData(columnname); // this is getting all text from specific column of a grid using gettext
       for (String item: colitemstosearch) {
            for(String all: colitems) {
                 if (item.contains(all) {
                      system.out.println(item   " is displayed.");
                      ispresent = true;
                      break;
                 }
                 if(!ispresent) {
                      system.out.println(item   " is not displayed.";
                      break;
                 }
            }
      }
 }

This is the call method I use:

String[] values = {"Not Started", "In Progress", "Complete"}
test.verifyItemsAreDisplayed("ID", values);

Expected output:

getRowData method output :

Column name: ID
Row Size: 6
> In Progress
> In Progress
> Not Started
> Complete
> Complete
> Not Started

verifyItemsAreDisplayed method output :

Not Started is displayed.
In Progress is displayed.
Complete is displayed.

What I got :

getRowData method output :

Column name: ID
Row Size: 6
> In Progress
> In Progress
> Not Started
> Complete
> Complete
> Not Started

verifyItemsAreDisplayed method output :

Not Started is NOT displayed.
In Progress is displayed.

The other string that i used to search: Complete are not displayed in the output whether it's in the list or not.

Anyone what went wrong with my code ? thanks

CodePudding user response:

You need three changes:

  • Move the declaration of ispresent to just before the inner loop, so its value is reset for every item being checked
  • Move the check for ispresent until after checking all the items - ie outside the inner loop (and delete the break therein)
  • Fix the numerous spelling/syntax errors preventing your posted code from compiling
public void verifyItemsAreDisplayed(String columnname, String[] columntosearch) {
    List<string> colitems = getRowData(columnname);
    for (String item : colitemstosearch) {
        boolean ispresent = false;
        for (String all : items) {
            if (item.contains(all) {
                System.out.println(item   " is displayed.");
                ispresent = true;
                break;
            }
        }
        if (!ispresent) {
            System.out.println(item   " is not displayed.";
        }

    }
}
  • Related