2009-09-22 58 views
2

當我在Selenium中調用Open(「[some URL]」)方法時,它會等待下載所有資源(圖像,CSS,JS)。但它是如何知道瀏覽器已完成請求資源的?Selenium的Open函數等待什麼?

這裏是我的驅動程序代碼段(在C#):

DefaultSelenium selenium = new DefaultSelenium(
    "localhost", 4444, "*iexplore", "http://www.stackoverflow.com"); 

// open my profile page 
selenium.Open("http://stackoverflow.com/users/111934/jeff-meatball-yang"); 

上面的URL僅僅是爲了示例。如果我在一個setTimeout()事件處理程序觸發的setTimeout()中包含一個AJAX調用,事件處理程序中Selenium實際上不會等待該AJAX調用...我必須手動告訴Selenium等待使用WaitForPageToLoad(timeout)

那麼Selenium如何檢測普通頁面(無AJAX)何時完成加載?

回答

2

我相信它會等待location.href可用,這意味着如果你在這之後運行AJAX,你還需要使用waitForCondition。

selenium.open("http://www.example.com/"); 
selenium.waitForCondition("var value = selenium.getText('id=pageLoadStatus'); value == 'Loaded'", "60000"); 

下面是從硒芯當前頁面負載檢測碼:

this.recordPageLoad = function(elementOrWindow) { 
    LOG.debug("Page load detected"); 
    try { 
     if (elementOrWindow.location && elementOrWindow.location.href) { 
      LOG.debug("Page load location=" + elementOrWindow.location.href); 
     } else if (elementOrWindow.contentWindow && elementOrWindow.contentWindow.location && elementOrWindow.contentWindow.location.href) { 
      LOG.debug("Page load location=" + elementOrWindow.contentWindow.location.href); 
     } else { 
      LOG.debug("Page load location unknown, current window location=" + this.getCurrentWindow(true).location); 
     } 
    } catch (e) { 
     LOG.error("Caught an exception attempting to log location; this should get noticed soon!"); 
     LOG.exception(e); 
     self.pageLoadError = e; 
     return; 
    } 
    self.newPageLoaded = true; 
}; 
+0

感謝司令戴夫。 :)你能指點我的文件或SVN地址嗎? – 2009-09-22 15:00:55

+0

不客氣! http://code.google.com/p/selenium/source/browse/selenium-core/trunk/src/main/resources/core/scripts/selenium-browserbot.js#83 – 2009-09-22 15:16:51

+0

您也可以使用['waitForElementPresent' ](http://release.seleniumhq.org/selenium-core/1.0.1/reference.html#storeElementPresent)調用'open'後。 – ma11hew28 2011-02-19 18:50:49

1

@戴夫亨特you're correct that recordPageLoad is called,但是如果可用它僅僅記錄該頁面加載位置(location.href),並設置self.newPageLoaded = true,由isNewPageLoaded返回的值,因此由doOpen返回。

isNewPageLoaded_isNewPageLoaded < = makePageLoadCondition < = doOpen稱爲selenium-api.js。並且,在Selenium IDE運行open命令之後調用doOpen

相關跡線是:

doOpen電話(移動到selenium-browserbot.jsopenLocation =>getCurrentWindow =>_modifyWindow =>modifySeparateTestWindowToDetectPageLoads(其中根據它上面的評論,等待「通過輪詢持續直到文檔的改變,並full loaded「)=>pollForLoad =>getReadyState

getReadyState返回document.readyStaters),並pollForLoad等待直到rs == 'complete',即直到頁面已經完全加載,圖像和所有。

Ace Ventura解釋它比我更好。


P.S. @Dave Hunt,謝謝你的Selenium IDE Flow Control script on GitHub。它派上用場! :)