c# - 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 方式,但它仍然被打破。
断言应该如何以正确的方式编写?现在我收到的信息是:“元素没有建立......”。即使想,我知道它工作正常......
解决方案
推荐阅读
- javascript - 为什么当我对 src URL 进行硬编码时会显示我的 img,而当我以编程方式分配它时却没有?
- git - 不小心从我的电脑上删除了我的项目
- postgresql - PostgreSQL 使用 pg_trgm 比全扫描慢
- java - 调用构造函数并初始化数据库名称
- ios - 为什么我无法从 Apple iOS Contacts Framework 的默认容器中检索组?
- c# - C# 无法绑定 EmailMessage
- scheme - 如何在 DrRacket 中执行操作顺序
- image - 如何修复我当前的图表没有在我的用户表单中导出其文件名?每次我必须重命名图表才能工作
- javascript - 如何使 InnerHTML 淡出
- git - 我的 Github Action 的工作流程完成后,如何运行 GitHub Webhook?