如何在HashMap中放置两个Web元素列表?

时间:2018-08-31 14:54:05

标签: java selenium

我有两个Selenium Web元素列表。两个列表都包含字符串值。如何将它们放入哈希图中,验证是否存在“上一金额”和“总金额”,并且两者的精度均为2?

enter image description here

我目前已编写以下代码:

@FindBy(How=How.Xpath, using =“xpath to header”)
private static List<WebElement> header;

@FindBy(how=How.Xpath, using =“xpath to headerValue”)
private static List<WebElement> headerValue;

public HasMap<String,String> verifyHeaderAndValue()
{
    HasMap<String,String> hedDetails = new HasMap<>();
    for(int I = 0; I < hedDetails.size(); I++)
    {
        hedDetails.put(header.get(I).getText(), headerValue.get(I).getText());
    }
    return hedDetails;
}

2 个答案:

答案 0 :(得分:0)

您的代码中有一些错别字/错误,因此无法编译。我已经在下面修复了这些问题。

@FindBy(how = How.XPATH, using = "")
private static List<WebElement> header;

@FindBy(how = How.XPATH, using = "")
private static List<WebElement> headerValue;

您的方法实际上并没有验证任何内容,因此我将名称更改为get *,因为这实际上是在做...获取值混合的HashMap

您应该做的第一件事是确保两个List的大小相同,否则比较将立即失败。您的代码中的一个问题是您循环到headDetails的大小。 headDetails是您刚刚创建的变量,因此其大小将为0,并且for循环将永远不会运行。您应该参考headerheaderValue的大小。进行大小检查后,它们的大小都相等,因此使用哪一个都不重要。

public HashMap<String, String> getHeaderAndValue()
{
    // need to make sure that the two element lists are the same size or the comparison should/will fail
    Assert.assertEquals(header.size(), headerValue.size());

    HashMap<String, String> headDetails = new HashMap<>();
    for (int i = 0; i < header.size(); i++)
    {
        headDetails.put(header.get(i).getText(), headerValue.get(i).getText());
    }

    return headDetails;
}

答案 1 :(得分:0)

此处是“前一金额”的值控制代码,对于“总金额”,您可以使用相同的(TestNG声明):

String prevAmount = headDetails.get("Previous Amount");
Assert.assertNotNull(prevAmount, "Previous Amount found");
//if amount can NOT be negative
Assert.assertTrue(prevAmount.matches("^\\d+\\.\\d{2}$"), "Has 2 precision");
//if amount can be negative
Assert.assertTrue(prevAmount.matches("\\d+\\.\\d{2}$"), "Has 2 precision");