Jump to content

Testing if a Variable is NULL


Recommended Posts

Hi

Hope whoever is reading this is having a great day 

I have a function which Tests an xpath equals what you want to test. I've been expanding this as originally the xpath was obtained only by innerText like below 

$ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'innerText')

However I've realised there are scenarios where $Element is not in innerText but value so I would need to obtain the value by:

$ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'value')

However in my current function it doesn't seem to work. Here is the segment 

$ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'innerText')
            if $ClipData = NULL Then
               WriteToConsoleAndLog('---Log: $ClipData is NULL on  _WD_ElementAction($sSession, $sElement, property, innerText)')
               WriteToConsoleAndLog('---Log: Will now retrieve $ClipData as VALUE instead of innerText: $ClipData =  _WD_ElementAction($sSession, $sElement, property, value)')
               $ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'value')
               EndIf

Am I using NULL correctly? Would a value (albeit an empty one) go into the $ClipData variable by using the below?

$ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'innerText')

This is the full function for those wanting to see it in context 

Func WebDriver_Test_Xpath($sSession, $Xpath, $TextToBeTested, $TestDescription, $X, $Y )
WriteToConsoleAndLog("-----------------------------------Entering WebDriver_Test_Xpath() function ------------------------------------------")
      $ClipData = ""
      Sleep(300)
      $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath)
      if @error then
         WriteToConsoleAndLog("ERROR Cannot see element for Xpath: " & $Xpath)
         WriteToConsoleAndLog("ERROR Test Description:  " & $TestDescription)
         WriteToConsoleAndLog("ERROR $Element equals:  " & $sElement)
         if $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath) Then
            WriteToConsoleAndLog("---LOG: Success at getting element 2nd time")
         Else
            WriteToConsoleAndLog("---ERROR: Failure at getting element 2nd time $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath)")
         endif
                 if $AttemptIfStatement_MouseClicks == true then
                  ClickALocation_ThenCopyTheSelection($X, $Y, 3)
                  $ClipData = ClipGet()
         endif
      Else
         $ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'innerText')
            if $ClipData = NULL Then
               WriteToConsoleAndLog('---Log: $ClipData is NULL on  _WD_ElementAction($sSession, $sElement, property, innerText)')
               WriteToConsoleAndLog('---Log: Will now retrieve $ClipData as VALUE instead of innerText: $ClipData =  _WD_ElementAction($sSession, $sElement, property, value)')
               $ClipData = _WD_ElementAction($sSession, $sElement, 'property', 'value')
               EndIf
         WriteToConsoleAndLog("---Log: Success on $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath) - 1st Else Statement")
         WriteToConsoleAndLog("---Log:$ClipData equals: " & $ClipData)
     EndIf
     _WD_HighlightElements($sSession, $sElement)
      TestVariableContainsInClipBoardOne($ClipData, $TextToBeTested, $TestDescription)
 WriteToConsoleAndLog("-----------------------------------End WebDriver_Test_Xpath() function ------------------------------------------")
Endfunc

 

Link to comment
Share on other sites

Here are some script snippets I use:

Func _ValueIsNull($value)

    $value = String($value)
    If IsString($value) And $value = "0" Or $value = "" Or $value = "-1" Or $value = "False" Or $value = "_FFCmd_Err" Or $value = "[]" Then Return 1

EndFunc

You can add in more conditions based on whatever libraries you're using.

Edited:

corrected "And-->Or" for some of the conditions as mentioned by @AspirinJunkie

removed _ValueIsNotNull since it can be done with a Not, as suggested by @mistersquirrle

Edited by noellarkin
corrected error
Link to comment
Share on other sites

@noellarkin Have you ever tested your function?:

Local $aTmp[0]

ConsoleWrite("0:" & @TAB & @TAB & _ValueIsNull(0) & @CRLF)
ConsoleWrite("NULL:" & @TAB & _ValueIsNull(NULL) & @CRLF)
ConsoleWrite('"":' & @TAB & @TAB & _ValueIsNull("") & @CRLF)
ConsoleWrite('-1:' & @TAB & @TAB & _ValueIsNull(-1) & @CRLF)
ConsoleWrite('False:' & @TAB & _ValueIsNull(-1) & @CRLF)
ConsoleWrite('[]:' & @TAB & @TAB & _ValueIsNull($aTmp) & @CRLF)

Func _ValueIsNull($value)

    $value = String($value)
    If IsString($value) And $value = "0" Or $value = "" Or $value = "-1" Or $value = "False" Or $value = "_FFCmd_Err" And $value = "[]" Then Return 1

EndFunc


If you want to check explicitly for NULL, then use IsKeyWord():

$x = Null ; "", 0

If IsKeyword($x) = 2 Then
    MsgBox(0, "", "$x IS NULL")
Else
    MsgBox(0, "", "$x IS NOT NULL")
EndIf



 

Link to comment
Share on other sites

14 minutes ago, noellarkin said:

Here are some script snippets I use

"Null" is probably the wrong terminology for those, since null is pretty specifically its own thing, and you can check that with just: IsKeyword: https://www.autoitscript.com/autoit3/docs/functions/IsKeyword.htm, return value of 2 means it's Null (specifically). What you're checking is just true/false conditions, basically.

Also is there some reason to have two separate functions instead of just doing:

Global $sFalse = "false"

ConsoleWrite('_ValueIsNull: ' & String(_ValueIsNull($sFalse)) & @CRLF)
ConsoleWrite('Not _ValueIsNull: ' & String(Not _ValueIsNull($sFalse)) & @CRLF)

Func _ValueIsNull($value)

    $value = String($value)
    If IsString($value) And ($value = "0" Or $value = "" Or $value = "-1" Or $value = "False" Or $value = "_FFCmd_Err" Or $value = "[]") Then Return 1

EndFunc   ;==>_ValueIsNull

I also fixed a couple issues in your snippet. You have "And $value = "[]"", which causes all the other conditions to fail, and I grouped the OR's in a parenthesis so it behaves more expected.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...