Difference Between Thread.sleep, ImplicitlyWait And ExplicitlyWait

ImplicitlyWait and Explicitly Wait to play a major role to automate and search for a particular element on the page as we know that selenium is all about integrating different APIs only about 20% we can use actual selenium i.e. WebDriver.

So let us explore these three terms in terms of theory and code:

Thread.Sleep: Thread.sleep() used in selenium for a purpose to halt the execution of a certain specified time, code is not further executed until the mentioned time so it is totally halting the execution of code until defined time limit reached.

Usability: Thread.sleep is not necessarily using in automating browser because it stops the execution of the code.

Code:


 

Thread.sleep(2000);    // 2000 is the millisecond time unit


 

ImplicitWait: Implicit wait is one of the popular terms which is using to wait for a certain element to appear on the page and the best thing with Implicitlywait is that it does not stop the execution of the program. It means that selenium can wait for the specified amount of time before throwing an error if element not found on the page. it is integrated with WebDriver so any element on the page to search for is taking more than specified time then it throws an error message.

Code:


WebDriver driver = new FirefoxDriver();

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

driver.get(“http://testingclass.com”);

Webelement locator = driver.findElement(By.id(“YourPageElementId”));


ExplicitlyWait: It is one of the advance or extended version of implicitlywait it is behave like implicitwait but the difference is that Explicitwait follows some condition in which basis it is usable to findout or searching for a particular element.

It uses ExpectedCondition based on which certain declaration can be defined.

  • It is usable if for a certain element we already know that this element will take more time to visible/hide so we can simply put an expected condition on that element and decrease the amount of time for implicitly wait

Code:


WebDriverWait wait = new WebDriverWait(driver,10);

WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.id(“dynamicId“));


 

Note: ImplicitWait & ExplicitWait are the most important topic of selenium you must be confident and assure the functionality of these terms.

If you have any doubt regarding above-explained tutorial do write your question in the below comment box.

Also Read:  TIPS TO CREATE EFFECTIVE RESUME – GET YOUR DREAM JOB

Adil

My Name is Mohammad Adil and I am a tech enthusiastic, a technical blogger, motivational speaker, trainer and passionate quality analyst with over 5+ years of experience in software testing field as well as information technology field. I worked with various reputed CMM level companies and currently working with a well-reputed product development company.