我遇到了用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方式,但仍然无法解决。
应该如何以正确的方式编写断言?现在,我收到的信息是:“元素未建立...”。甚至以为,我知道它可以正常工作...