场景:要循环界面Table数据源与导出Excel数据源作对比。
说明: List<HashMap<String,String>>
List中每一项都是一个HashMap
HashMap<String,String> map中 key是一个String,value也是一个String
一: 如何定位界面Table 一整行的定位。
XPath写法示例://table[contains(@class,'condensed')][contains(@style,'margin-bottom')]/tbody/tr
注意事项:要确保定位只能匹配唯一的Code
二: Feature 示例:
1 Scenario: E-1342:休假结余设立导出,跟菜单显示一致 2 When I click on menu 时间管理>休假结余>休假结余设立 from HCM page 3 #Then I select leaveCode for NAL|SL2|OL| 4 Then I advance search staff no 0001 5 Then I select leaveCode for NAL 6 Then I click export button leave Banlance 7 Then verify the export Excel Data compare with Interface table
三 :Step 示例:
@Then("^verify the export Excel Data compare with Interface table$") public void verify_the_export_Excel_Data_compare_with_Interface_table() throws Exception { List<HashMap<String, String>> previewData = vp.getLeaveBanlaceList(); //获取Table数据源集合 String fileName = anp.downloadFile(); vp.verifyExportExcelDataCompareWithInterfaceTable(fileName, previewData); }
四、Page 示例
1 /** 2 * 用于休假结余设立菜单,返回页面的List<LinkedHashMap> 3 * @return 4 * @throws Exception 5 */ 6 public List<HashMap<String, String>> getLeaveBanlaceList() throws Exception { 7 List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); 8 List<WebElement> trList = findAll(By.xpath("//table[contains(@class,'condensed')]/tbody/tr")); 9 for (int i = 1; i<trList.size(); i++) { 10 String trRowXpath = "//table[contains(@class,'condensed')][contains(@style,'margin-bottom')]/tbody/tr["+ i +"]"; 11 LinkedHashMap<String, String> dataOneRow = getRowLeaveBanlanceData(trRowXpath); 12 data.add(dataOneRow); 13 } 14 return data; 15 }
调用getRowLeaveBanlanceData方法,得到LinkedHashMap
1 public LinkedHashMap<String, String> getRowLeaveBanlanceData(String trRowXpath) throws Exception { 2 waitLoading(); 3 LinkedHashMap<String, String> actualData = new LinkedHashMap<String, String>(); 4 List<WebElement> headers = waitFor(By.xpath(trRowXpath + "/../../thead/tr/td")); 5 List<WebElement> values = waitFor(By.xpath(trRowXpath + "/td")); 6 for (int i = 1 ; i < headers.size(); i++) { 7 if (!headers.get(i).getText().trim().isEmpty()) { 8 actualData.put(headers.get(i).getText().trim(), values.get(i).getText().trim()); 9 } 10 } 11 return actualData; 12 } 13
输出结果:{员工工号=0001, 姓名=黄明, 休假代码=NAL, 休假名称=年假, 上年结余=0, 本年享有=96, 本年调整=0, 下年享有=96, 下年调整=0, 合共已用=0, 备注=}