简体   繁体   中英

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. 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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