使用 WinAppDriver 单击之前等待元素

Tre*_*opz 4 c# winappdriver

我有一个如此微不足道的问题,但我很难让我的代码在继续之前正确等待对象。

我为我的驱动程序设置了以下配置

session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
Run Code Online (Sandbox Code Playgroud)

我原以为这意味着它会等待至少 60 秒,然后才会抛出与元素识别相关的错误,例如

Message: System.InvalidOperationException : An element could not be located on the page using the given search parameters.
Run Code Online (Sandbox Code Playgroud)

然而,这种情况并非如此。当我尝试调用以下命令时,大约 2 秒后出现错误。

WindowsElement btn = session.FindElementByXPath("//Button[@Name='NEXT']");
btn.Click();
Run Code Online (Sandbox Code Playgroud)

该错误是在我刚刚定义按钮属性的行上引发的,而不是在实际的 Click() 方法上引发的。我没有正确传递元素属性吗?为什么按钮的实例化也会对其进行搜索?

Pix*_*lex 5

winappdriver github 上有一个未解决的问题。看看这个关于它的评论。看来是Appium的问题。我不知道这个问题的现状。

基本上,这意味着您将不得不采取解决方法。使用Thread.Sleep(/*milliseconds*/)是一个坏主意

while在函数中实现了一个循环,以通过 Automation ID 获取控件,如下所示:

    /// <summary>
    /// Gets a UI element based on a AutomationId.
    /// </summary>
    /// <param name="automationId">The AutomationId is a unique value that can be found with UI inspector tools.</param>
    /// <param name="controlName">The name of the UI element.</param>
    /// <param name="timeOut">TimeOut in milliseconds</param>
    /// <returns></returns>
    protected WindowsElement GetElement(string automationId, string controlName, int timeOut = 10000)
    {
        bool iterate = true;
        WindowsElement control = null;
        _elementTimeOut = TimeSpan.FromMilliseconds(timeOut);
        timer.Start();

        while (timer.Elapsed <= _elementTimeOut && iterate == true)
        {
            try
            {
                control = Driver.FindElementByAccessibilityId(automationId);
                iterate = false;
            }
            catch (WebDriverException ex)
            {
                LogSearchError(ex, automationId, controlName);
            }
        }

        timer.Stop();
        Assert.IsFalse(timer.Elapsed > _elementTimeOut, "Timeout Elapsed, element not found.");
        timer.Reset();

        return control;
    }
Run Code Online (Sandbox Code Playgroud)

Thread.Sleep()与简单地阻止代码执行相比,使用循环有一些优点,它更灵活,并且您有更多的选择。

一些优点:

  • 您的测试脚本继续执行:想象一下您的脚本暂停 5 秒,而被测试的应用程序继续运行。在这 5 秒内可能会发生很多您的脚本可能想知道的事情。但它不能,因为如果您使用“Thread.Sleep()”,代码执行将被阻止。
  • 动态等待: while 循环将迭代,直到满足条件。这使得您的脚本在满足此条件后立即继续测试,从而使您的脚本运行得更快。例如,您正在等待页面加载。Thread.Sleep(5000)假设可以继续,而循环知道可以继续测试。
  • 使用计时器/超时组合,您可以检查操作花费了多长时间(例如保存一些编辑),如果花费的时间超过超时,您就知道不能继续。

或者,此代码也可以正常工作:

protected WindowsElement GetElement(string automationId, string propertyName, 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, propertyName);
        Assert.Fail(ex.Message);
    }

    return element;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码只会抛出 aWebDriverTimeoutException而不是连续抛出NoSuchElementException。它不使用 while 循环,但我怀疑wait.Until(...)正在做类似的事情,因为 WinAppDriver 每 500 毫秒轮询一次 gui(请参阅对象PollingInterval上的属性DefaultWait)。