![](/img/trans.png)
[英]Iterate through a list of webelements within a table and assert equal each string
[英]Assert Equal to compare string and Array List with text data from excel
下面的代码我已经使用过if(包含),但是我不能使用if(等于),因为String [] actualViews数据来自Excel表。
“报告期,营业日,受管理的地理区域,企业组织,关系受管理的行业,会计处理,业务部门,受管部门,预订国,部门,第三方/公司间,显示调整,批准单位,法律手段,法律手段国家,资产类型,承保范围,IRU信用组织,IRU信用承保范围,IRU地区,CAGID高级风险经理”
在Excel中未提及的获取的文本命名仍然是测试通过,并且List xviews等于工作表数据仍然是我的测试通过。 下面的代码不起作用。 我需要帮助,如何验证来自ArrayList和String []的数据是否相等。
public void dataVisualizationCheck(WebDriver driver, String actualView, String reportingPeriod,
String summaryReportName) throws InterruptedException {
String[] actualViews = actualView.split(",");
List<String> xviews = new ArrayList<>();
boolean flag = false;
waitForLoad(driver, 60);
waitForElementPresent(driver, 30, dataVisualizationHeader);
int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
for (int i = 1; i <= size; i++) {
xviews.add(getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i));
//System.out.println(xviews +" New View");
// System.out.println(xviews +" New View");
}
for (int i1 = 0; i1 < actualViews.length; i1++) {
//System.out.println(actualViews[i1] + " Actual");
if (xviews.contains(actualViews[i1].trim().toString())) {
Add_Log.info("Successfully displayed "+actualViews[i1].trim().toString() + " filter criteria is displayed in Data Visualisation Criteria screen");
Reporter.log("Successfully displayed "+actualViews[i1].trim().toString() + " filter criteria is displayed in Data Visualisation Criteria screen");
} else {
Add_Log.info(actualViews[i1].trim().toString() + " filter criteria is not displayed in Data Visualisation Criteria screen");
Reporter.log(actualViews[i1].trim().toString() + " filter criteria is not displayed in Data Visualisation Criteria screen");
flag = true;
}
}
if (flag) {
TestResultStatus.Testfail = true;
Assert.fail();
} else {
Add_Log.info("Successfully all filter criterias are displayed in Data Visualisation Criteria screen");
Reporter.log("Successfully all filter criterias are displayed in Data Visualisation Criteria screen");
}
}
输出量
INFO [main] (SeleniumUtils.java:506) - Successfully waited for Summary option expand to be present on page
INFO [main] (SeleniumUtils.java:469) - Successfully waited for loader to disappear
INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from Filter criteria #1
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from Filter criteria #2
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from Filter criteria #3
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from Filter criteria #4
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from Filter criteria #5
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from Filter criteria #6
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from Filter criteria #7
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from Filter criteria #8
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from Filter criteria #9
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from Filter criteria #10
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from Filter criteria #11
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from Filter criteria #12
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from Filter criteria #13
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from Filter criteria #14
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from Filter criteria #15
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from Filter criteria #16
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from Filter criteria #17
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from Filter criteria #18
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from Filter criteria #19
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from Filter criteria #20
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from Filter criteria #21
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from Filter criteria #22
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from Filter criteria #23
[Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, Denomination, CAGID] New View
INFO [main] (FrontPage.java:78) - Successfully displayed Reporting Period filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Business Day filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Managed Geography filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Business Organization filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Relationship Managed Industry filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Accounting Treatment filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Business Unit filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Managed Segment filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Booking Country filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Sector filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Third Party / Intercompany filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Show Adjustments filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Approving Unit filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Legal Vehicle filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Legal Vehicle Country filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Asset Type filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Coverage filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed IRU Credit Org filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed IRU Credit Coverage filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed IRU Region filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed Senior Risk Manager filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:78) - Successfully displayed CAGID filter criteria is displayed in Data Visualisation Criteria screen
INFO [main] (FrontPage.java:91) - Successfully all filter criterias are displayed in Data Visualisation Criteria screen
INFO [main] (Sanity_TC.java:3382) - Sanity_TC002 is PASS
您好,阿里CSE
我尝试了此方法,但无法正常工作,这使我如何解决此代码。
public void dataVisualizationCheck1(WebDriver driver, String actualView, String reportingPeriod,
String summaryReportName) throws InterruptedException {
waitForLoad(driver, 60);
waitForElementPresent(driver, 30, dataVisualizationHeader);
String[] actualViews = actualView.split(",");
boolean flag = false;
int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
String[] xviews = new String[size];
for (int i = 1; i <= size; i++) {
xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);
for(int i1=0;i1<actualViews.length;i1++) {
boolean equal = false;
for(int j=0;j<xviews.length;j++) {
if(actualViews[i1].equals(xviews[j])) {
equal = true;
break;
}
}
if(equal) {
System.out.println("=> '"+actualViews[i1]+"' is there in the other array...");
} else {
System.out.println("=> '"+actualViews[i1]+"' is NOT there in the other array...");
}
}
}
输出值
INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from Filter criteria #1
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from Filter criteria #2
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from Filter criteria #3
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from Filter criteria #4
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from Filter criteria #5
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from Filter criteria #6
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from Filter criteria #7
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from Filter criteria #8
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from Filter criteria #9
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from Filter criteria #10
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from Filter criteria #11
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from Filter criteria #12
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from Filter criteria #13
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from Filter criteria #14
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from Filter criteria #15
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from Filter criteria #16
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from Filter criteria #17
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from Filter criteria #18
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from Filter criteria #19
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from Filter criteria #20
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from Filter criteria #21
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from Filter criteria #22
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from Filter criteria #23
=> 'Reporting Period' is there in the other array...
=> ' Business Day' is NOT there in the other array...
=> ' Managed Geography' is NOT there in the other array...
=> ' Business Organization' is NOT there in the other array...
=> ' Relationship Managed Industry' is NOT there in the other array...
=> ' Accounting Treatment' is NOT there in the other array...
=> ' Business Unit' is NOT there in the other array...
=> ' Managed Segment' is NOT there in the other array...
=> ' Booking Country' is NOT there in the other array...
=> ' Sector' is NOT there in the other array...
=> ' Third Party / Intercompany' is NOT there in the other array...
=> ' Show Adjustments' is NOT there in the other array...
=> ' Approving Unit' is NOT there in the other array...
=> ' Legal Vehicle' is NOT there in the other array...
=> ' Legal Vehicle Country' is NOT there in the other array...
=> ' Asset Type' is NOT there in the other array...
=> ' Coverage' is NOT there in the other array...
=> ' IRU Credit Org' is NOT there in the other array...
=> ' IRU Credit Coverage' is NOT there in the other array...
=> ' IRU Region' is NOT there in the other array...
=> ' Senior Risk Manager' is NOT there in the other array...
=> ' Denomination' is NOT there in the other array...
=> ' CAGID' is NOT there in the other array...
INFO [main] (Sanity_TC.java:3391) - Sanity_TC002 is
输出中断
INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from Filter criteria #1
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from Filter criteria #2
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from Filter criteria #3
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from Filter criteria #4
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from Filter criteria #5
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from Filter criteria #6
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from Filter criteria #7
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from Filter criteria #8
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from Filter criteria #9
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from Filter criteria #10
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from Filter criteria #11
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from Filter criteria #12
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from Filter criteria #13
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from Filter criteria #14
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from Filter criteria #15
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from Filter criteria #16
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from Filter criteria #17
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from Filter criteria #18
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from Filter criteria #19
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from Filter criteria #20
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from Filter criteria #21
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from Filter criteria #22
INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from Filter criteria #23
=> 'Reporting Period' is there in the other array...
INFO [main] (Sanity_TC.java:3391) - Sanity_TC002 is
无需将字符串数组数据与数组列表数据进行比较,您可以将它们转换为相同的数据结构(与字符串数组或数组列表一样),对其进行排序并进行比较比较容易。
根据下一行,您将了解实际的视图长度,因此将它们存储在某个变量中并创建一个具有该长度的新字符串数组:
String[] actualViews = actualView.split(",");
int actualViewsSize = actualViews.length;
String xviews = new String[actualViewsSize];
或使用以下大小初始化数组:
int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
String xviews = new String[actualViewsSize];
然后循环遍历,直到数据可视化字段的大小并将它们存储在新创建的数组中:
for (int i = 1; i <= size; i++) {
xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);
//System.out.println(xviews +" New View");
// System.out.println(xviews +" New View");
}
最后,您可以对两个字符串数组进行排序,并且可以像下面这样进行比较:
Arrays.sort(actualViews);
Arrays.sort(xviews);
boolean equal = true;
for(int i=0;i<actualViews.length && equal;i++) {
if(!actualViews[i].equals(xviews[i]) {
equal = false;
}
}
if(equal) {
System.out.println("=> The both String Arrays data is equal...");
} else {
System.out.println("=> The both String Arrays data is NOT equal...");
}
但是如果两个数组的长度相同,那么上面的代码就可以完美地工作,否则您需要使用两个循环来比较数据,如下所示,此处的排序是可选的:
for(int i=0;i<actualViews.length;i++) {
boolean equal = false;
for(int j=0;j<xviews.length;j++) {
if(actualViews[i].equals(xviews[j])) {
equal = true;
break;
}
}
if(equal) {
System.out.println("=> '"+actualViews[i]+"' is there in the other array...");
} else {
System.out.println("=> '"+actualViews[i]+"' is NOT there in the other array...");
}
}
更新1:
您没有在修改后的代码中添加中断条件,而是需要分离存储部分,请尝试以下代码:
public void dataVisualizationCheck1(WebDriver driver, String actualView, String reportingPeriod, String summaryReportName) throws InterruptedException {
waitForLoad(driver, 60);
waitForElementPresent(driver, 30, dataVisualizationHeader);
String[] actualViews = actualView.split(",");
boolean flag = false;
int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
String[] xviews = new String[size];
for (int i = 1; i <= size; i++) {
xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);
}
for(int i=0;i<actualViews.length;i++) {
boolean equal = false;
for(int j=0;j<xviews.length;j++) {
if(actualViews[i].equals(xviews[j])) {
equal = true;
break;
}
}
if(equal) {
System.out.println("=> '"+actualViews[i]+"' is there in the other array...");
break;
} else {
System.out.println("=> '"+actualViews[i]+"' is NOT there in the other array...");
}
}
}
更新2:
如果仍然遇到问题,请参考以下示例代码,该示例代码将遍历两个数组并尝试查找匹配的,不匹配的值:
// Suppose, below are the actual views
String actualViewsString = "Ali, Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, CAGID";
String[] actualViews = actualViewsString.split(", ");
for(String actualView : actualViews) {
System.out.println(actualView);
}
System.out.println("****************************************************************");
// Suppose, below are the xviews
String xviewsString = "Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, CAGID";
String[] xviews = xviewsString.split(", ");
for(String xview : xviews) {
System.out.println(xview);
}
// Comparing both the String arrays
for(int i=0;i<actualViews.length;i++) {
boolean matching = false;
for(int j=0;j<xviews.length;j++) {
if(actualViews[i].equals(xviews[j])) {
matching = true;
break;
}
}
if(matching) {
System.out.println("=> '"+actualViews[i]+"' matching is there in the both arrays...");
} else {
System.out.println("\n===> '"+actualViews[i]+"' matching is NOT there in the both arrays...\n");
}
}
希望对您有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.