简体   繁体   English

在使用显式等待之前关闭隐式等待,因为显式等待需要隐式等待的时间

[英]Turning Off Implicit Wait before using explicit wait as explicit wait takes time from Implicit wait

Scenairo: I am looking for error element after each operation(click/sendkeys).场景:我在每次操作(单击/发送键)后寻找错误元素。

Due to inconsistencies in the application i use both implicit and explcit wait.由于应用程序中的不一致,我同时使用隐式和显式等待。 When the Driver is Initialised,implicit wait is set to 10 seconds.初始化驱动程序时,隐式等待设置为 10 秒。 The code below is called after each click to check for any error element in the page, here if i dont turn off the implicit wait(by setting to 0), explicit wait still takes 10 sec wait(Given in implicit wait) inspite of specifing time of 1 second in the Wait Object for explicit wait.每次点击后调用下面的代码来检查页面中的任何错误元素,如果我不关闭隐式等待(通过设置为 0),显式等待仍然需要 10 秒等待(在隐式等待中给出)尽管指定等待 Object 中的 1 秒时间用于显式等待。

Question : Is it ok to turn off implicit wait and set it back again after the desired check to find errror element问题:是否可以关闭隐式等待并在所需检查后重新设置以查找错误元素

try {
    Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);

    var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(1));
    isDisp = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(errorLocator));
    Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

    Log.setTestDebug("Checking error after");
}

The best practice is not to use Implicit Wait and I agree the point made by @pcalkins.最佳做法是不使用隐式等待,我同意@pcalkins 的观点。

But as you have mentioned on the first part of your question, due to inconsistencies, if you are forced to use Implicit waits, it is safe to turn off and turn on them.但是正如您在问题的第一部分提到的那样,由于不一致,如果您被迫使用隐式等待,则关闭并打开它们是安全的。 I am answering this with my personal experience of doing the same.我用我个人的经历来回答这个问题。 I am not saying that this is the best approach or practice.我并不是说这是最好的方法或做法。

If you are planning to do this, I would suggest you to turn the implicit wait back on, in the finally block.如果您打算这样做,我建议您在 finally 块中重新打开隐式等待。

try
{
  turn off implicit wait
  do error check
}
catch 
{
  exception handling
}
finally
{
  turn on implicit wait
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM