【问题标题】:How do I Assert if statement which contains如何断言包含的 if 语句
【发布时间】:2019-04-17 10:02:18
【问题描述】:

我的值在 if 语句中包含 equals 但它仍然未能通过测试。请帮助找出脚本中的问题。
我需要if (383797)contains ($ 383,797)
测试应该通过

    // Database query to get value
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()){
        String mytotal = rs.getString(2);                                       
        System.out.println(mytotal);
        BigDecimal result = new BigDecimal(mytotal);
        System.out.println(result+ " convert valueOf to int");

        // round of to million  
        System.out.println(result.divide(new BigDecimal("1000000")).setScale(0, RoundingMode.FLOOR)+" Result 3");
        // Database data value
        String aarwavalue = (result.divide(new BigDecimal("1000000")).setScale(0, RoundingMode.FLOOR)+"");
        System.out.println(aarwavalue);
        driver.switchTo().frame("mstrFrame");
        //Extract dynamic id for aarawtotal
        WebElement extractxpathid = driver.findElement(By.xpath("//span[text()='AA RWA']"));
        System.out.println(extractxpathid.getAttribute("id"));
        String pathnum = extractxpathid.getAttribute("id");
        String[] xpathid = pathnum.split("-");
        System.out.println(xpathid[0]);
        System.out.println(xpathid[1]);
        System.out.println(xpathid[2]);
        // UI data 
        String aarwatotal = driver.findElement(By.xpath("(//td[@class='x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-"+xpathid[1]+" x-unselectable'])[last()]//div")).getText();
        System.out.println(aarwatotal);
        //assert UI value to database value 
        if(aarwatotal.contains(aarwavalue))
        {
            Reporter.log("Successfully  Database value equal UI value " + aarwatotal);
            Add_Log.info("Successfully  Database value equal UI value " + aarwatotal);
        }
        else
        {
            Reporter.log("  Database value  not equal UI value " + aarwatotal +"and"+ aarwavalue);
            Add_Log.info("Database value  not equal UI value " + aarwatotal +"and"+ aarwavalue);
            //  Assert.fail();
        }

        if(aarwavalue.equalsIgnoreCase(aarwatotal))
        {
            Reporter.log("Successfully  Database value equal UI value " + aarwatotal);
            Add_Log.info("Successfully  Database value equal UI value " + aarwatotal);
        }
        else
        {
            Reporter.log("  Database value  not equal UI value " + aarwatotal +"and"+ aarwavalue);
            Add_Log.info("Database value not equal UI value " + aarwatotal +"and"+ aarwavalue);
            //Assert.fail();
        }
    }

输出

 383797521414.9034250
 383797521414.9034250 convert valueOf to int
 383797 Result 3
 383797
 gridcolumn-1020-textEl
 gridcolumn
 1020
 textEl
 $ 383,797
   Database value  not equal UI value $ 383,797 and 383797
  Database value not equal UI value $ 383,797 and 383797

【问题讨论】:

    标签: java selenium if-statement selenium-webdriver


    【解决方案1】:

    值比较返回false,这就是它未通过测试的原因。 问题在于逗号,

    您可以像这样操作String aarwatotal 来删除逗号:

    String aarwatotal = driver.findElement(By.xpath("(//td[@class='x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-"+xpathid[1]+" x-unselectable'])[last()]//div")).getText();
    //in the line below we replace the comma with empty String
    aarwatotal = aarwatotal.replaceAll(",", "");
    

    之后,aarwatotal 将等于:$ 383797,您现在可以检查它是否正确包含 383797

    其余代码可以保持不变。

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 2019-04-27
      • 2013-06-20
      • 1970-01-01
      • 2020-02-10
      • 2020-05-15
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多