【问题标题】:Selenium : Waiting till Progress bar process is completed in the UI (wait till for Progress bar might vary test to another test)Selenium:等待 UI 中的进度条过程完成(等待进度条可能会因测试而异)
【发布时间】:2020-01-12 11:13:42
【问题描述】:

我正在使用 selenium 进行 UI 自动化,我想等到进度条完成,在进度条完成后我想执行一些其他操作。我已经尝试过 WedDriverWait,找到的元素不可见但它不起作用。请提出一些使用 C# Selenium 处理进度条的方法。

我已经尝试过 waitUntil Invisibility of element located function for the Particular Xpath

 WebDriverWait wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(900000000));
                                wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath(buildProgressStringExp)));

string buildProgressStringExp = "//table[@class='pane stripped']/tbody//descendant::tr[@class='build-row transitive single-line overflow-checked']//table[@class='progress-bar ']"

我已经尝试了下面的代码,但它仍然没有工作。它一直等到特定的时间限制,然后抛出异常

        {while(true)
                {
                    try
                    {
                        IWebElement el = driver.FindElement(By.XPath("Xpath of bar"));
                        bool abc = el.GetAttribute("style").Equals("width:100%;");
                        if (abc)
                        {
                            return true;
                        }
                    }
                        catch(StaleElementReferenceException exp)
                    {
                            return true;
                        }
                    Thread.Sleep(1000);
                    }
                }```

【问题讨论】:

  • 在构建结束时,屏幕上的一些状态会发生变化,比如小圆圈会变成红色或绿色对吗?或者那个“x”图标也会消失,您是否尝试将它们用作等待的基础?
  • 请花一点时间来修复代码上的缩进。另外,请阅读为什么a screenshot of code is a bad idea。粘贴代码并正确格式化它。页面截图很好,但 HTML 截图不行。你知道你等待的时间是 28.5 年,对吧?这似乎有点傻,不是吗?
  • @Spencer ,在构建结束时,第一个元素的 xpath 会发生变化,x 图标也会消失。我都试过了,但还是不行。
  • @Jeff 我会这样做的 thnks 建议 ????。我知道我等待的时间太长了,这很愚蠢,而且它也是一个测试代码,但仍然有这个等待时间,驱动程序并没有等到状态栏上的元素消失。

标签: c# selenium automation progress-bar webautomation


【解决方案1】:

元素可能不符合预期条件所使用的标准。下面是InvisibilityOfElementLocated的代码

/// <summary>
/// An expectation for checking that an element is either invisible or not present on the DOM.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns><see langword="true"/> if the element is not displayed; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> InvisibilityOfElementLocated(By locator)
{
    return (driver) =>
    {
        try
        {
            var element = driver.FindElement(locator);
            return !element.Displayed;
        }
        catch (NoSuchElementException)
        {
            // Returns true because the element is not present in DOM. The
            // try block checks if the element is present but is invisible.
            return true;
        }
        catch (StaleElementReferenceException)
        {
            // Returns true because stale element reference implies that element
            // is no longer visible.
            return true;
        }
    };
}

如果页面没有改变,它就不会过时,如果搜索找到它但它是隐藏的另一种显示永远不会返回 false 的方式,这永远不会工作。

您可能需要编写自己的 func 才能使用您的页面。

【讨论】:

  • 坦克凯恩响应,但这些是内置功能,有什么办法更新吗?
  • @Zoso619 我不确定我是否关注。 InvisibilityOfElementLocated 是一种现有方法,但您可以为 .Until 编写自己的函数,以使用特定于您想要的条件的函数。也可以在任何时候使用 Wait.Until 设置最大时间,之后将引发异常。如果进度条花费的时间比 x 长,则等待失败。
  • 试过这个,它对我不起作用。实际上,驱动程序在抛出 Stale Element 异常之前等待 5-6 分钟。不知道为什么它不等到 UI 中的栏不可见。它的等待但不超过 5-6 分钟。对于需要较长时间的测试用例失败。
猜你喜欢
  • 2022-01-21
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多