uezo / TinySeleniumVBA

A tiny Selenium wrapper written in pure VBA
MIT License
60 stars 17 forks source link

Add Method. SetImplicitlyWait, SetPageLoadTimeout, SetScriptTimeout, GetImplicitlyWait, GetPageLoadTimeout, GetScriptTimeout, Wait #26

Open ghost opened 3 years ago

ghost commented 3 years ago

TinySeleniumVBA WebDriver.cls

Win32 API定義を WebDriver.cls の最初の関数記述より前に追加。

' ==========================================================================
' Win32 API
' ==========================================================================
' Sleep                         '2021/7/4 add ishi
#If VBA7 And Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal millisecond As Long)
#End If

以下を WebDriver.cls の CMD定義 の末尾に追加。

Private CMD_GET_TIMEOUTS         '2021/7/14 add ishi

以下を WebDriver.cls の InitCommands関数 の末尾に追加。

    CMD_GET_TIMEOUTS = Array("GET", "/session/$sessionId/timeouts")         '2021/7/14 add ishi

以下は待ち時間関連のMethod関数。

' ==========================================================================
' Wait operations
' ==========================================================================

' Implicitly Wait (millisecond)         '2021/6/24 add ishi
Public Function ImplicitlyWait(Optional millisecond As Double = 0, _
                               Optional ByVal sessionid As String = vbNullString)
    Dim data    As New Dictionary
    data.Add "implicit", millisecond
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Execute CMD_SET_TIMEOUTS, data
End Function

' Set Page Load Timeout (millisecond)   '2021/6/25 add ishi
Public Function SetPageLoadTimeout(Optional millisecond As Double = 300000, _
                                   Optional ByVal sessionid As String = vbNullString)
    Dim data    As New Dictionary
    data.Add "pageLoad", millisecond
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Execute CMD_SET_TIMEOUTS, data
End Function

' Set Script Timeout (millisecond)      '2021/7/14 add ishi
Public Function SetScriptTimeout(Optional millisecond As Double = 30000, _
                                 Optional ByVal sessionid As String = vbNullString)
    Dim data    As New Dictionary
    data.Add "script", millisecond
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Execute CMD_SET_TIMEOUTS, data
End Function

' Get Implicitly Wait (millisecond)     '2021/7/14 add ishi
Public Function GetImplicitlyWait(Optional ByVal sessionid As String = vbNullString) As Double
    Dim data    As New Dictionary
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Dim results
    Set results = Execute(CMD_GET_TIMEOUTS, data)

    GetImplicitlyWait = results.Item("implicit")
End Function

' Get Page Load Timeout (millisecond)   '2021/7/14 add ishi
Public Function GetPageLoadTimeout(Optional ByVal sessionid As String = vbNullString) As Double
    Dim data    As New Dictionary
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Dim results
    Set results = Execute(CMD_GET_TIMEOUTS, data)

    GetPageLoadTimeout = results.Item("pageLoad")
End Function

' Get Script Timeout (millisecond)      '2021/7/14 add ishi
Public Function GetScriptTimeout(Optional ByVal sessionid As String = vbNullString) As Double
    Dim data    As New Dictionary
    If sessionid <> vbNullString Then
        data.Add "sessionId", sessionid
    End If

    Dim results
    Set results = Execute(CMD_GET_TIMEOUTS, data)

    GetScriptTimeout = results.Item("script")
End Function

' Wait (millisecond)            '2021/7/4 add ishi
Public Function Wait(Optional millisecond As Long = 1000)
    Sleep millisecond
End Function
GCuser99 commented 2 years ago

Excellent work @ezagdd! I suggest to consider changing in all of the above:

Optional millisecond

to

Optional ByVal millisecond

In fact, I went through the entire code base and changed any input parameter that was not an object or variant (such as strongly-typed Double, Boolean, or String) to ByVal. Sorry I should have mentioned this before.... Many of my proposed modifications will raise an error if this is not done...

ghost commented 2 years ago

助言ありがとうございます。 ところで、ImplicitlyWait を SetImplicitlyWait に変更ですね。

Thank you for your advice. By the way, I changed ImplicitlyWait to SetImplicitlyWait.

ghost commented 2 years ago

Change method name ImplicitlyWaitto SetImplicitlyWait. WebDriver.cls

' Set Implicitly Wait (millisecond)     '2022/1/10 chg
Public Function SetImplicitlyWait(Optional ByVal millisecond As Double = 0, _
                                  Optional ByVal sessionId As String = vbNullString)
    Dim Data    As New Dictionary
    Data.Add "implicit", millisecond
    If sessionId <> vbNullString Then
        Data.Add "sessionId", sessionId
    End If

    Execute CMD_SET_TIMEOUTS, Data
End Function