How to invoke java method

Best practices, code snippets for common functionality, examples, and guidelines.
dimabs
Posts: 7
Joined: Thu Mar 24, 2011 5:49 pm

How to invoke java method

Post by dimabs » Fri Jun 17, 2011 6:45 pm

Hi,

I’m trying to automate some Java Applet functionality. I have an object within the applet, for which developers created couple APIs. How can I call those APIs against the object?

Thank you in advance

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Mon Jun 20, 2011 2:52 pm

You can call the API using following code snippet:
var result = <yourJavaElement>.Element.InvokeAction
             (“invokemethod”, <methodname>, <parameters>,...);
You can use following data types as parameters:
  • long
  • int
  • string
  • bool
  • null
With this method you should be able to invoke all public methods provided by your API.
Please make sure, that you are working with our latest Java plug in to make sure that everything works as expected from our blog:
http://www.ranorex.com/blog/automation- ... plications
WARNING: Since Ranorex 3.1 the Java plugin is already delivered with the Ranorex setup. See the User Guide on how to instrument your Java application for automation.

Regards,
Tobias
Support Team

dimabs
Posts: 7
Joined: Thu Mar 24, 2011 5:49 pm

Re: How to invoke java method

Post by dimabs » Tue Jun 21, 2011 12:47 am

Thank you. I will try it

Dima

dimabs
Posts: 7
Joined: Thu Mar 24, 2011 5:49 pm

Re: How to invoke java method

Post by dimabs » Thu Oct 13, 2011 6:22 pm

Here is the problem. I have a method to invoke, which was provided by developers as public. However, method's input parameter is object type of java.awt.Rectangle. Based on your explanation above, I can only use primitive variables to invoke the methods. So what is the possible solution for me?

Thank you,
Dima

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Fri Oct 14, 2011 12:24 pm

dimabs wrote:So what is the possible solution for me?
Sorry but currently you can only use the primitive ones as mentioned above. I will add a feature request to our internal bug tracking system to allow more objects.

Regards,
Peter
Ranorex Team

dimabs
Posts: 7
Joined: Thu Mar 24, 2011 5:49 pm

Re: How to invoke java method

Post by dimabs » Fri Oct 14, 2011 10:38 pm

Thanks. Hopefully, in the near future, it will be implemented, as it is a key for Java Applet UI based automation

Thank you,
Dima

dimabs
Posts: 7
Joined: Thu Mar 24, 2011 5:49 pm

Re: How to invoke java method

Post by dimabs » Wed Oct 19, 2011 1:16 am

What about a case scenario when involved java method returns an object, for instance Java Table?

Thank you,
Dima

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Fri Oct 21, 2011 1:14 pm

Hi,

This is not possible at the moment, you can just handle data types like int, string, bool, or null.
Peter has already added a feature request to support more data types.

Regards,
Markus
Ranorex Support Team

Prad
Posts: 12
Joined: Tue Feb 21, 2012 11:50 pm

Re: How to invoke java method

Post by Prad » Thu Apr 26, 2012 4:46 pm

Hi ,

Could you please provide some additional examples / sample code

on how exactly to use this ,
since based on the example / outline provided

the codes does not compile at all.

for eg.

how do i use this to call the IsPrinting function for the JideTable object ?

http://www.jidesoft.com/javadoc/com/jid ... lePrinting()


Regards,
Prad

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Fri Apr 27, 2012 3:17 pm

Hi,

Are you sure you sent us the right link, because I can't find the specific isPrinting method?
You can also try to use the InvokeMethod of the specific java control, for additional information please take a look at our .Net Api Documentation, just click, in Ranorex Studio, on Help->Api .Net Documentation.

Regards,
Markus
Ranorex Support Team

Prad
Posts: 12
Joined: Tue Feb 21, 2012 11:50 pm

Re: How to invoke java method

Post by Prad » Fri Apr 27, 2012 8:01 pm

The link is fine ,

its just that when you click on the link , it misses the closing parenthesis

http://www.jidesoft.com/javadoc/com/jid ... lePrinting()

that function was just for a reference as a test function

It would be of help if you can send me an example of how to use the InvokeMethod ,

since even when all the references are included , i am unable to resolve the InvokeMethod API ,
and get an error stating Ranonrex.Core.element does not contain a definition for "InvokeMethod"

Thanks & Regards,
Prad

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Mon Apr 30, 2012 10:29 am

Hi,

I created a small sample solution where you can see how to invoke a Java method. The files are attached.
Please also note that the API had changed, so the method Tobias mentioned had also changed, for additional information take a look at our online .Net Api.

Regards,
Markus
Ranorex Support Team
You do not have the required permissions to view the files attached to this post.

Prad
Posts: 12
Joined: Tue Feb 21, 2012 11:50 pm

Re: How to invoke java method

Post by Prad » Thu May 17, 2012 8:42 pm

Hi Markus ,

Thanks for the update , was able to use your example as reference and use this function .

but if the return value is an internal java object ,
is there an example on how to use /case that java object and call functions in that object .

should the definition of the returned object be loaded / created in the project ,

if yes ,
how to define or inherit java objects in ranorex ?


Thanks & Regards,
Pradeep

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: How to invoke java method

Post by Support Team » Fri May 18, 2012 3:32 pm

If the returned object is a java class instance,
the returned value is a JavaObjectWrapper (in Ranorex.Plugin namespace)

You can call the same methods (GetProperty, InvokeMethod, ...) on JavaObjectWrapper.
But this object has no representation in the Ranorex Element tree (like the JavaElement has)

For example:
var javaObj = (JavaObjectWrapper)javaElement.GetPropertyValue("YourComplexProperty");
var name = (string)javaObject.GetPropertyValue("SomeStringProperty");
//or, for example:
javaObject.InvokeMethod("DoSomething", 1, true);
Michael,
Ranorex Team

Prad
Posts: 12
Joined: Tue Feb 21, 2012 11:50 pm

Re: How to invoke java method

Post by Prad » Wed May 23, 2012 3:52 pm

Hi ,

Thanks for the response , it helps ,

but how would this work when the return value is just not one class instance
but an array of class instances ,

could you please provide some info/examples on how to loop on each object
and execute an invokeMethod/Invoke on each of the elements in the array

Thanks & Regards,
Pradeep