How I can check whether a page is loaded completely or not in web driver?

前端 未结 9 1842
走了就别回头了
走了就别回头了 2020-12-01 01:33

I am writing some Java Webdriver code to automate my application. How can I correctly check whether the page has been loaded or not? The application has some Ajax calls, too

9条回答
  •  鱼传尺愫
    2020-12-01 02:22

    Here is how I would fix it, using a code snippet to give you a basic idea:

    public class IFrame1 extends LoadableComponent {
    
        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);
            }
        }
    
        /**
         * Method: load
         * Overidden method from the LoadableComponent class.
         * @return  void
         * @throws  null
         */
        @Override
        protected void load() {
            LOGGER.info("IFrame1.load()...");
            Wait wait = new FluentWait( 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") ) );
        }
    ....
    

提交回复
热议问题