2016-09-15 27 views
6

我使用Selenium進行UI測試。Selenium或Coypu等待元素秀並獲得展示前的時間

當我點擊一個按鈕時,我想要什麼。然後我會等到一個元素存在。花時間花費多長時間。如果它比超時毫秒更長。所以它會給0或不存在。

我試試這個使用海狸鼠:

browser.FindCss("[name=""searchbtn""]").Click() 
Dim vStopwatch = Stopwatch.StartNew() 

browser.TryUntil(Function() browser.FindXPath("//*[@id=""blockDocumentsSearch""]").Hover(), Function() browser.FindCss("#repSearchDocuments > .list-group-item").Exists(), TimeSpan.FromMilliseconds(500), New Options() With { 
       .Timeout = TimeSpan.FromMilliseconds(10000)}) 


     If Not browser.FindCss("#repSearchDocuments > .list-group-item").Exists() Then 
      pTCH.ErrorCurrentStep("Not showing any documents or timeout.", browser) 
      Return 0 
     End If 

     Return vStopwatch.ElapsedMilliseconds 

但它並不完全,似乎給了正確的結果。

回答

1

我找到一個soltuion爲海狸鼠:

Public Module BrowserSessionExtension 
    <Extension> 
    Public Function WaitUntilElementIsPresent(browser As BrowserSession, cssSelector As String, Optional timeout As Integer = 10) As Long 
     Dim vExist As Boolean = False 
     Dim vStopwatch = Stopwatch.StartNew() 
     For i As Integer = 0 To timeout - 1 
      If browser.FindCss(cssSelector, Options.First).Exists() Then 
       vExist = True 
       Exit For 
      End If 
      Thread.Sleep(1000) 
     Next 
     vStopwatch.Stop() 
     If vExist Then 
      Return vStopwatch.ElapsedMilliseconds 
     Else 
      Return 0 
     End If 
    End Function 
End Module 

然後:

Dim vElementLoadTime As Long = browser.WaitUntilElementIsPresent("#repSearchDocuments > .list-group-item", 20)