2016-09-15 15 views
6

Tôi sử dụng Selenium để kiểm tra giao diện người dùng.Selenium hoặc Coypu Chờ hiển thị phần tử và nhận thời gian trước khi hiển thị

Điều tôi muốn khi tôi nhấp vào nút một lần. Sau đó, tôi sẽ đợi cho đến khi một phần tử tồn tại. Và mất nhiều thời gian. Nếu mất nhiều thời gian hơn ms hết giờ. Vì vậy, nó sẽ cho 0 hoặc không tồn tại.

Tôi đã thử này sử dụng giống hải ly nam mỹ:

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 

Nhưng nó không hoàn toàn dường như đưa ra kết quả đúng.

Trả lời

1

Tôi tìm thấy một soltuion cho giống hải ly nam mỹ:

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 

Và sau đó:

Dim vElementLoadTime As Long = browser.WaitUntilElementIsPresent("#repSearchDocuments > .list-group-item", 20) 
Các vấn đề liên quan