gpt4 book ai didi

c# - 在使用 WinAppDriver 单击之前等待元素

转载 作者:行者123 更新时间:2023-12-03 23:12:28 25 4
gpt4 key购买 nike

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

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

session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);

我期望这意味着它会在抛出与元素识别相关的错误之前至少等待 60 秒,例如
Message: System.InvalidOperationException : An element could not be located on the page using the given search parameters.

然而,这种情况并非如此。尝试调用以下命令时,我在大约 2 秒内收到错误消息。
WindowsElement btn = session.FindElementByXPath("//Button[@Name='NEXT']");
btn.Click();

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

最佳答案

那里有一个 open issue在 winappdriver github 上。看看this对此发表评论。这似乎是一个 Appium 问题。我不知道这个问题的状态。

基本上,这意味着您将不得不求助于解决方法。使用 Thread.Sleep(/*milliseconds*/)bad idea .

我实现了一个 while在函数中循环以通过自动化 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;
}

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;
    }

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

    关于c# - 在使用 WinAppDriver 单击之前等待元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117645/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com