【问题标题】:How can the timeout time be logged when using wait.until with selenium?使用带有 selenium 的 wait.until 时如何记录超时时间?
【发布时间】:2019-09-10 10:16:21
【问题描述】:

目前我正在使用以下 wait.until 代码:

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

我希望能够识别这段代码花费时间最长的场景。我在多个地方使用此代码,但有没有一种方法可以通过 selenium 记录超时,而无需在调用此代码之前/之后添加秒表?

【问题讨论】:

  • 你设置了超时。您是否在询问将超时设置为 30 秒并在 15 秒完成并且您想要记录 15 或?请添加一个示例,因为现在很难说出您的要求。

标签: c# selenium selenium-webdriver specflow webdriverwait


【解决方案1】:

我想出的最佳方法是扩展方法:

    public static IWebElement LoggedUntil(this WebDriverWait wait, Func<IWebDriver, IWebElement> condition)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        try
        {
            return wait.Until(condition);
        }
        finally
        {
            watch.Stop();
            var elapsedSeconds = watch.Elapsed.TotalSeconds;
            Console.WriteLine($"Wait took: {elapsedSeconds} Seconds");
        }
    }

在此之后,我可以将所有使用 .Until 的引用更改为使用 .LoggedUntil

   protected IWebElement FindElement(By by)
   {
        Console.WriteLine($"Find {by}");
        return Wait.LoggedUntil(ExpectedConditions.ElementExists(by));
   }

【讨论】:

    【解决方案2】:

    您不测量 超时,而是配置 TimeOut

    WebDriverWaitExpectedConditions 一起被诱导等待所需的元素是:

    • present
    • visibe
    • interactable

    WebDriverWait类的构造函数是:

    • WebDriverWait Constructor (IWebDriver, TimeSpan) 定义为:

      public WebDriverWait(
          IWebDriver driver,
          TimeSpan timeout
      )
      
      Parameters:
      driver
          Type: OpenQA.Selenium.IWebDriver
          The WebDriver instance used to wait.
      timeout
          Type: System.TimeSpan
          The timeout value indicating how long to wait for the condition.
      
    • WebDriverWait Constructor (IClock, IWebDriver, TimeSpan, TimeSpan) 定义为:

      public WebDriverWait(
          IClock clock,
          IWebDriver driver,
          TimeSpan timeout,
          TimeSpan sleepInterval
      )
      
      Parameters
      clock
          Type: OpenQA.Selenium.Support.UI.IClock
          An object implementing the IClock interface used to determine when time has passed.
      driver
          Type: OpenQA.Selenium.IWebDriver
          The WebDriver instance used to wait.
      timeout
          Type: System.TimeSpan
          The timeout value indicating how long to wait for the condition.
      sleepInterval
          Type: System.TimeSpan
          A TimeSpan value indicating how often to check for the condition to be true.
      

    timeoutsleepInterval 几乎是可配置的,应该根据 Test Specifications 进行配置,因为所需的元素需要是 时间跨度内>呈现可见可交互

    【讨论】:

      【解决方案3】:

      作为扩展方法的替代方法,您可以创建一个实现 IWait&lt;IWebDriver&gt; 的类来烘焙持续时间的计时和日志记录。

      WebDriverWait 类继承自 DefaultWait,它实现了 IWait 接口(确切地说是 IWait)。您可以嵌套“等待”对象。测量持续时间的“等待”对象只需要实现 IWait 接口即可:

      public class TimeableWebDriverWait : IWait<IWebDriver>
      {
          private readonly Action<string> logger;
          private readonly IWait<IWebDriver> wait;
      
          public string Message
          {
              get => wait.Message;
              set => wait.Message = value;
          }
      
          public TimeSpan PollingInterval
          {
              get => wait.PollingInterval;
              set => wait.PollingInterval = value;
          }
      
          public TimeSpan Timeout
          {
              get => wait.Timeout;
              set => wait.Timeout = value;
          }
      
          public TimeableWebDriverWait(IWait<IWebDriver> wait, Action<string> logger = null)
          {
              this.wait = wait ?? throw new ArgumentNullException(nameof(wait));
              this.logger = logger ?? Console.WriteLine;
          }
      
          public void IgnoreExceptionTypes(params Type[] exceptionTypes)
          {
              wait.IgnoreExceptionTypes(exceptionTypes);
          }
      
          public TResult Until<TResult>(Func<T, TResult> condition)
          {
              var watch = System.Diagnostics.Stopwatch.StartNew();
      
              try
              {
                  return wait.Until<TResult>(condition);
              }
              finally
              {
                  watch.Stop();
      
                  logger($"Wait took: {watch.Elapsed.TotalSeconds} Seconds");
              }
          }
      }
      

      现在您可以将它与 any WebDriverWait 对象一起使用:

      var realWait = new WebDriverWait(driver, 30);
      var wait = new TimeableWebDriverWait(realWait);
      
      wait.Until(...);
      

      您可以指定自己的函数来记录消息,只要它接受字符串参数并具有void 返回类型:

      var realWait = new WebDriverWait(driver, 30);
      var wait = new TimeableWebDriverWait(realWait, System.Diagnostic.Trace.WriteLine);
      
      wait.Until(...);
      

      【讨论】:

        猜你喜欢
        • 2019-10-30
        • 2022-01-21
        • 1970-01-01
        • 2020-11-07
        • 1970-01-01
        • 2012-11-18
        • 2017-09-01
        • 2017-12-06
        • 1970-01-01
        相关资源
        最近更新 更多