c# - 如果一段时间后值发生变化,则使用 WebDriverWait 等待

标签 c# selenium webdriverwait

一段时间后,我正在使用 WebDriverWait 获取 WebElement 的值,但如果它没有改变,我不希望它失败,只需获取当前值并继续执行即可。

我正在 Web 中运行一个进程,该进程有一个“进程已完成”标签,我用它来检查它是否完成。在同一个屏幕上,我需要阅读该过程的“运行时间”,以报告运行该过程所花费的时间。 问题是即使在“进程已完成”标 checkout 现后,后端进程仍在运行,并且时间会更新。我不知道这个“后端更新”需要多少时间,我不知道在快乐运行中它是否会出现在提到的标签之前,目前,从我的测试来看它从 10 到 40 秒(60 到请确定)。

我们有一个 Waits 类来处理这类事情,但我们没有验证文本更改的方法,所以我想到了这个:

private static WebDriverWait _customWait(int value) => new WebDriverWait(
    clock,
    Driver.Instance,
    TimeSpan.FromSeconds(value),
    TimeSpan.FromMilliseconds(Settings.Timeouts.SleepIntervalInMillis));

public static void WaitForTextElementToChange(Func<IWebDriver, IWebElement> elementFinder, IWebDriver driver, int time)
{
    string oldValue = elementFinder.Invoke(driver).Read();
    _customWait(time).Until(drv =>
    {
        try
        {
            return !elementFinder.Invoke(driver).Read().Contains(oldValue);
        }
        catch (NotFoundException)
        {
            return true;
        }
        catch (StaleElementReferenceException)
        {
            return true;
        }
    });
}

这行得通。我评论它是因为我还不完全理解 Until 方法背后的语法和逻辑,我知道它给出了一个 WebDriverTimeoutException 并且我就这样离开了框架的附加方法。

所以,如果值发生变化,它就会退出并继续运行,但在这种特殊情况下,如果它没有变化,我不需要它来抛出异常,所以我在 try 中调用了它/ catch

Waits.WaitForProcessToFinish(drv => processCompleteLabel); //<- context code
try
{ 
    //If value changes before 60 secs everything is fine
    Waits.WaitForTextElementToChange(drv => timeElement, someDriverObject, 60); 
    //60 is a hardcoded int value just for testing
}
catch (WebDriverTimeoutException)
{ 
    //But if it doesn't change just do nothing 
}
string timeElapsed = timeElement.Read(); //<- context code

我的问题是,这样可以吗? 你会怎么做?

最佳答案

根据我在源代码中看到的内容,我认为 Until 没有办法解决这个问题作品。

Until 方法在循环中执行传递给它的方法,直到返回 bool 或对象或发生超时。

public virtual TResult Until<TResult>(Func<T, TResult> condition)
{
    if (condition == null)
    {
        throw new ArgumentNullException("condition", "condition cannot be null");
    }

    var resultType = typeof(TResult);
    if ((resultType.IsValueType && resultType != typeof(bool)) || !typeof(object).IsAssignableFrom(resultType))
    {
        throw new ArgumentException("Can only wait on an object or boolean response, tried to use type: " + resultType.ToString(), "condition");
    }

    Exception lastException = null;
    var endTime = this.clock.LaterBy(this.timeout);
    while (true)
    {
        try
        {
            var result = condition(this.input);
            if (resultType == typeof(bool))
            {
                var boolResult = result as bool?;
                if (boolResult.HasValue && boolResult.Value)
                {
                    return result;
                }
            }
            else
            {
                if (result != null)
                {
                    return result;
                }
            }
        }
        catch (Exception ex)
        {
            if (!this.IsIgnoredException(ex))
            {
                throw;
            }

            lastException = ex;
        }

        // Check the timeout after evaluating the function to ensure conditions
        // with a zero timeout can succeed.
        if (!this.clock.IsNowBefore(endTime))
        {
            string timeoutMessage = string.Format(CultureInfo.InvariantCulture, "Timed out after {0} seconds", this.timeout.TotalSeconds);
            if (!string.IsNullOrEmpty(this.message))
            {
                timeoutMessage += ": " + this.message;
            }

            this.ThrowTimeoutException(timeoutMessage, lastException);
        }

        Thread.Sleep(this.sleepInterval);
    }
}

您正在做的工作有效,但我建议您在不使用 Wait.Until() 的情况下拥有自己的超时循环,因为它的核心功能是在出现问题时抛出异常。

关于c# - 如果一段时间后值发生变化,则使用 WebDriverWait 等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861068/

相关文章:

javascript - AngularJS - MVC ASP.NET 目录结构

c# - 每1秒自动发送一次消息

c# - 正则表达式 C# - 如何匹配类的方法和属性名称

java - 如何在 webdriver 中捕获页面发出的所有请求? Browsermob有什么替代品吗?

java - 如何检查元素是否是可点击的按钮而不是文本?

python - 如何使用 Selenium 和 Python 从下拉菜单中选择选项

c# - 索引超出范围抛出但索引并没有真正超出范围

javascript - 有/没有 Selenium 运行 Protractor 的区别?

java - 如何使用 Java 通过 Selenium WebDriver 处理 fancybox 弹出窗口

Selenium Webdriver 等待很长时间