【问题标题】:How can I catch whether the element is not visible如何捕捉元素是否不可见
【发布时间】:2020-02-02 09:41:30
【问题描述】:

我正在尝试创建一个脚本,我可以在其中检查该元素是否显示在 UWP 应用程序上。我有一个方法可以检查元素是显示还是不显示..

如果元素实际显示并且我调用了“IsElementDisplayed”,测试似乎工作正常并继续执行测试。但是当元素真的没有显示并且我调用了“IsElementDisplayed”时,它没有返回错误的布尔值..但它只是停止测试执行并说,“没有找到这样的给定参数”......

请检查我的示例代码....

我有一个包含我的定位器并返回 WindowsElement 实例的类:

    protected WindowsElement Id(string id)
    {
        return Driver.FindElementById(id);
    }

    protected WindowsElement XPath(string xpath)
    {
        return Driver.FindElementByXPath(xpath);
    }

    protected WindowsElement Name(string name)
    {
        return Driver.FindElementByName(name);
    }

    protected WindowsElement AccessibilityId(string id)
    {
        return Driver.FindElementByAccessibilityId(id);
    }

然后我有 Page 类,其中包含我的元素的属性.. 下面的示例代码:

    public WindowsElement SaveObligation_Btn => this.AccessibilityId("OBLGTN_saveObligation_btn");

    public WindowsElement CancelObligation_Btn => this.AccessibilityId("OBLGTN_CancelObligation_btn");

    public WindowsElement ObligationAdd_Btn => this.AccessibilityId("SMMRY_AddObligation_btn");

最后我有一个 testhelper 类,其中包含以下方法:

    public bool IsNotDisplayed(WindowsElement element)
    {
        try
        {
            Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
            return false;
        }
        catch (Exception)
        {
            return true;
        }
    }

但是当我尝试调用“IsNotDisplayed”方法以在它捕获到任何异常时返回 false 时......,我的测试执行停止并且我将遇到一个指向我的 Locator 类的错误并说,“元素不是用给定的参数找到“...

我希望方法“isNotDisplayed”应该返回 false 布尔值,所以我可以验证元素是否可见。

【问题讨论】:

    标签: c# selenium testing appium winappdriver


    【解决方案1】:

    我不熟悉 C#。

    但是在 Python 中,我会这样写:

    try:
        element = //find your element
        if element.is_displayed():
            return True
        else:
            raise AssertionError
    except Exception:
        return False
    

    【讨论】:

      【解决方案2】:

      第一个问题是当noElementIsDisplayed 时你想做什么?当然,它会捕获一些异常。在您的场景中,如果您在抛出异常时不想做任何事情,那么您应该将 catch 块留空。 试试下面的代码:

      public bool IsNotDisplayed(WindowsElement element)
      {
          try
          {
              Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
              return false;
          }
          catch (Exception e)
          {
              // do nothing and it will continue your script.
      
          }
      }
      

      【讨论】:

      • 只有当你的函数返回 void 时,你的 catch 才会起作用。您的函数需要返回一个布尔值。
      【解决方案3】:

      如果元素显示则返回 true,否则返回 false。

      C#代码

      public bool IsNotDisplayed(WindowsElement element)
          {
              try
              {
                  element.Displayed;
      
              }
              catch (Exception)
              {
                  return false;
              }
              return true;
          }
      

      Java 代码

      public static boolean IsDispayed(WebElement webelement)
      {
          try {
              webelement.isDisplayed();
          } catch (Exception e) {
              return false;
          }
          return true;
      }
      

      Python 代码

      try:
          element = driver.find_element_by_xpath('XPATH')
          if element.is_displayed():
              return True
          else:
              raise AssertionError
      except Exception:
          return False
      

      【讨论】:

      • -1。您应该返回webelement.isDisplayed();,因为如果元素被隐藏,则不会抛出异常但不会显示该元素。
      • @AndiCover 感谢您的评论。我已更新我的代码以返回 webelement.isDisplayed(); 。请检查并告诉我
      • 更好但仍不是最佳的。只需在 try 块中 return 并删除 catchblock 之后的 then unreachable return 语句。在您的示例中,isDisplayed 方法将在成功的情况下被调用两次。
      • @AndiCover 方法的返回类型为布尔值,因此我无法删除最后一个返回语句
      【解决方案4】:

      试试下面的 c# 代码。这是另一种选择..

      private bool IsElementPresent(By by)
         {
             try
             {
                 driver.FindElement(by);
                 return true;
             }
             catch (NoSuchElementException)
             {
                 return false;
             }
         }
      

      【讨论】:

      • 这个答案显示了如何检查一个元素是否存在,但它没有提供关于是否显示 existing 元素的任何帮助......
      猜你喜欢
      • 2017-01-01
      • 2012-11-21
      • 2012-07-08
      • 2012-02-08
      • 1970-01-01
      • 2013-08-17
      • 2016-02-07
      • 2014-04-22
      • 2020-08-06
      相关资源
      最近更新 更多