如何使用 Selenium WebDriver 断言十进制 Excel 值与 Web 值?

How to assert decimal Excel value with the web value using Selenium WebDriver?

Excel 文件中提到的值是 21.650 并且页面上显示相同的值。但是当脚本执行时,它将 Excel 值读取为 21.65 并且我的断言条件变为 false.

List<string> rowValue = new List<string> { };
var ExcelFilePath = "D:\testexcel.xlsx";

Excel.Application xlApp = new Excel.Application(ExcelFilePath);
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(ExcelFilePath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

int rowCount = xlRange.Rows.Count;

for (int i = 1; i <= rowCount; i++)
{
    rowValue.Add(xlRange.Cells[i, j].Value2.ToString());

    IWebElement element = driver.FindElement(By.Id(weight_field));
    ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);

    string weight = driver.FindElement(By.Id(weight_field)).Text;

    if (mpuid == i)
    {  
        Assert.AreEqual(weight,rowValue[0]);
        Console.WriteLine(" Expected Value: " + rowValue[0] + " is equal to Actual Value: " + weight);
        break;
    }

    rowValue.Clear();
}

在您的 Excel 文件中,将您的单元格类别更改为文本。

按照 This SO question 的回答中的建议使用 DataFormatter class。因为这将 return 显示 excel 文件中的确切文本。