8

I want to get exception of page load, but still have not results on it. I use implicitlyWait to set timer to throw exception.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MILLISECONDS);
driver.get("http://www.rambler.ru");
driver.quit();

Could somebody please update me with suggestions? I need this to make sure that page load will not be infinite, and if time to load will be more than I've defined in timer -> throw exception as result and skip TC (as failed).

Thank you, Volodymyr

Andrejs
  • 10,803
  • 4
  • 43
  • 48
Volodymyr Prysiazhniuk
  • 1,897
  • 4
  • 22
  • 33

2 Answers2

17

Why are you using implicit wait before the opening of the page? Try to use explicit wait. Find some major page element at ramber(for example, the search textbox). For example:

 WebDriverWait wait = new WebDriverWait(webDriver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));

until() method will throw TimeoutException if search text box will not appear within 5 seconds.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Pazonec
  • 1,549
  • 1
  • 11
  • 35
  • Thank you for your suggestion, correct me if I'm wrong, you suggest me to use wait for element after driver.get("http://www.rambler.ru"); but WebDriver will wait until driver.get() will finish page loading and than will go to element wait? – Volodymyr Prysiazhniuk Mar 28 '12 at 11:00
  • webdriver always waits for loading of page. May be I have wrong understanding of your question. In this case you can use timer that will start the counting when you invoke get("rambler.ru") and throw TimeoutException yourself if the load time is more than 30 seconds(fro example) – Pazonec Mar 28 '12 at 11:13
  • How can time be set for the visibility of a text on the page? For example, a text "Welcome" is present on the page. I want to wait 3 sec until the text is visible on the page. What's the way to set 3 sec for waiting until the text visibility? – Ripon Al Wasim Nov 22 '12 at 07:17
  • you can create your own wait contition via implementing ExpectedCondition interface. It's very easy, just look how standard conditions are implemented – Pazonec Dec 03 '12 at 11:57
0

I do not agree that Pavel Zorins answer will work because he doesn't show how to handle the exceptions.

Here is how I do a wait for an iFrame. This requires that your JUnit test class pass the instance of RemoteWebDriver into the page object :

public class IFrame1 extends LoadableComponent<IFrame1> {

    private RemoteWebDriver driver;

    @FindBy(id = "iFrame1TextFieldTestInputControlID" )
    public WebElement iFrame1TextFieldInput;

    @FindBy(id = "iFrame1TextFieldTestProcessButtonID" )
    public WebElement copyButton;

    public IFrame1( RemoteWebDriver drv ) {
        super();
        this.driver = drv;
        this.driver.switchTo().defaultContent();
        waitTimer(1, 1000);
        this.driver.switchTo().frame("BodyFrame1");
        LOGGER.info("IFrame1 constructor...");
    }

    @Override
    protected void isLoaded() throws Error {        
        LOGGER.info("IFrame1.isLoaded()...");
        PageFactory.initElements( driver, this );
        try {
            assertTrue( "Page visible title is not yet available.", driver
     .findElementByCssSelector("body form#webDriverUnitiFrame1TestFormID h1")
                    .getText().equals("iFrame1 Test") );
        } catch ( NoSuchElementException e) {
            LOGGER.info("No such element." );
            assertTrue("No such element.", false);
        }
    }

    @Override
    protected void load() {
        LOGGER.info("IFrame1.load()...");
        Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring( NoSuchElementException.class ) 
                .ignoring( StaleElementReferenceException.class ) ;
            wait.until( ExpectedConditions.presenceOfElementLocated( 
            By.cssSelector("body form#webDriverUnitiFrame1TestFormID h1") ) );
    }
....

NOTE: You can see my entire working example here.

djangofan
  • 28,471
  • 61
  • 196
  • 289