Danp2 / au3WebDriver

Web Driver UDF for AutoIt
MIT License
108 stars 23 forks source link

_WD_GetFreePort fixes/improvements #457

Closed Danp2 closed 1 year ago

Danp2 commented 1 year ago

Pull request

Proposed changes

Implement fixes / improvement to _WD_GetFreePort

Checklist

Put an x in the boxes that apply. If you're unsure about any of them, don't hesitate to ask. We are here to help!
This is simply a reminder of what we are going to look for before merging your code.

Types of changes

Please check x the type of change your PR introduces:

What is the current behavior?

What is the new behavior?

Additional context

Add any other context about the problem here.

System under test

Please complete the following information.

mlipok commented 1 year ago

Will look at today or tomorrow

mlipok commented 1 year ago

I would also suggest to change _WD_GetFreePort usage in _WD_Startup()

here: https://github.com/Danp2/au3WebDriver/blob/b8bda9add2f875dc71083e38e53d6549724c7608/wd_core.au3#L1212-L1226

in this way:

    Call($sFunction, $_WD_PORT, Int($_WD_PORT) + 1000)
mlipok commented 1 year ago

or even better, check this following concept:

Func _WD_Startup()
    Local Const $sFuncName = "_WD_Startup"
    Local $sFunction, $bLatest, $sUpdate, $sFile, $iPID, $iErr = $_WD_ERROR_Success
    Local $sDriverBitness = "", $sExistingDriver = "", $sPortAvailable = "", $sMessage = ""

    If $_WD_DRIVER = "" Then
        Return SetError(__WD_Error($sFuncName, $_WD_ERROR_InvalidValue, "Location for Web Driver not set."), 0, 0)
    ElseIf Not FileExists($_WD_DRIVER) Then
        Return SetError(__WD_Error($sFuncName, $_WD_ERROR_FileIssue, "Non-existent Web Driver: " & $_WD_DRIVER), 0, 0)
    EndIf

    If $_WD_DRIVER_CLOSE Then __WD_CloseDriver()

    $sFunction = "_WD_GetFreePort"
    $sPortAvailable = Call($sFunction, $_WD_PORT, Int($_WD_PORT) + 1000)

    Select
        Case @error = 0xDEAD And @extended = 0xBEEF
            ; function not available

        Case @error = $_WD_ERROR_GeneralError
            $sMessage = " (Unknown error when tried to get free port)"

        Case @error
            $sMessage = " (Unavailable free port between " & $_WD_PORT & " and " & (Int($_WD_PORT) + 1000) & ")"

        Case Int($_WD_PORT) <> Int($sPortAvailable)
            $sMessage = " (Using free port " & $sPortAvailable & " instead " & $_WD_PORT & ")"
            $_WD_PORT = $sPortAvailable

    EndSelect

    Local $sCommand = StringFormat('"%s" %s ', $_WD_DRIVER, $_WD_DRIVER_PARAMS)

    $sFile = __WD_StripPath($_WD_DRIVER)
    $iPID = ProcessExists($sFile)

    If $_WD_DRIVER_DETECT And $iPID Then
        $sExistingDriver = "Existing instance of " & $sFile & " detected! (PID=" & $iPID & ")"
    Else
        $iPID = Run($sCommand, "", ($_WD_DEBUG >= $_WD_DEBUG_Info) ? @SW_SHOW : @SW_HIDE)
        If @error Or ProcessWaitClose($iPID, 1) Then $iErr = $_WD_ERROR_GeneralError
    EndIf

    If $_WD_DEBUG >= $_WD_DEBUG_Info Or ($iErr <> $_WD_ERROR_Success And $_WD_DEBUG = $_WD_DEBUG_Error) Then
        $sFunction = "_WD_IsLatestRelease"
        $bLatest = Call($sFunction)

        Select
            Case @error = 0xDEAD And @extended = 0xBEEF
                $sUpdate = "" ; update check not performed

            Case @error
                $sUpdate = " (Update status unknown [" & @error & "])"

            Case $bLatest
                $sUpdate = " (Up to date)"

            Case Not $bLatest
                $sUpdate = " (Update available)"

        EndSelect

        Local $sWinHttpVer = __WinHttpVer()
        If $sWinHttpVer < "1.6.4.2" Then
            $sWinHttpVer &= " (Download latest source at <https://raw.githubusercontent.com/dragana-r/autoit-winhttp/master/WinHttp.au3>)"
        EndIf

        If _WinAPI_GetBinaryType($_WD_DRIVER) Then _
                $sDriverBitness = ((@extended = $SCS_64BIT_BINARY) ? (" (64 Bit)") : (" (32 Bit)"))

        __WD_ConsoleWrite($sFuncName & ": OS:" & @TAB & @OSVersion & " " & @OSType & " " & @OSBuild & " " & @OSServicePack)
        __WD_ConsoleWrite($sFuncName & ": AutoIt:" & @TAB & @AutoItVersion)
        __WD_ConsoleWrite($sFuncName & ": Webdriver UDF:" & @TAB & $__WDVERSION & $sUpdate)
        __WD_ConsoleWrite($sFuncName & ": WinHTTP:" & @TAB & $sWinHttpVer)
        __WD_ConsoleWrite($sFuncName & ": Driver:" & @TAB & $_WD_DRIVER & $sDriverBitness)
        __WD_ConsoleWrite($sFuncName & ": Params:" & @TAB & $_WD_DRIVER_PARAMS)
        __WD_ConsoleWrite($sFuncName & ": Port:" & @TAB & $_WD_PORT & " " & $sMessage)
        __WD_ConsoleWrite($sFuncName & ": Command:" & @TAB & (($sExistingDriver) ? $sExistingDriver : $sCommand))
    EndIf

    $sMessage = ($iErr) ? ("Error launching WebDriver!") : ("")
    Return SetError(__WD_Error($sFuncName, $iErr, $sMessage), 0, $iPID)
EndFunc   ;==>_WD_Startup