【问题标题】:How do I wait for an element using the WinAppDriver in UWP?如何在 UWP 中使用 WinAppDriver 等待元素?
【发布时间】:2019-07-11 16:38:35
【问题描述】:

我目前正在使用 WinAppDriver 将 UWP 应用程序的编码 UI 测试迁移到 Appium,我遇到了这个问题,我等不及要显示一个元素。没有办法像 Microsoft 的 Coded UI Test 那样等待元素“准备好”。

ClassInitialize 方法中一切正常(在登录视图中输入数据)并单击登录按钮。触发点击事件后,应用程序会显示一个进度条,直到用户登录。我的问题是登录过程后我无法等待组件。

我找到了一些代码 sn-ps,但是,它们似乎对我不起作用。这是我目前使用的扩展方法:

public static IWebElement WaitForElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
   if (timeoutInSeconds > 0){
      driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeoutInSeconds);
      var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
      return wait.Until(ExpectedConditions.ElementIsVisible(by));
   }
   return driver.FindElement(by);
}

我还了解到必须设置 Windows 驱动程序的隐式超时:

session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);

并在 WaitForElement 方法中被覆盖,这对我也不起作用。

Waiting for element before clicking with WinAppDriver

[TestMethod]
public void UploadDocuments()
{
   var UploadButton = session.WaitForElement(By.XPath("//Button[@AutomationId='AddDocument']"), 60);
   UploadButton.Click();

   session.FindElementByXPath("//ToolbarWindow32[@AutomationId='1001']").SendKeys(Keys.Control + "a");
   session.FindElementByXPath("//ToolbarWindow32[@AutomationId='1001']").SendKeys(testFilesFolder);

   //session.FindElementByName("Open").Click();
}

测试通常在使用ClassInitialize 完成后在第一行崩溃。所以我想在测试继续之前等待'AddDocument'按钮弹出。

如果有人有解决方案,我将不胜感激。 谢谢!

【问题讨论】:

    标签: selenium uwp appium coded-ui-tests winappdriver


    【解决方案1】:

    您可以像这样实现等待功能:

    public WindowsElement GetElementByAutomationID(string automationId, int timeOut = 10000)
    {
        WindowsElement element = null;
    
        var wait = new DefaultWait<WindowsDriver<WindowsElement>>(Driver)
        {
            Timeout = TimeSpan.FromMilliseconds(timeOut),
            Message = $"Element with automationId \"{automationId}\" not found."
        };
    
        wait.IgnoreExceptionTypes(typeof(WebDriverException));
    
        try
        {
            wait.Until(Driver =>
            {
                element = Driver.FindElementByAccessibilityId(automationId);
                return element != null;
            });
        }
        catch (WebDriverTimeoutException ex)
        {
            LogSearchError(ex, automationId);
            Assert.Fail(ex.Message);
        }
    
        return element;
    }
    

    您的问题似乎是 appium-dotnet-driver 问题。 在 github 上查看这些问题: https://github.com/Microsoft/WinAppDriver/issues/329

    https://github.com/appium/appium-dotnet-driver/issues/225

    【讨论】:

    • 这个时候答案不起作用。如果您将wait.IgnoreExceptionTypes(typeof(WebDriverException)); 更改为wait.IgnoreExceptionTypes(typeof(InvalidOperationException));,您的等待超时将再次起作用。原因是,当调用FindElementByAccessibilityId 时,当找不到元素并且NOT WebDriverException 时,你会得到InvalidOperationException
    • @krs 感谢此更新。我所知道的是,它在撰写该答案时曾经有效。 - 在这里做一个假设 - 也许这在最近版本的 selenium 框架中有所改变?
    • @PixelPlex 对我来说,当使用异常类型 - WebDriverException 或 InvalidOperationException 时,该函数会立即返回错误“使用给定的搜索参数无法在页面上找到一个元素”并且不会等待超时
    猜你喜欢
    • 2020-05-02
    • 2019-09-30
    • 1970-01-01
    • 2021-01-28
    • 2021-06-15
    • 2016-11-05
    • 2020-05-20
    • 2012-07-28
    • 2020-02-13
    相关资源
    最近更新 更多