![](/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.