简体   繁体   English

Selenium或Coypu等待元素展示并在展示前获得时间

[英]Selenium or Coypu Wait for element show and get time before show

I using Selenium for UI testing. 我使用Selenium进行UI测试。

What I want to when I click on a button once . 当我点击一次按钮时我想要的是什么。 Then I'll wait until an element exists. 然后我会等到一个元素存在。 And take time on how long it takes . 并花时间花多长时间。 If it takes longer than timeout ms . 如果花费的时间超过超时ms。 So it will give 0 or not exist. 所以它会给0或不存在。

I have try this using Coypu : 我用Coypu尝试了这个:

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

But it does not quite seem to give right result. 但它似乎没有给出正确的结果。

I find one soltuion for Coypu: 我找到了一个关于海狸鼠的解决方案:

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

And then: 然后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM