1

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, .....]
0

2 Answers 2

1

You can change the for loop condition and xpath alone as below

Changes:

 for(int j = 0; j < itemArrDB_FOIAExemptionOptions.length; j++) {

.

int xpathIndex=j+1;
String xpathLocation = "//*[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr["+xpathIndex+"]/td/span/label";

Code:

for(int j = 0; j < itemArrDB_FOIAExemptionOptions.length; j++) {
                    // getting each table row checkbox text and storing it in an array to compare
                    int xpathIndex=j+1;
                    String xpathLocation = "//*[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr["+xpathIndex+"]/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");
                }
Sign up to request clarification or add additional context in comments.

3 Comments

When I made the changes and tested it out, xpath location started at 1, then jumped to 7, and failed.
Ooops. Actually , j should be increased by 1 for each iteration.I added the xpathIndex explicitly and modified the xpath. Please try with the updated one.
Glad to know that it helped.
1

Replace

actualFOIAExemptions[j] = expectedText;

with

actualFOIAExemptions[j-1] = expectedText;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.