所以我在Java中用Selenium WebDriver编写测试脚本,将16个复选框文本与我们数据库中的文本进行比较。我们想验证它是否是相同的文本。复选框标签的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";
所以只有tr ['num here']改变了。我创建了一个1 - 16的for循环,每次更新tr []框,然后将它存储在字符串数组中。问题是当它将它存储在字符串数组中时,它从索引1开始,因为for循环从1开始。因此索引0将为null。当我将它与我的数据库中的数组进行比较时,第一个索引(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");
}
这是输出
temArrDB_FOIAExemptionOptions = [1. Some text here., 2. Some text here, ......]
actualFOIAExemptions = [null, 1. Some text here., 2. Some text here, .....]
您可以单独更改for循环条件和xpath,如下所示
变化:
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";
码:
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");
}
更换
actualFOIAExemptions[j] = expectedText;
同
actualFOIAExemptions[j-1] = expectedText;