Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium:比较复选框中的两个字符串数组_Java_Selenium - Fatal编程技术网

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

Java Selenium:比较复选框中的两个字符串数组,java,selenium,Java,Selenium,因此,我正在用Java编写SeleniumWebDriver测试脚本,将16个复选框文本与数据库中的文本进行比较。我们想验证它是否是相同的文本。复选框标签的xpath位置从 /[@id='ctl00\u ctl00\u Content\u contentplaceholder main\u ctl00\u EditControl\u foouexemptioncheckbox']/tbody/tr[1]/td/span/label” 到 /[@id='ctl00\u ctl00\u Conten

因此,我正在用Java编写SeleniumWebDriver测试脚本,将16个复选框文本与数据库中的文本进行比较。我们想验证它是否是相同的文本。复选框标签的xpath位置从
/[@id='ctl00\u ctl00\u Content\u contentplaceholder main\u ctl00\u EditControl\u foouexemptioncheckbox']/tbody/tr[1]/td/span/label”
/[@id='ctl00\u ctl00\u Content\u contentplaceholder main\u ctl00\u EditControl\u foouexemptioncheckbox']/tbody/tr[16]/td/span/label”

所以只有tr['num here']改变。我从1-16创建了一个for循环,每次更新tr[]框,然后将其存储在字符串数组中。问题是当它将其存储在字符串数组中时,它从索引1开始,因为for循环从1开始。因此索引0将为空。当我将它与数据库中的数组进行比较时,第一个索引(0)是第一个字符串,因此它将失败,因为它不匹配。我不知道怎么才能解决这个问题。我曾想过用两个for循环,但效果最差。这是我的密码

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

actualFOIAExemptions[j] = expectedText;


您可以单独更改for循环条件和xpath,如下所示

更改:

 for(int j = 0; j < itemArrDB_FOIAExemptionOptions.length; j++) {
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");
                }
代码:

 for(int j = 0; j < itemArrDB_FOIAExemptionOptions.length; j++) {
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");
                }
for(int j=0;j
当我进行更改并测试时,xpath位置从1开始,然后跳到7,失败了。Ooops。实际上,每次迭代j应该增加1。请试用最新版本。很高兴知道它有帮助。
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");
                }