-2

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Is it possible to set '10' as variable so that it can be chabged dynamically? If possible how? Thank you!

2 Answers2

0

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.

It is set for life of webdriver session.

Coming to your question Is it possible to set '10' as variable ?

Yes it's possible.

int a = 10;

driver.manage().timeouts().implicitlyWait(a, TimeUnit.SECONDS);

wrap this line in a method and pass the int args if you have static driver reference like this :

public void wdImplicitWait(int duration){
    driver.manage().timeouts().implicitlyWait(duration, TimeUnit.SECONDS);
}

call it like this :

wdImplicitWait(5);
some web element interaction like click or sendkeys here
wdImplicitWait(3)

if your driver is not static then, make sure to pass driver instance reference

public void wdImplicitWait(int duration, WebDriver driver){
        driver.manage().timeouts().implicitlyWait(duration, TimeUnit.SECONDS);
    }

But there won't be any impact since it is set for entire life of webdriver for the particular execution.

so calling implicitlyWait again and again won't have any impact.

See what official doc says

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hi @cruisepandey what do you mean by call it like this?. I tried wrapping it in a method but, when i called my `keyword` and change the `value` to 180 it didn't have any effect. Thank you – John Paul Arcilla May 28 '21 at 13:11
  • Can you share the wrapped code ? How did you determine that it didn't wait for 180 seconds ? Did you get any erro ? Implicit wait says that whatever value you set, set it for life time of driver object which means that once driver is initialised and till the time it is closed or quit. – cruisepandey May 28 '21 at 13:18
  • `public class ImplicitWait { public static WebDriver driver; int timeInSeconds = 0; @Keyword public void implicitWait(int timeInSeconds) { // Get active window driver = DriverFactory.getWebDriver(); //Specify implicit wait of 2 minutes driver.manage().timeouts().implicitlyWait(timeInSeconds, TimeUnit.SECONDS); } }` >Here is my code, i added this on my test case as custom keyword and change the input value to 180 and didn't wait for 3 minutes to click a certain button. – John Paul Arcilla May 28 '21 at 13:28
  • Calling `implicitlyWait` again and again will have an impact if you change the value in seconds. For example, if you set it to 0 in the middle of code it will mean that there is no wait. – vitaliis May 28 '21 at 13:36
  • @vitaliis : Why not set it up once for life time of driver object ? and use explicit wait for specific waiting purpose ? – cruisepandey May 28 '21 at 13:53
  • There are some corner cases when you need to change it https://stackoverflow.com/questions/65373501/why-does-seleniums-wait-until-notec-invisibility-of-element-located-does-not – vitaliis May 28 '21 at 13:54
  • @JohnPaulArcilla : I guess by seeing your code is that "You should opt for explicit wait instead of implicit wait" – cruisepandey May 28 '21 at 13:54
  • 1
    It is recommended to to mix explicit/implicit waits in general – vitaliis May 28 '21 at 13:56
  • @vitaliis : there you had 10 seconds wait and it's obvious the nobody would want to mix explicit wait with implicit wait. But I see lot of folks like mixing both of them and always have undetermined timeout – cruisepandey May 28 '21 at 13:57
  • @vitaliis : "Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds." – cruisepandey May 28 '21 at 13:58
  • @vitaliis : That is what official says :) – cruisepandey May 28 '21 at 13:59
  • I faced unpredictable wait times only once. When you wait until an element is invisible. – vitaliis May 28 '21 at 14:01
  • 1
    @vitaliis : I believe we can not argue on this since that's what official says. – cruisepandey May 28 '21 at 14:02
0

If you are looking for a method that will set an implicitlyWait accordingly to passed time duration it can be done as following:

public void setImplicitlyWait(Webdriver driver, int duration){
    driver.manage().timeouts().implicitlyWait(duration, TimeUnit.SECONDS);
}

Depending on your actual project structure it's possible that you need to pass time duration only, do it will be like this:

public void setImplicitlyWait(int duration){
    driver.manage().timeouts().implicitlyWait(duration, TimeUnit.SECONDS);
}

However it's not recommended to use implicitly waits, explicit waits should be used.
Also, if you still want to define implicitly wait it is normally defined once per driver instance i.e. for the entire test life.

Prophet
  • 32,350
  • 22
  • 54
  • 79