Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Change implicitlyWait dynamically

36 views
Skip to first unread message

Vladimir B

unread,
Jun 12, 2020, 3:20:19 PM6/12/20
to Selenium Users

Please comment the following code I found on YouTube. It checks whether an element is present at the time

public boolean isElementPresent(By locator)
    {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        List<WebElement> list = driver.findElements(locator);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        if (list.size() == 0)
            return false;
        else
            return list.get(0).isDisplayed();
    }

It dynamically changes implicitlyWait in the method. In all Selenium resources are always stated that the implicitWait can be set only once in the test class. The code above is similar to some extent to the explicit wait since it adapts to different situations. What is your opinion about this code? 

Joe Ward

unread,
Jun 12, 2020, 11:33:33 PM6/12/20
to seleniu...@googlegroups.com
It seems a bit pointless. What opinions do you have?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6dfe7778-fc49-4086-8b30-7f14812e3d66o%40googlegroups.com.

Vladimir B

unread,
Jun 13, 2020, 1:25:12 AM6/13/20
to Selenium Users
In the Selenium documentation https://www.selenium.dev/documentation/en/webdriver/waits/ is said

Once set, the implicit wait is set for the life of the session.

In the code presented we change the implicitlyWait twice. 
And it works. Does it mean that the documentation is wrong?

On Friday, June 12, 2020 at 8:33:33 PM UTC-7, That Guy wrote:
It seems a bit pointless. What opinions do you have?
On Fri, 12 Jun 2020 at 20:20, Vladimir B <vladimir...@gmail.com> wrote:

Please comment the following code I found on YouTube. It checks whether an element is present at the time

public boolean isElementPresent(By locator)
    {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        List<WebElement> list = driver.findElements(locator);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        if (list.size() == 0)
            return false;
        else
            return list.get(0).isDisplayed();
    }

It dynamically changes implicitlyWait in the method. In all Selenium resources are always stated that the implicitWait can be set only once in the test class. The code above is similar to some extent to the explicit wait since it adapts to different situations. What is your opinion about this code? 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

Joe Ward

unread,
Jun 13, 2020, 4:42:19 AM6/13/20
to seleniu...@googlegroups.com
No, it does not.

Let's take this piece of code as an example.

1 public boolean isElementPresent(By locator)
2     {
3         driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
4         List<WebElement> list = driver.findElements(locator);
5         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
6         if (list.size() == 0)
7             return false;
8         else
9             return list.get(0).isDisplayed();
10     }
Line 3 sets implicit wait timeout to 0 seconds, presumably because line 4 is just trying to find if an element is present in the DOM at all.

Line 5 sets implicit wait timeout to 30 seconds, presumably because it was 30 seconds (setup in some place we can' see) before line 3 set it to 0 seconds.

Both line 3 and 5 set the implicit wait timeout for the session. What the documentation means is that an implicit wait is not specifically scoped to the function you use it in but the scope of the driver itself.

The reason I think it's a bit pointless is because I don't really like functions that make random changes like this. At the very least they're annoying to debug.

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/0d16b232-fb5a-4c9f-bd10-6f12cdc427c9o%40googlegroups.com.

Vladimir B

unread,
Jun 13, 2020, 3:54:40 PM6/13/20
to Selenium Users
Thanks. Then, one more question. What is exactly a session?
Let's assume you have the following code.

@BeforeEach
void setUp()
{
     driver
= new ChromeDriver();
}


@Test
void test1()
{
     driver
.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   
...
}


@Test
void test2()
{
   
...
}

How many sessions do we have here? Two since the driver object will be reset in every test? So the wait setup in test 1 will not be applicable to test2?
Reply all
Reply to author
Forward
0 new messages