Forum Discussion

kor's avatar
kor
Occasional Contributor
7 years ago
Solved

Passing object from script to keyword test

Hi,

I wrote a script that returns a grid's cell object. I want to use it in keywords test to perform methods on it.
How to use such object in keyword test?

I created object variable that is set from the script but when I want to use it e.g. in 'Call Object Method' I get message that there is no object or it's unavailable or something like this. When I use this variable in 'Run Code Snippet' it works but I don't want to use that way.  

Any ideas/suggestion ?

  • The answer is: you can't.  The object needs to be referenced in some way as to be able to interact with it.  So, when your script returns an object, you need to have some way of getting to that object.  Assigning it to a variable is the only way I've found to be able to do this and a code snippet is the only way without implementing some other code (another script function for example) to perform the necessary action against it.

     

    Now... if you DO go the way of a custom keyword operation (which is basically writing another script function but encapsulating it), you could pass the retrieved object to that operation using "Last Results" item as I mentioned above.  I'd be curious to see what you come up with but, either way, you're going to write script code somewhere... either as a snippet, as a script routine, or as a script extension.

     

    May I ask... why do you want to avoid a code snippet?  If it's not broke, don't fix it...

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    What I would do is write your script code to return the object as the result of the script. Then you call the Script Routine and then use "Last Operation Result" to get the object.
    • kor's avatar
      kor
      Occasional Contributor

      Thanks, but in 'Call Object Method' operation or any other keyword test operation I can't see the way to select 'last operation result' when selecting object to perform action on. Could you give more details on that ?

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        kor wrote:

        Thanks, but in 'Call Object Method' operation or any other keyword test operation I can't see the way to select 'last operation result' when selecting object to perform action on. Could you give more details on that ?

         


        It is simple... first of all, you don't use "Call Object Method".  Your script code needs to look something like this:

         

        function findMyObject(someparameters) {
            var myObject;
            myObject = <some code to find the desired object>;
            return myObject;
        
        }

        Then, you do something like this:


         

        To explain what you're seeing...  I'm running the script routine to find my object.  Then I set a variable (type is "Object") to "Last Operation Result".  Then I call a code snippet to call the click method off the myVar variable.  

         

        This is a very simplistic demonstration of the concept.  I'd personally add a check to make sure that the function returned an actual onscreen object (check for "Exists") before I attempt to operate on it.

         

        Side note: If this was an urgent problem that needed rapid results, your best bet is not the forums.  We folks here do try to reply quickly... but I'm in the East Coast of the US and you just happened to catch me at a rare time when I was on after my normal hours.  So... yes, I was going to answer you... but it was going to take some time. :)