I am trying implicit wait in my code that is not applied for each and every element. I want to want 3 sec for selecting each and every element in my scripts.
-
You can use the the below code thread.sleep(3000)– chaitra teppadCommented Mar 29, 2019 at 11:58
-
Why? That will slow down the script with 3 seconds per element used.– Niels van ReijmersdalCommented Apr 3, 2019 at 10:14
-
If a question attracts 5 answers, I don’t get why there isn’t a single upvote…– beatngu13Commented Apr 3, 2019 at 11:09
5 Answers
You can use the following code wherever you feel to add wait.
Thread.sleep(1000); //Enter your desired wait interval in milliseconds
It depends on what you are trying to do.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Will stop your tests from failing if elements don't immediately appear on the page, with it waiting up to 10 seconds to find them. However click events will fire off immediately upon the element being found, not necessary when they are actually clickable.
e.g. If you click something and an overlay appears, then the next click event may still be actioned because the element exists in the DOM at that point, but the test will fail as the click would be applied against the overlay.
As others have suggested you can use Thread.sleep(x); to force the test to wait x amount of time, however this then slows down the test as even if it could continue it will wait x.
Thread.sleep(1000); //Enter your desired wait interval in milliseconds
The better option is to work out what is causing the issue and wait for it to not be visible, or wait for the click element to be clickable:
wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("id")));
This then means it will wait 10 seconds for the element to report it is clickable, then click on it as soon as it does. Rather than as normal clicking the element as soon as it is located on the page.
Per the docs
https://webdriver.io/docs/timeouts.html
Session Implicit Wait Timeout
A session has an associated session implicit wait timeout that specifies a time to wait for the implicit element location strategy when locating elements using the findElement or findElements commands (respectively $ or $$ when running WebdriverIO with or without wdio testrunner). Unless stated otherwise it is zero milliseconds. You can set this timeout via:
browser.setTimeout({ 'implicit': 5000 });
In your case
browser.setTimeout({ 'implicit': 3000 });
As others note this global approach can significantly slow down tests. However if you have a lot of javascript and a lot of flaky tests and reliability is more important than run time then it may be a strategy that helps. Many folks find that despite the slowdown, being able to rely on passing tests, especially in a devops environment can be vital.
-
1This is how I would do it. Warning, it makes every call slower. Generally you'd prefer to use webdriver waits so you can adjust the amount of time waiting based on the part of the system under test. It's rarely a good practice to set the implicit timeout too high.– VeretaxCommented Apr 2, 2019 at 20:36
Just add this line after driver instance:-
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
-
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dell\\eclipse-workspace\\driver\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Commented Mar 29, 2019 at 6:36
-
That is my code i am applying 10 but it will not wait 10 seconds by clickin elements @UpkarSingh Commented Mar 29, 2019 at 6:37
-
It is defined only once and it will remain same throughout the driver object instance, may be in your code you have changed the driver instance. Can u please share your console, it will telling you the wait time that how much driver has wait for that element. Commented Mar 29, 2019 at 7:01
-
@anjithneerukonda for your better script try to use explicit wait or fluent wait, because these wait work on expected conditions. Commented Mar 29, 2019 at 7:05
It is a very bad practice to use Thread.Sleep for elements wait.
You can use Implicit or Explicit Wait provided by Selenium as per your use case.
You can refer below urls for more details:
https://www.joecolantonio.com/selenium-performance-reliability/
And never mix use of Implicit and Explicit Wait as the Wait would not behave as expected.
Check below url for more details on that:
https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits