java - 如何断言包含以下内容的 if 语句

标签 java selenium if-statement selenium-webdriver

我的值在 if 语句中包含 equals 但仍然未通过测试。请帮忙定位脚本中的问题。
我需要if (383797)包含($ 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

最佳答案

值比较返回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

其余代码可以保持不变。

关于java - 如何断言包含以下内容的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55715221/

相关文章:

python - 如何使用带有 Python 的 Selenium 单击确认按钮?

python - 无法从网页解析 "First name"

R:改进嵌套 ifelse 语句和多种模式

C# - 多个 if else 语句无法正常工作

java - 如何使用 Spring RestTemplate 将 List 或 String 数组传递给 getForObject

java - 仅向拥有商业公司邮件地址的人员授予通过 Google oAuth 的访问权限

css - 带有 ID 的测试变量标题 - selenium ide

java - 如果用户输入错误答案,如何跳回到 if 语句的开头

java - 如何用动态变化的简单数组创建多维数组?

java - XSD 到 JAVA 到 XML