Java WebDriver waiting for page to load

I want to get a page load exception, but still have no results. I use implicitlyWait to set a timer to throw exceptions.

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

Can someone please update me with suggestions? I need this to make sure that the page loading will not be infinite, and if the loading time is longer than I defined in the timer β†’ exception exception as a result and skip TC (as failed).

Thank you Vladimir

+8
java testng timeout webdriver
source share
2 answers

Why are you using implicit wait until the page opens? Try using explicit wait. Find some basic page element in the ramp (for example, a search text box). For example:

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

before () the method will raise a TimeoutException if the search text field does not appear within 5 seconds.

+17
source share

I do not agree that Pavel Zorins's answer will work, because it does not show how to handle exceptions.

This is how I wait for iFrame. This requires your JUnit test class to pass the RemoteWebDriver instance to 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 .

0
source share

All Articles