HashMap如何做循环遍历

1.TestCase:

 

2.通过cucumber匹配Steps,  用HashMap循环列表。

1     @Then("^verify payRoll data information payRollCode (\\d+)$")
2   public void verify_payroll_data_information(int payRollCode, DataTable data) throws Throwable {
3         HashMap<String, String>  payRollResult = DataTableUtils.toHashMap(data);
4         pr.verifyPayRollInfo(payRollCode, payRollResult);  
5     }   

 

2.1  tohashMap 方法

//对于具有1行标题+1行值的数据表
public static HashMap<String, String> toHashMap(DataTable data) {
        List<String> keys = data.raw().get(0);
        List<String> values = data.raw().get(1);
        LinkedHashMap<String, String> dataHash = new LinkedHashMap<String, String>();
        for (int i=0; i<keys.size(); i++) {
            dataHash.put(keys.get(i), values.get(i));
        }
        return dataHash;
    }
    

 

 

 

 

3. 方法名里面传入类型为:HashMap,  然后通过键值对方式,通过列名,跟值做匹配做循环传入参数。

  

 /**
     * Common  验证薪资计算处理结果
     * @param staffno
     * @param payRollResult
     */
    public void verifyPayRollInfo(int payRollCode, HashMap<String, String> payRollResult) {
        for (Entry<String, String> entry : payRollResult.entrySet()) {
            String columnName = entry.getKey();
            String value = entry.getValue();
            String xpathLess = "//tr[./td[@title='"+ columnName +"']]/td[3]";
            String xpathGreater = "//tr[./td[text()='"+ columnName + "']]/td[4]";
            if (payRollCode < 5000) {
                String actualResult =  waitFor(By.xpath(xpathLess)).getText().trim();
                //driver.findElement(By.xpath("//tr[./td[@title='餐补']]/td[3]")).getText().trim();
                Assert.isEqual(value, actualResult);
            } else {
                String actualComputation =  waitFor(By.xpath(xpathGreater)).getText().trim();
                Assert.isEqual(value, actualComputation);
            }
        }

 

4.最后得到预期的结果值。

posted @ 2017-10-26 16:56  vame  Views(1561)  Comments(0)    收藏  举报