【问题标题】:How to apply wait for an element using WinAppDriver on UWP in C#如何在 C# 中的 UWP 上使用 WinAppDriver 应用等待元素
【发布时间】:2020-05-02 05:13:51
【问题描述】:

我正在使用带有 C# 的 WinAppDriver 实现 UWP 应用程序的自动化,但在某些时候我的测试失败了,因为某些元素需要时间才能出现,并且我无法应用 waitDriverWait(猜 WAD 没有这个)所以我发现下面的代码正在工作,但想知道是否有更好的方法?

 public static void WaitForElement(this WindowsDriver<WindowsElement> driver, string IDType, string elementName, int time = 10000)
    {
        var wait = new DefaultWait<WindowsDriver<WindowsElement>>(BasePage.WindowsDriver)
        {
            Timeout = TimeSpan.FromSeconds(time),
            PollingInterval = TimeSpan.FromSeconds(0.5)
        };

        wait.IgnoreExceptionTypes(typeof(InvalidOperationException));

        wait.Until(driver1 =>
        {
            int elementCount = 0;
            switch (IDType)
            {
                case "id":
                    elementCount = driver1.FindElementsByAccessibilityId(elementName).Count;
                    break;
                case "xpath":
                    elementCount = driver1.FindElementsByXPath(elementName).Count;
                    break;
                case "name":
                    elementCount = driver1.FindElementsByName(elementName).Count;
                    break;
            }
            return elementCount > 0;
        });
    }

【问题讨论】:

    标签: c# uwp automation appium winappdriver


    【解决方案1】:

    我过去曾成功使用 WebDriverWait 与我的 WinAppDriver 自动化 - 您必须安装 DotNetSeleniumExtras.WaitHelpers 才能访问 ExpectedConditions 类,因为它现在在标准 Selenium 命名空间中已过时,但我没有看不到任何阻止您使用WebDriverWait 的东西。

    等待元素文本被填充一个值:

    var Driver = new WindowsDriver<WindowsElement>();
    var myElement = Driver.FindElementByAccessibilityId("someId");
    
    var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
    wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.TextToBePresentInElement(myElement, "text"));
    

    等待显示元素:

    var Driver = new WindowsDriver<WindowsElement>();
    var myElement = Driver.FindElementByAccessibilityId("someId");
    
    new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => myElement.Displayed);
    

    等待元素存在,使用 FindElementsBy.Name 的另一种方法:

    var Driver = new WindowsDriver<WindowsElement>();
    
    new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => d.FindElements(By.Name("someName")).Count > 0);
    

    如前所述,我过去曾使用这些确切的方法等待WinAppDriver,并成功了。请让我知道您在使用这些方法时遇到了什么问题。

    【讨论】:

    • 嗨,克里斯汀,感谢您非常有帮助的回复 - 我已经尝试过:var myElement = Driver.FindElementByAccessibilityId("someId");新的 WebDriverWait(驱动程序,TimeSpan.FromSeconds(30)).Until(d => myElement.Displayed);但是我在第一行立即遇到异常“使用给定的搜索参数无法在页面上找到一个元素”,即它无法存储该元素,因为它不存在......这种方法仍然适用于你吗?我在 StackOverflow 上提出了一些关于这个问题的问题——但没有人回应。这可能在 WinAppDriver 中被破坏了吗?
    • 如果调用Driver.FindElementByAccessibilityId 时第一行失败,则最可能的问题是UI 元素与您用来定位它的选择器不匹配。因为我看不到您正在处理的 UI,您可以在 Accessibility Insights(或您使用的任何检查程序)中检查该元素,并将其检查属性发布在您的原始问题中。
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2021-01-28
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2014-07-05
    相关资源
    最近更新 更多