0

Hi, I am trying to use webdriver in selenium test. I want to check how it works. I given 5 seconds as maximum time in webdriver wait. My page loading time is more than 7 seconds but I am still not getting any timeout exception from webdriver wait. I am giving my console output also. Please tell why i am not getting timeout exception?

public class MainClass {

private static ChromeDriver driver;


public static void main(String[] args) {



            executeTest();

}

private static void executeTest( )  {

    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    driver = new ChromeDriver();
    // driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    driver.get("http://myUrl");

    Long loadtime = (Long) ((JavascriptExecutor) driver)
            .executeScript("return performance.timing.loadEventEnd - performance.timing.navigationStart;");

    System.out.println("loadding time " + Loadtime);

    WebDriverWait wait = new WebDriverWait(driver, 5);

    Boolean sign_in = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")))
            .isDisplayed();

    if (sign_in == true) {
        driver.findElement(By.id("signinbutton")).click();
    } else {
        System.out.println("Oops! Couldn't locate sign_in element!");
    }

    Boolean user_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")))
            .isDisplayed();
    // user_name.sendKeys("ANAND@RIL");
    if (user_name == true) {
        driver.findElement(By.id("username_id")).sendKeys("ANAND@RIL");
    } else {
        System.out.println("Oops! Couldn't locate user_name element!");
    }

    Boolean password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")))
            .isDisplayed();

    if (password == true) {
        driver.findElement(By.id("password_id")).sendKeys("ANAND");
    } else {
        System.out.println("Oops! Couldn't locate password element!");
    }

    WebElement login = 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    login.click();

}
}

Console ooutput:

Attempt 1 :

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42020
Only local connections are allowed.
Apr 18, 2018 12:07:35 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loading time 7872**

Attempt 2 :

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 38325
Only local connections are allowed.
Apr 18, 2018 12:07:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 6632**

Attempt 3 :

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 34619
Only local connections are allowed.
Apr 18, 2018 12:07:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 7522**

Attempt 4 :

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 48000
Only local connections are allowed.
Apr 18, 2018 12:08:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
learner
  • 331
  • 1
  • 9
  • 22
  • Try with try-catch. In under catch, use 'throw' statement. – Bala Apr 18 '18 at 06:49
  • where i have to put try catch – learner Apr 18 '18 at 06:55
  • @learner Which line of code are you expecting the error? There is a difference between selenium loading the page and webdriverwait... – Grasshopper Apr 18 '18 at 08:45
  • i am expecting timeout exception in sign_in button code..bcoz sometime my initial url take more than 60 seconds to load ..after load initial URL first page contains sign in button.. – learner Apr 18 '18 at 08:59

2 Answers2

1

I have modified the method with a try-catch. Please try the below thing:

public static void main(String[] args) throws Exception {
try{
    executeTest();
    } catch (Exception e) {
        System.out.println(e);
        throw e;
    }
}
private static void executeTest( ) throws Exception {
try{

    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    driver = new ChromeDriver();
    // driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    WebDriverWait wait = new WebDriverWait(driver, 5);

    driver.get("http://myUrl");

    Boolean sign_in = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")))
            .isDisplayed();

    Long loadtime = (Long) ((JavascriptExecutor) driver)
            .executeScript("return performance.timing.loadEventEnd - performance.timing.navigationStart;");

    System.out.println("loadding time " + Loadtime);

    if (sign_in == true) {
        driver.findElement(By.id("signinbutton")).click();
    } else {
        System.out.println("Oops! Couldn't locate sign_in element!");
    }

    Boolean user_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")))
            .isDisplayed();
    // user_name.sendKeys("ANAND@RIL");
    if (user_name == true) {
        driver.findElement(By.id("username_id")).sendKeys("ANAND@RIL");
    } else {
        System.out.println("Oops! Couldn't locate user_name element!");
    }

    Boolean password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")))
            .isDisplayed();

    if (password == true) {
        driver.findElement(By.id("password_id")).sendKeys("ANAND");
    } else {
        System.out.println("Oops! Couldn't locate password element!");
    }

    WebElement login = 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    login.click();
    }
    catch(Exception e) {
        System.out.println(e);
        throw e;
    }
}
Bala
  • 184
  • 3
  • 19
  • tried as u said,but not getting any eception,just showing loading time only .still loading time above 7 seconds – learner Apr 18 '18 at 07:09
  • Please try the changed. I just declared wait before `driver.get()`. – Bala Apr 18 '18 at 07:33
  • still not getting any exceptions,,last loading times are "loadding time 7842,loadding time 9983".. – learner Apr 18 '18 at 07:42
1

You need to consider a few facts as follows :

  • pageLoadTimeout() : pageLoadTimeout() sets the amount of time to wait for a page load to completely before throwing an exception/error. If the timeout is negative, page loads can be indefinite.

    In your code you have configured pageLoadTimeout() to 40 seconds. As per the logs your page loading times are 7872 ms, 6632 ms and 7522 ms, so you don't see an exception/error.

    If you don't configure the pageLoadTimeout(), as per the current implementation of GeckoDriver the default value of "pageLoad":300000 is considered.

    You can find a detailed discussion on pageLoadTimeout() in pageLoadTimeout in Selenium not working

  • WebDriverWait() : WebDriverWait() is the specialization of FluentWait that uses WebDriver instances and works in-conjunction with the ExpectedConditions Class which is defined to wait for a certain condition to occur before proceeding further in the code.

    In your code you have configured WebDriverWait to 5 seconds which is applicable to all the ExpectedConditions you have used as :

     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")));
     wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    

    All the ExpectedConditions are achieved within the time span of 5 seconds. So you don't see any exception/error either.

    You can find a detailed discussion on WebDriverWait / ExplicitWait in Replace implicit wait with explicit wait (selenium webdriver & java)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • but in my code pageLoadTimeout() is commented,i am not using that code..and when i giver webdriver wait 1 second ,at that time also am not getting any exceptions.. – learner Apr 18 '18 at 08:05
  • Updated my answer _If you don't configure the `pageLoadTimeout()`, as per the current implementation of GeckoDriver, the default value of **"pageLoad":300000** is considered_ – undetected Selenium Apr 18 '18 at 08:15
  • k.can u say in webdriver wait when i given 1 second,at that time also i didnt get any exception..i tired many test runs..and loading elemets taken more than 1 second,den also it didnt throw any exception.so am confused – learner Apr 18 '18 at 08:24
  • A lot depends on the architecture of the _AUT (Application under Test)_. If the app is based on _HTML5_ normally all the elements will be loaded when the _WebClient_ achieves `'document.readyState' is equal to "complete"` but incase of _Angular_ or _React Native_ based apps _WebDriverWait()_ plays a vital role. In your case _WebDriverWait()_ may be trivial but definitely it's a good practice to move forward. – undetected Selenium Apr 18 '18 at 08:32