如何在一个测试中断言对web元素的多个更改

本文关键字:元素 web 断言 测试 一个 中断 | 更新日期: 2023-09-27 18:03:46

如何断言事件发生后页面上发生了多个更改?

我想断言的行为示例:

  1. 点击某个按钮
  2. 主菜单崩溃
  3. 出现其他元素

我想断言所有这些事情都在我点击按钮后发生了。我真的必须编写相同的方法,每个方法都对这些变化进行断言吗?

如何在一个测试中断言对web元素的多个更改

为什么需要为每个断言编写更多的方法?你可以只使用一个方法来检查元素是否可见,并为每个元素调用该方法一次。

private boolean IsElementVisible(By by)
{
    try
    {
        return driver.FindElement(by).IsDisplayed();
    }
    catch(NoSuchElementException e)
    {
        return false;
    }
}
private List<By> VerifyElementVisibility(
          List<By> expectedVisibile, List<By> expectedInvisible)
{
    List<By> failedExpectations = new List<By>();
    for (By by in expectedVisible)
    {
        if(!IsElementVisible(by)
        {
            failedExpectations.Add(by);
        }
    }
    for (By by in expectedInvisible)
    {
        if(IsElementVisible(by)
        {
            failedExpectations.Add(by);
        }
    }
    return failedExpectations;
}
List<By> failedExpectations = VerifyElementVisiblity(visibleElements, invisibleElements);
Assert.IsTrue(failedExpectations.Count == 0,
     failedExpectations.Count.ToString() + " elements did not meet expectations");