【问题标题】:how to compare two numbers with selenium and java? (One number from CSV file and another in website)如何将两个数字与 selenium 和 java 进行比较? (一个来自 CSV 文件,另一个来自网站)
【发布时间】:2021-07-23 08:21:19
【问题描述】:

我写了一个程序来比较网站中的价格和一个带有 selenium 和 java 的 csv 文件,在程序的最后我写了断言来确认两个数字,但它有一个问题: 预期价格为 57,15
实际价格为 57.15

实际价格应该是57,15,我不知道该怎么写,你能帮我吗?

@Test(priority = 1)
void checkBeschädigung() throws FileNotFoundException {
    prepareTest();
    List<CsvPrice> stuff = readFromCSV("resources/price/BikePrice.csv");

    
    Select price = new Select(driver.findElement(By.id("coverageSum")));
    price.selectByIndex(1);
    driver.findElement(By.xpath("//*[@id=\"landingApp\"]/div[1]/div/div[2]/div/div/div/form/div[3]/div/div[2]/div[2]/div/label/input")).click();

    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    WebElement FirstPrice = driver.findElement(By.xpath("//*[@id='product-configurator-spinner']//parent::span"));
    String finalprice = FirstPrice.getText().split(" ")[0];
    System.out.println(finalprice);


    for (int i = 0; i <= 329; i++) {

        if (String.valueOf(stuff.get(i).getCoverageSum()).equals("250")
                && String.valueOf(stuff.get(i).getDurationStart()).equals("1") && stuff.get(i).getQualityName().equals("Diebstahl")
                && stuff.get(i).getDurationTimeUnit().equals("YEARS")) {
            System.out.println(stuff.get(i).getPriceBasePrice());
            Assert.assertEquals(finalprice, String.valueOf(stuff.get(i).getPriceBasePrice()));
            break;
        } else {
            System.out.println("that is wrong");
        }
    }

【问题讨论】:

    标签: java selenium automated-tests compare assert


    【解决方案1】:

    请在断言行之前尝试以下代码以更改“。”到“,”在您的字符串数据中:

    String actualValueWithComa = String.valueOf(stuff.get(i).getPriceBasePrice()).replaceAll(".",",");            
    Assert.assertEquals(finalprice, actualValueWithComa));
    

    【讨论】:

    • 谢谢你,但是我写了replace,我写了replace,它工作了。
    【解决方案2】:

    我建议您修改 CSV 文件中的测试数据。 如果您尝试替换“。”使用 ',' 在验证带有小数点的更高价值数据的案例时,您可能会遇到问题。 有些值可能用逗号分隔千位等。例如:5,10,000.50

    【讨论】:

      猜你喜欢
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2018-12-06
      相关资源
      最近更新 更多