简体   繁体   English

Selenium:比较复选框中的两个字符串数组

[英]Selenium: compare two arrays of string from checkbox

So I am writing a test script in Selenium WebDriver in Java to compare 16 checkboxes text to the one in our database. 因此,我正在用Java的Selenium WebDriver编写一个测试脚本,以将16个复选框文本与我们数据库中的复选框进行比较。 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"; 复选框标签的xpath位置从//[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr[1]/td/span/label";//[@id='ctl00_ctl00_Content_ContentPlaceHolderMain_ctl00_EditControl_FouoExemptionCheckBox']/tbody/tr[16]/td/span/label";

so only the tr['num here'] changes. 因此只有tr ['num here']会更改。 I created a for loop from 1 - 16 and updated the tr[] boxes each time and then stores it in a string array. 我从1-16创建了一个for循环,并每次都更新了tr []框,然后将其存储在字符串数组中。 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. 问题是当它存储在字符串数组中时,它从索引1开始,因为for循环从1开始。因此索引0将为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. 当我将其与数据库中的数组进行比较时,第一个索引(0)是第一个字符串,因此它将失败,因为它不匹配。 I don't know how I can solve this. 我不知道该如何解决。 I thought about using two for loops but it got worst. 我考虑过使用两个for循环,但情况更糟。 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, .....]

Replace 更换

actualFOIAExemptions[j] = expectedText;

with

actualFOIAExemptions[j-1] = expectedText;

You can change the for loop condition and xpath alone as below 您可以如下更改单独的for循环条件和xpath

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM