Forum Discussion

googleid_118035's avatar
googleid_118035
Contributor
11 years ago

How to find whether text fields is empty or not?

Hi,



I just need to check whether text fields of the web page is empty or not.

But is always return that text fields is not empty, even that text fields is empty.

Here is the sample code.




function test(){


if (My_Object != null){


Log.Message("Username field is not empty")


}


else {


Log.Message("Username field is empty")


}


}





..............

Thanks

Dev

  • Just wrote this example real quick that hopefully helps you understand TC/JScript in conjunction with what you hope to accomplish:







    //JScript example


    function main(){


    /*


     open notepad and key in the word "hi"


    */


    wshell = new ActiveXObject('WScript.Shell');


    wshell.run("notepad");


    var objToCheck = Sys.Process("notepad").Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1);


    objToCheck.Keys("hi");


    /*


     call log.message and log the result of checkObjectText function


      (with specified property value)


    */


    Log.Message("The function returned the result of: " + checkObjectText(objToCheck,"wText","hi"));


    /*


     call log.message and log the result of checkObjectText function


      (without specified property value and using a different property as an example)


    */


    Log.Message("The function returned the result of: " + checkObjectText(objToCheck,"Enabled"));


    }


     


    function checkObjectText(myObj,propName,propValue){


    /*


     verify that the supplied object has the specified property


    */


    if (aqObject.IsSupported(myObj,propName)){


     /*


      check to see if the objects property matches the specified propValue


      if no propValue is specified then check to see if the property value has a specified length


     */


     if (myObj[propName] == propValue || !propValue && myObj[propName].toString().length > 0){


      return true;


      }


     }


    /*


     no previous criteria was met, return false


    */


    return false;


    }


  • I use VBScript (I think your sample is JScript?) so I'm not 100% sure. But ....



    It looks like you are checking if the object variable is null. In other words you are saying:



    IF this is an OBJECT THEN

       Log "It's not empty"

       ELSE

       Log "It's empty"

    END IF



    I think you should be checking a property of the object. Usually .value from memory ....





  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Just wrote this example real quick that hopefully helps you understand TC/JScript in conjunction with what you hope to accomplish:







    //JScript example


    function main(){


    /*


     open notepad and key in the word "hi"


    */


    wshell = new ActiveXObject('WScript.Shell');


    wshell.run("notepad");


    var objToCheck = Sys.Process("notepad").Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1);


    objToCheck.Keys("hi");


    /*


     call log.message and log the result of checkObjectText function


      (with specified property value)


    */


    Log.Message("The function returned the result of: " + checkObjectText(objToCheck,"wText","hi"));


    /*


     call log.message and log the result of checkObjectText function


      (without specified property value and using a different property as an example)


    */


    Log.Message("The function returned the result of: " + checkObjectText(objToCheck,"Enabled"));


    }


     


    function checkObjectText(myObj,propName,propValue){


    /*


     verify that the supplied object has the specified property


    */


    if (aqObject.IsSupported(myObj,propName)){


     /*


      check to see if the objects property matches the specified propValue


      if no propValue is specified then check to see if the property value has a specified length


     */


     if (myObj[propName] == propValue || !propValue && myObj[propName].toString().length > 0){


      return true;


      }


     }


    /*


     no previous criteria was met, return false


    */


    return false;


    }



  • Hi,



    I use something like this and it works.



    function test(){


    if (My_Object.ContentText != null){


    Log.Message("Username field is not empty")


    }


    else {


    Log.Message("Username field is empty")


    }

    }



    Regards,

  • Hi Ryan,

     


    Its really help, thanks.


     


    here is my code and I used your function.

    ------------------------------------------------------------------------------


    function checkmyobject_fields(){


    var my_object = Sys.Browser("iexplore").Page("*").Find("ObjectIdentifier","cphMainContent_txtUserName", 2000, true);


    if (checkObjectText(my_object,"Text")==true){


    Log.Message("field is Not empty");


    }


    else {


    Log.Message("field is empty");


    }


    }


    function checkObjectText(myObj,propName,propValue){


        if (aqObject.IsSupported(myObj,propName)){


         if (myObj[propName] == propValue || !propValue && myObj[propName].toString().length > 0){


          return true;


          }


         }


        return false;


    }






    -----------------

    Thanks

    Dev