Friday, April 6, 2012

Webdriver - Timeouts

Webdriver - Timeouts

In webdriver there are three different kinds of time outs. Proper values should be assigned, so that your script run efficiently without waiting for too log or short when page is not loaded and element not preset.

Method Summary
 WebDriver.TimeoutsimplicitlyWait(long time, java.util.concurrent.TimeUnit unit)
          Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
 WebDriver.TimeoutspageLoadTimeout(long time, java.util.concurrent.TimeUnit unit)
          Sets the amount of time to wait for a page load to complete before throwing an error.
 WebDriver.TimeoutssetScriptTimeout(long time, java.util.concurrent.TimeUnit unit)
          Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.

Timeouts can also be implemented using TestNG @Test annotation

Example

driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);


Timeouts can also be implemented using TestNG @Test annotation
@Test(groups = { "SampleTest" }, enabled = true, timeOut = 9000)


driver.manage().timeouts().implicitlyWait(ImplicitWait,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(PageLoadWait,TimeUnit.SECONDS); (Not implemented in IE)
driver.manage().timeouts().setScriptTimeout(AsyScripWait,TimeUnit.SECONDS);

Method Detail

implicitlyWait

WebDriver.Timeouts implicitlyWait(long time,
                                  java.util.concurrent.TimeUnit unit)
Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing aNoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.
Increasing the implicit 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.
Parameters:
time - The amount of time to wait.
unit - The unit of measure for time.
Returns:
A self reference.


setScriptTimeout

WebDriver.Timeouts setScriptTimeout(long time,
                                    java.util.concurrent.TimeUnit unit)
Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
Parameters:
time - The timeout value.
unit - The unit of time.
Returns:
A self reference.
See Also:
JavascriptExecutor.executeAsyncScript(String, Object...)


pageLoadTimeout

WebDriver.Timeouts pageLoadTimeout(long time,
                                   java.util.concurrent.TimeUnit unit)
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
Parameters:
time - The timeout value.
unit - The unit of time.
Returns:


17 comments:

  1. Hi Bharat,
    When we execute selenium scripts on FireFox 11,script is not running and a pop up is opening in FireFox.

    can you please let us know how to proceed with FireFox.

    Thanks,
    Ramachandra

    ReplyDelete
  2. Hi Bharat,

    When I try to run a test case with "driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);" in a server Jenkins the Ant Plugin, is not building properly, have any idea how can I solve that?

    The error is this one:

    compile:
    [javac] Compiling 93 source files to /home/ccontrol/.hudson/jobs/Grameen/workspace/bin
    [javac] /home/ccontrol/.hudson/jobs/Grameen/workspace/src/util/ParentTestCase.java:20: cannot find symbol
    [javac] symbol : method timeouts()
    [javac] location: interface org.openqa.selenium.WebDriver.Options
    [javac] driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    [javac] ^
    [javac] 1 error

    BUILD FAILED


    Thx and sorry for my bad english :)

    Mauricio P.

    ReplyDelete
    Replies
    1. I compile the code using ANT in windows through command prompt, it is working fine for me.
      Not using server Jenkins.

      Delete
    2. I use following ANT script
      http://bharath-marrivada.blogspot.in/2011/11/testng-selenium-build-jar-standlone.html

      Delete
    3. yes locally I have no problems to do the build but remotely (Jenkins) it doesn't work :/..

      Thx so much for the answer :)

      Delete
  3. Hi Baharat,

    I have the following code in my test (java) but the timeout doesn't seem to work (it has no effect at all). I've tested it with really slow connections and I expect it to fail after 5 secnods but it waits for page to load indefinately and sometimes it come back in 8-10 seconds and the test passes. Any idea with page timeout command is not doing what it is supposed to do?

    Cheers

    Syed

    driver.manage().timeouts().pageLoadTimeout(5,TimeUnit.SECONDS);
    driver.get("http://www.google.com");

    ReplyDelete
    Replies
    1. .implicitlyWait is currently implemented.
      .pageLoadTimeout - As per Jim (IE driver Lead) this method is not implemented.

      Delete
    2. Thanks for the prompt reply.

      I am using FirefoxDriver. Here is the declaration:

      protected static WebDriver driver;
      driver = new FirefoxDriver();
      driver.manage().timeouts().pageLoadTimeout(5,TimeUnit.SECONDS);
      driver.get("http://www.google.com");

      Totally stumped as according to the documentation it should work.

      Delete
    3. This method is not implemented in IE. I am not sure about the firefox.
      implicitlyWait method working?

      Delete
    4. I'm afraid not, I've tried that also. I just can't seem to think of any other way of making Selenium return the control back to the java program if the timeout limit is hit. It sometimes waits for long time and I have to kill it manaully.

      Any other ideas as to How I can get the control back from driver.get(URL) call?

      Delete
    5. Timeouts can also be implemented using TestNG @Test annotation
      @Test(groups = { "SampleTest" }, enabled = true, timeOut = 9000)

      Delete
    6. Ok I'll give it a try. It would have been nice to find out why a documented api is not working as it should.

      Many thanks.

      Delete
  4. Hi Bharath.

    I'm new in Selenium and
    I found news that pageLoadTimeout is implamented for IEDriver.
    http://code.google.com/p/selenium/source/detail?r=16949

    ReplyDelete
  5. hi bharath.
    I am new in selenium webdriver and I have following code in my test (java) some error displayed
    My code is
    package shopmaster;

    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverBackedSelenium;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class master {

    private WebDriverBackedSelenium selenium;

    @Before
    public void setUp() throws Exception {
    WebDriver driver = new ChromeDriver();
    String baseUrl = "http://65.111.172.80/";
    selenium = new WebDriverBackedSelenium(driver, baseUrl);
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

    }

    @Test
    public void shopmaster() throws Exception {
    selenium.open("/security");
    selenium.type("//*[@id='User_Name']", "master");
    selenium.type("//*[@id='Password']", "test");
    selenium.click("//*[@id='Link']");
    selenium.waitForPageToLoad("30000");
    selenium.click("//*[@id='awesome-menu']/li[5]/a");
    selenium.waitForPageToLoad("70000");
    assert (selenium.isElementPresent("css=input[id*='hdnTimeLogDate']"));
    selenium.click("css=input[value*='day'");
    }

    @After
    public void tearDown() throws Exception {
    }
    }


    error displayed on
    public void shopmaster() throws Exception {
    any one plz suggestion me

    ReplyDelete
  6. Hi Bharath.
    I am new in selenium webdriver I have the following code in my test (java).but it should be displayed error message.

    this is my code
    package shopmaster;

    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverBackedSelenium;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class master {

    private WebDriverBackedSelenium selenium;

    @Before
    public void setUp() throws Exception {
    WebDriver driver = new ChromeDriver();
    String baseUrl = "http://65.111.172.80/";
    selenium = new WebDriverBackedSelenium(driver, baseUrl);
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

    }

    @Test
    public void shopmaster() throws Exception {
    selenium.open("/security");
    selenium.type("//*[@id='User_Name']", "shop");
    selenium.type("//*[@id='Password']", "test");
    selenium.click("//*[@id='Link']");
    selenium.waitForPageToLoad("30000");
    selenium.click("//*[@id='awesome-menu']/li[5]/a");
    selenium.waitForPageToLoad("70000");
    assert (selenium.isElementPresent("css=input[id*='hdnTimeLogDate']"));
    selenium.click("css=input[value*='day'");
    }

    @After
    public void tearDown() throws Exception {
    }
    }

    error message displayed on
    @Test
    public void shopmaster() throws Exception {

    so plz suggestion any one.

    ReplyDelete