【问题标题】:Check if element exists appium C#检查元素是否存在appium C#
【发布时间】:2018-06-07 18:53:07
【问题描述】:

我已经进行了许多单元测试,并且正在使用 appium 运行它们。当出现错误时,我尝试通过

捕获它
if (PowerPointSession.FindElementByAccessibilityId("CreateErrorIcon").Displayed == true)
{
    exception = "An error occured when creating Agenda/TOC";
    ExceptionHandler(exception);
}

如果我有一个错误并且它找到一个名为CreateErrorIcon 的元素,这会很好。但是,如果没有弹出错误,它似乎会卡在 if 语句的开头,就好像它正在检查要显示的名为 CreateErrorIcon 的元素,并且由于某种原因它永远不会退出 if 语句,如果它没有找到元素。

有没有人有任何想法或可以指出正确的方向来解释为什么会发生这种情况?

【问题讨论】:

    标签: c# appium


    【解决方案1】:

    This post 回答您的问题。 基本上,FindElementByAccessibilityId 一直等到找到一个元素或发生超时。

    在 C# 中,检查类似于:

    private static bool IsElementPresent(WindowsDriver<AppiumWebElement> driver, Action findAction)
    {
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
        try
        {
            findAction();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            driver.Manage().Timeouts().ImplicitlyWait(GuiTestRunner.Timeout);
        }
    }
    

    及用法:

    var isError = IsElementPresent(PowerPointSession, 
        () => PowerPointSession.FindElementByAccessibilityId("CreateErrorIcon"));
    if (isError) 
    {
        exception = "An error occured when creating Agenda/TOC";
        ExceptionHandler(exception); 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多