Pages

Search This Blog

Monday, February 9, 2015

what is Implicit, Explicit & Fluent Wait in Selenium Web driver and their difference.

Implicit Wait

By using Implicit wait we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.



 WebDriver driver = new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung");
 WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));


Explicit Wait

It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible, etc.

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

Fluent Wait

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page..

FluentWait  wait = new FluentWait(driver).withTimeout(timeOutInSeconds,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
        element = (WebElement) wait.until(ExpectedConditions.visibilityOfElementLocated(by));
package PageFactory; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; /** * Created by Niraj on 2/10/2015. */ public class ConceptPage { WebDriver driver; @FindBy(xpath="//input[@type='submit' and @title='See more']") WebElement btnSeeMore; @FindBy(xpath="//div[@class='itm'][21]//div[@class='itemttl']") WebElement thelement; public static final int DEFAULT_WAIT_4_PAGE = 12; public static final int DEFAULT_WAIT_4_ELEMENT = 15; public ConceptPage(WebDriver driver){ this.driver=driver; PageFactory.initElements(driver,this); } public void verify21stElement(){ WebElement webElement = waitforElementUsingFluent(driver, By.xpath("//div[@class='itm'][21]//div[@class='itemttl']//a"), 20);
 public void  verify21stElement(){
        WebElement webElement = waitForElement
(driver, By.xpath("//div[@class='itm'][21]//div[@class='itemttl']//a"), 20);

System.out.println("Element present : "+ webElement.getAttribute("href")); }


//Explicit wait function definition here...:

    public static WebElement waitForElement(WebDriver driver, final By by, int timeOutInSeconds) {
        WebElement element;
        try{
            //To use WebDriverWait(), we would have to nullify implicitlyWait().
            //Because implicitlyWait time also set "driver.findElement()" wait time.
            //info from: https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/6VO_7IXylgY
            driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()

            WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

            driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_ELEMENT, TimeUnit.SECONDS); //reset implicitlyWait
            return element; //return the element
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
//Fluent wait function definition goes here... 

    public static WebElement waitforElementUsingFluent(WebDriver driver, By by, int timeOutInSeconds){
        
        WebElement element;
       FluentWait  wait = new FluentWait(driver).withTimeout(timeOutInSeconds,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);
        element = (WebElement) wait.until(ExpectedConditions.visibilityOfElementLocated(by));
        return element;


    }

public void clickOnSeeMore(){
    btnSeeMore.click();
}
}


// Main test goes here...

package Selenium;
import PageFactory.ConceptPage;import PageFactory.HomePage;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/** * Created by Niraj on 2/5/2015. */public class LoginWithPageFactory {
    WebDriver driver;
    LoginPage loginPage;
    HomePage homePage;
    @BeforeTest    public void setup(){
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung");
    }
    
    @Test(priority = 1)
    public void test_AJAX(){
        driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung");
        ConceptPage conceptPage = new ConceptPage(driver);
        conceptPage.clickOnSeeMore();
        conceptPage.verify21stElement();

    }
    public void teardown(){
        driver.close();
        driver.quit();;

    }
}



23 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Very efficiently written information. It will be valuable to everyone who uses it, including myself. Thanks a lot!
    website performance test

    ReplyDelete
  6. Selenium is not just a single tool or a utility, rather a package of several testing tools and for the same reason it is referred to as a Suite. This blog gives great view for selenium beginners. Learn Selenium from the best Selenium Online Training in your locality at CatchExperts.com

    ReplyDelete
  7. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete

  8. Interested in mastering digital marketing training in 2016? Join Digital Marketing training Certification Course. Join FREE Demo

    ReplyDelete
  9. love this blog very informative thanks for share.
    Learn Selenium WebDriver Training by QEdge Technologies.
    Selenium Training in Hyderabad

    ReplyDelete
  10. Good article and very nice information keep posting ,
    selenium training http://tekclasses.com/course/selenium-training/

    ReplyDelete
  11. Hello sir, Its quite difficult to understand the coding. Can you please update the explanation for the coding part, which makes us to be more easy to understand the entire concept.
    Selenium Training in Chennai

    ReplyDelete
  12. I must thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now..

    Selenium Training in Chennai

    ReplyDelete
  13. This is very informative blog and article thank you for sharing with us keep posting more information about salesforce trainingselenium training, selenium online training, selenium course,learn selenium course

    ReplyDelete
  14. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  15. SAV Design plans, installs and documents the entire system. From start to finish, we handle all your needs from cabling to components. Presenting the excellent data and networking offerings in Hamilton at realistic fee.  We provides the best Data and networking in Hamilton at Reasonable price. Thanks a lot for visiting your blog.

    Data and networking Hamilton

    ReplyDelete
  16. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete
  17. Looking for Selenium Training in Hyderabad with Java. we provide you Best WebDriver by IT Industry Experts.
    Selenium Training in Hyderabad

    ReplyDelete
  18. Great post with very useful information to all thanks for sharing with all of us
    buy wireless mouse online | buy wired mouse online

    ReplyDelete
  19. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete