TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Difference between Implicit and Explicit Wait

    Introduction

    In this post, we will go through the difference between Implicit Wait and Explicit wait in Selenium WebDriver. In addition, we would go through some code example about both of them.

    Implicit Wait

    We can use Implicit wait for waiting for a web element. So, we would be able to set for all the web elements, that we use in our test scripts. Furthermore, it is generic to all the web elements of the web application. Also, in Selenium, Implicit wait time is common for all the operations for the web driver. Therefore, WebDriver Implicit wait is like a global wait time for the whole application. For example, we can set it as shown below:

     

    FirefoxDriver driver=new FirefoxDriver();
    driver.get("http://www.testingdocs.com");
    driver.manage().window().maximize();
     
    WebElement element =driver.findElement(By.partialLinkText("TESTNG"));
    Actions action=new Actions(driver);
    action.contextClick(element).perform();
    action.sendKeys("w").perform();
    driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
     
    // browser window close
    driver.close();

    As shown above 45 seconds specifies the amount of time the driver should wait
    when searching for an element if it is not immediately present.

    Single element

    WebDriver polls the page for an element.It polls the web page until the element is found or the timeout expires. When timeout expires it throws a NoSuchElementException to the calling method.

    Multiple elements

    Webdriver when searching for multiple elements, polls the page until at least one element has been found or the timeout expires.

    We should exhibit caution while using implicit wait: Increasing the implicitly wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.

    Explicit Wait

    Explicit wait is like conditional wait for any specific web element. Also, we may want to wait overriding the implicit wait time. We can specify ExpectedCondition to apply the condition wait. Explicit Wait is an intelligent kind of wait that provides a better approach than that of Implicit Wait.

    Example Code snippet: sample method to wait until the visibility of elements on the web page.

     

    public void waitForVisibilityOfElement(By by, String locator)
     throws Throwable {
     
     WebDriverWait wait = new WebDriverWait(driver, 45);
     try {
     wait.until(ExpectedConditions.visibilityOfElementLocated(by));
     } 
     catch (Exception e) {
     e.printStackTrace();
     
     } 
     }

    WebDriverWait is a subclass of FluentWait class :
    public class WebDriverWait extends FluentWait<WebDriver>

    FluentWait Example is discussed here: FluentWait Example

    —

    Selenium WebDriver Tutorials on this website can be found at:

    https://www.testingdocs.com/selenium-webdriver-tutorial

    For more details on the Selenium, visit the official website at:

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Getting Started with Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ Difference between driver.get() and driver.navigate()› WebDriver Code Snippets

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com