JKL
JKL

Reputation: 51

How to compare between two list simultaneously and perform conditional actions?

I am comparing between the web page checkbox status with the parameter set in the excel file. However, it stuck on toggling the checkboxes on and off endlessly.

I tried the below codes.

The way I get data from excel for comparison.

    HSSFSheet dispcolsheet = workbook2.getSheet("Display Columns");
    DataFormatter df = new DataFormatter();
    Iterator<Row> colRowItr = dispcolsheet.rowIterator();
    List<String> colstatuslist = new ArrayList<String>();
    while (colRowItr.hasNext()){
        Row row = colRowItr.next();
        Cell colname = row.getCell(0);
        if (colname.getStringCellValue().startsWith("chkColumns_")) {
            Cell colstatuscell = row.getCell(1);
            String colstatus = df.formatCellValue(colstatuscell);
            colstatuslist.add(colstatus);
        }
    }

trial 1

    List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
        for (int i = 0; i < checkboxes.size() ; i++) {
            for (int j = 0; j < colstatuslist.size() ; j++)
              if(checkboxes.get(i).getAttribute("checked type") !=null){
                 if(colstatuslist.get(j).equals("FALSE")){
                    checkboxes.get(i).click();
                } 
            } 
              else {
                   if(colstatuslist.get(j).equals("TRUE")){
                      checkboxes.get(i).click();
               }
             }  
         }

trial 2

    List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
        for (int i = 0; i < checkboxes.size() ; i++) {
            if(checkboxes.get(i).getAttribute("checked type") !=null){
            for (int j = i; j < colstatuslist.size() ; j++) {
                if (colstatuslist.get(j).equals("FALSE")){
                    checkboxes.get(i).click();
                } 
              }
            } 
            else {
                 for (int j = i; j < colstatuslist.size() ; j++) {
                     if (colstatuslist.get(j).equals("TRUE")){
                         checkboxes.get(i).click();
                    }
                  }
                }   
         }

these two codes would toggle only the first checkbox on and off.

trial 3

    List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
    Iterator<String> collistitr = colstatuslist.iterator();
    Iterator<WebElement> chkboxitr = checkboxes.iterator();
    while (collistitr.hasNext() && chkboxitr.hasNext()) {
          for (int i = 0; i < checkboxes.size() ; i++) {
              if(checkboxes.get(i).getAttribute("checked type") !=null){
                 if(colstatuslist.get(i).equals("No")){
                    checkboxes.get(i).click();
            } 
          } 
              else {
                   if(colstatuslist.get(i).equals("Yes")){
                      checkboxes.get(i).click();
                     }
                   }
        }        
    }

this code would click all the checkboxes one by one endlessly.

For the context of the excel part, you may visit my other thread: Why I cannot get values from excel into a list using my codes? I expect to achieve when checkbox1 is checked and the excel value is 'False', it shall click on checkbox1 to uncheck and vice versa to match the excel parameter. Like:

     On web|check status|On xls |xls parameter
box0 get(i)   checked    get(j)   False    ->click box0 on web to uncheck, next
box1 get(i+1) not check  get(j+1) True     ->click box1 on web to check, next
box2 get(i+2) checked    get(j+2) True     ->do nothing, compare next
box3 get(i+3) not check  get(j+3) False    ->do nothing, compare next
...

Upvotes: 0

Views: 131

Answers (1)

frianH
frianH

Reputation: 7563

Use && operator if comparing more than one conditional and use method .isSelected() to check the element is checked or not. Try this bellow :

for (int i = 0; i < checkboxes.size() ; i++) {
    if(checkboxes.get(i).isSelected() && colstatuslist.get(i).toUpperCase().equals("FALSE")){
        checkboxes.get(i).click();
    } else if((!checkboxes.get(i).isSelected()) && colstatuslist.get(i).toUpperCase().equals("TRUE")){
        checkboxes.get(i).click();
    }
}

Upvotes: 2

Related Questions