首页 > 解决方案 > Selenium - 未正确检查断言

问题描述

我在使用 Selenium + C# 编写的测试时遇到问题。示例测试:

    [Test]
    [Description("Cashback - Checks whether Cashback field shows correct amount of money which needs to be returned to the customer")]
    public void CachbackFieldIsCountedCorrectly()
    {
        LoginHelpers.LogInUser(driver);

        var mainPosPage = new MainPosPage(driver);
        mainPosPage.WorkSpaceComponent.ProductGalleryComponent.LatteElement.Click();
        mainPosPage.PaymentOptionsComponent.CashElement.Click();
        mainPosPage.WorkSpaceComponent.CashNumpadComponent.Click(5).Click(0).Click(0).Click(NumpadCommand.Pay);

        Assert.AreEqual("Cashback 1.50", mainPosPage.StickyInfoComponent.CashbackElement.Text);
        Assert.IsNotNull(mainPosPage.StickyInfoComponent.CashbackElement);
        Assert.AreEqual("0.00€", mainPosPage.BasketComponent.TotalAmountElement.Text);
        Assert.IsTrue(mainPosPage.CategoriesBarComponent.IsTopCategoryElementActive, "POS was not redirected to the 'TOP' category");
        Assert.IsTrue(mainPosPage.BasketComponent.IsBasketEmpty, "Basket is not cleared after successfull payment");

Xpath selector: private static readonly By totalAmountLocator = By.XPath("//div[@class='total-container']//div[2]");

我已经实现了 Page 对象模型,并且在我的本地 PC 上一切正常,每个测试都标记为通过。但是,由于我已经从虚拟机实现了 CI 和夜间构建/测试,因此我面临着断言问题。似乎断言是在第二阶段(动作)之后“立即”完成的,即使我知道它在本地运行(而不是 VSTS)时它可以正常工作,结果也被标记为失败。错误喜欢:

String lengths are both 5. Strings differ at index 0.
Expected: "0.00€"
But was: "3.50€"
-----------^

OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class = 'sticky-info error']"}
(Session info: content shell=)
(Driver info: chromedriver=2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e),platform=Windows NT 10.0.17134 x86_64)

我需要以某种方式“强制”我的测试来等待断言,我已经尝试了两种方式:隐式和 thread.sleep 方式,但它仍然被打破。

断言应该如何以正确的方式编写?现在我收到的信息是:“元素没有建立......”。即使想,我知道它工作正常......

标签: c#seleniumselenium-webdriverselenium-chromedriver

解决方案


推荐阅读