如何在UWP中使用WinAppDriver等待元素? [英] How do I wait for an element using the WinAppDriver in UWP?

查看:311
本文介绍了如何在UWP中使用WinAppDriver等待元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用 WinAppDriver 将UWP应用的编码UI测试迁移到Appium.我遇到了这个问题,我等不及要出现一个元素.无法像Microsoft的编码UI测试那样等待元素就绪".

I'm currently migrating my Coded UI Tests for a UWP app to Appium using the WinAppDriver and I have come across the issue, that I can't wait for an element to show up. There is no way to wait for a element to be "ready" as the Coded UI Test from Microsoft did.

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

In the ClassInitialize method everything works fine (data is entered on the login view) and the login button is clicked. After the click event is triggered the app shows a progress bar until the user is logged in. My problem is that I cannot wait for the components after the login process.

我找到了一些代码片段,但是它们似乎不适用于我.这是我当前正在使用的扩展方法:

I have found some code snippets, however, they don't seem to work for me. Here is the extension method I'm currently using:

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驱动程序设置隐式超时:

I have also read that the implicit timeout for the Windows driver has to be set:

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

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

and overridden in the WaitForElement method, which also didn't work for me.

在使用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"按钮弹出,然后再进行测试.

The test usually crashes in the first line after it's finished with the ClassInitialize. So I would like to wait for the 'AddDocument' button to pop up before the test carries on.

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

If anyone has a solution I'd appreciate the help. Thanks!

推荐答案

您可以实现以下等待功能:

You can implement wait funcionalities like this:

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驱动程序问题. 有关此问题,请参见github上的以下问题: https://github.com/Microsoft/WinAppDriver/issues/329

Your problem seems to be a appium-dotnet-driver issue. See these issues on github about this: https://github.com/Microsoft/WinAppDriver/issues/329

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

这篇关于如何在UWP中使用WinAppDriver等待元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆