Forum Discussion

akalogeropoulos's avatar
akalogeropoulos
Occasional Contributor
8 years ago
Solved

Passing an array as function parameters from KeyWordTest?

Hi all,

I created a function to find a single object on vue from a webpage. If i call the function from script ex: "tblObject = findObject(["ObjectType","innerHTML"],["Table","*ckColumn*"],null);", it work perfectly but ièm not able to make it work when I call it from a keywordTest. I included the code of the function. I also included an screen capture of the parameters window from KeywordTest.

 

Best reagrds,

Anastasios

 

function findObject(mType,mFieldName,mObject)
{
  var myObject;
  var depth = 10;
   
    Sys.Refresh();
    
    if(mObject == null)
    {
      mObject = Sys.Browser("iexplore").Page(getURL());
      depth = 20;
    }
        
    myObject = mObject.Find(mType,mFieldName,depth,false);
         
    if(myObject.Exists == true)
    {
        return myObject;
    }
    else
    {
      Log.Message("The object with the " + myFieldName + " parameters was not found");
      return null;
    }
  CollectGarbage();   
}
  • tristaanogre's avatar
    tristaanogre
    8 years ago

    As far as I can tell, there's not a really easy way to pass an array in as a parameter to a script routine via a keyword test.  So, what I did is do a little detection within the function itself to determine if the parameter being passed is a string or if it is an array.  If it is a string, then use the eval function to convert to an array.  If it is not a string, I'm assuming it is an array.  Your function code would look something like this:

    function findObject(mType,mFieldName,mObject)
    {
      var myObject;
      var depth = 10;
       
        Sys.Refresh();
        if (typeof mType == 'string') {
            mType = eval(mType);
        }
        if (typeof mFieldName == 'string') {
            mFieldName = eval(mFieldName);
        }
        
        if(mObject == null)
        {
          mObject = Sys.Browser("iexplore").Page(getURL());
          depth = 20;
        }
            
        myObject = mObject.Find(mType,mFieldName,depth,false);
             
        if(myObject.Exists == true)
        {
            return myObject;
        }
        else
        {
          Log.Message("The object with the " + myFieldName + " parameters was not found");
          return null;
        }
      CollectGarbage();   
    }

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Some more information would be helpful.  You say, "it doesn't work".  Are there error messages you are getting in the log? Are you not getting the object you are looking for? Your screenshot has a parameter called "mySection" that is showing as undefined. I'm assuming that corresponds to the mObject parameter in your function.  If that's undefined, then your function will fail because mObject.Find won't exist. So, what is being passed in via "mySection" when you are running your test?


    • akalogeropoulos's avatar
      akalogeropoulos
      Occasional Contributor

      There is no error messages.

      When I call the function vai KeywordTest, the parameters is seen as String and not as an array.

      MySection is object the current vue from the webpage I'm testing.

       

      Thanks

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        As far as I can tell, there's not a really easy way to pass an array in as a parameter to a script routine via a keyword test.  So, what I did is do a little detection within the function itself to determine if the parameter being passed is a string or if it is an array.  If it is a string, then use the eval function to convert to an array.  If it is not a string, I'm assuming it is an array.  Your function code would look something like this:

        function findObject(mType,mFieldName,mObject)
        {
          var myObject;
          var depth = 10;
           
            Sys.Refresh();
            if (typeof mType == 'string') {
                mType = eval(mType);
            }
            if (typeof mFieldName == 'string') {
                mFieldName = eval(mFieldName);
            }
            
            if(mObject == null)
            {
              mObject = Sys.Browser("iexplore").Page(getURL());
              depth = 20;
            }
                
            myObject = mObject.Find(mType,mFieldName,depth,false);
                 
            if(myObject.Exists == true)
            {
                return myObject;
            }
            else
            {
              Log.Message("The object with the " + myFieldName + " parameters was not found");
              return null;
            }
          CollectGarbage();   
        }