2013-07-30 31 views
2

我需要檢查頁面加載時間是否超過n秒。檢查Selenium中的頁面加載時間

我使用C#與硒和NUnit

我見過幾個方面:

1:

var time1=DateTime.Now.Ticks/TimeSpan.TicksPerMillisecond; 
driver.Navigate().GoToUrl("http://football.ua"); 
var time2=DateTime.Now.Ticks/TimeSpan.TicksPerMillisecond; 
Console.WriteLine(time2-time1); 

//check difference 

2:

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));   
driver.Navigate().GoToUrl(---------); 

//If the load is greater than 10 seconds, takes exception 
//I doubt this method, since exceptions emerge, even if the place is quite large  timeout (10 sec) 

3:用明確等待

示例:

IWebDriver driver = new FirefoxDriver(); 
driver.Url = "http://football.ua"; 
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement myDynamicElement = wait.Until<IWebElement>((d) => 
    { 
    return d.FindElement(By.Id("someDynamicElement")); 
    }); 

但我不知道如何在網址上等待移動。 告訴我,有什麼更好的,或提供自己的版本。

+1

如何定義頁面加載? By.Id(「someDynamicElement」)存在嗎? –

+0

如果您使用Firefox瀏覽器,這可能會有用 - https://groups.google.com/forum/#!searchin/firebug/persist/firebug/m-3liXcd_T4/d-qlcGUHaKIJ – praneel

回答

0

如果要檢查頁面的加載時間超過N秒,則應在調用您嘗試加載的頁面時設置pageLoadTimeOut N秒。這也應該是一個try/catch塊,所以你可以看到頁面何時試圖加載太久。據我所知,如果達到超時,那麼下一行代碼將繼續執行,因此,如果您不想在執行完代碼之後,整個代碼塊應該處於try/catch狀態。