Forum Discussion

Lee_M's avatar
Lee_M
Community Hero
4 years ago

test for empty var

Keyword tests - how can I check for an empty/null string var (doesn't exist)

 

some functions fail at a later point in my test because the string is empty

I would like to test for this conditions and if found set the varialbe to a single space

 

I have tried create an if statement to test for "" or null but is does appear to work correct and is always skipping over this test

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    What scripting language your test project is based on?

     

    • Lee_M's avatar
      Lee_M
      Community Hero

      Alex,

       

      I am using Keywords tests but project are Javascript based

       

      in the KW test I have if the on-screen object Equals "" Or Equals null but this doesn't work

       

      I was looking to avoid writing another code snippet but the JS code would be something like

       

      if (!var) {

      //do this

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        I am not an expert with JavaScript, but to my understanding when a variable is assigned empty string or null value then this means that it is not empty.

        You may consider aqObject.EmptyVariant or classic check like

        if ('undefined' == typeof(<parameter>))

         

        P.S.

        if ('undefined' == typeof(<parameter>)) ' condition will not be met for null and empty string and will meet if parameter is missed at all

         

        if (!var) ' this will work for null but not for empty string if I am not mistaken

        I.e. the former check should be used when null is acceptable value for the parameter and the latter when it is not.