So I am writing a test script in Selenium WebDriver in Java to compare 16 checkboxes text to the one in our database. We want to validate if its the same text. The xpath location for the checkbox label goes from
//[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr[1]/td/span/label";
to
//[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr[16]/td/span/label";
so only the tr['num here'] changes. I created a for loop from 1 - 16 and updated the tr[] boxes each time and then stores it in a string array. The problem is when it stores it in the string array, it starts at index 1 because the for loop starts at 1. So index 0 will be null. When I compare it to the array from my database, the first index (0) is the first string, so it will fail because it does not match. I don't know how I can solve this. I thought about using two for loops but it got worst. Here is my code.
// from 1 - 16
for(int j = 1; j <= itemArrDB_FOIAExemptionOptions.length; j++) {
// getting each table row checkbox text and storing it in an array to compare
String xpathLocation = "//*[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr["+j+"]/td/span/label";
expectedText = driver.findElement(By.xpath(xpathLocation)).getText().trim();
actualFOIAExemptions[j] = expectedText;
System.out.println("ADDED: " + actualFOIAExemptions[j]);
}
System.out.println("----------------------------------");
System.out.println("itemArrDB_FOIAExemptionOptions = " + Arrays.toString(itemArrDB_FOIAExemptionOptions));
System.out.println("actualFOIAExemptions = " + Arrays.toString(actualFOIAExemptions));
System.out.println("----------------------------------");
if(Arrays.equals(actualFOIAExemptions, itemArrDB_FOIAExemptionOptions)) {
System.out.println("matches the DB");
report.log(LogStatus.PASS, "matches the DB");
}
else {
System.out.println("DOES NOT match the DB");
report.log(LogStatus.FAIL, "s DOES NOT match the DB");
}
Here is the output
temArrDB_FOIAExemptionOptions = [1. Some text here., 2. Some text here, ......]
actualFOIAExemptions = [null, 1. Some text here., 2. Some text here, .....]