0

I've done a decent amount of work in Java (enough to say I'm literate), and only started learning Python because my company works in Python-based Robot Framework. However, I'm starting to realize that certain things that are elementary in Java are (seemingly, allowing for lack of experience) quite difficult in Python. Is it possible, if so how, to do one of the following things:

  • A: Call a Java-based Robot Framework library in the *** Settings *** section, along with my Python-based libraries, or...
  • B: Call a Python-based keyword in Robot Framework, which then allows me to call Java-based keywords in another file?

Here is approximately what I'm looking to do for option A:

*** Settings ***
Library     MyPythonLibrary.py
Library     MyJavaLibrary.java

Here is the Robot Framework side of what I'm looking to do for option B:

*** Settings ***
Library     JavaPythonIntegratingLibrary

Here's the bulk of the Python side of what I'm looking to do for option B:

class JavaPythonIntegratingLibrary(object):
    def __init__(self):
        self.selenium_lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
    def java_keyword_name_1(self, *otherinputs):
        # This is where I'd put the code to execute Java code, which would return a boolean argument for pass/fail.
        if pass == True:
            BuiltIn().pass_execution()
        else:
            BuiltIn().fail()

I found JPype, the accepted answer to the only (with allowance for duplicate questions) other question like this, but JPype doesn't seem to be usable for Python 2.7.13, the version my company uses. Jython is not viable as my company is pretty set on Python 2.7.13 as the base for their Robot Framework.

I realize that this question treads very close to the line of "recommend or find a book, tool, software library, tutorial or other off-site resource", so if this can be accomplished through Robot Framework, Python, or Java with a simple "this is how I'd fill in the blanks", then that is the answer that I would prefer. If it cannot, or if you think that this question is getting a little too close to that line and you can recommend a better site to take this question to, I won't be offended and the question can be moved to that site.

2
  • 1
    Have you tried using your java keywords via the remote library interface? What is the nature of the java code -- is it just some existing java-based functions, or are you wanting to use selenium from both java and python at the same time? is it acceptable if the java based functions are executing in a different process from the robot test runner? Commented Sep 5, 2017 at 23:05
  • Not yet, that concept is brand new to me. I'll try it and if it works, I'll answer my own question. It is just some existing java-based functions. I don't necessarily need to use Selenium from Java if I can send values from Java to Robot Framework, and from there to Python. It is probably fine if they're executing in a different process. Commented Sep 5, 2017 at 23:08

1 Answer 1

1

You can use the remote library interface

What you have to do is create a small XMLRPC server that can run your Java code. You can then either run that server before starting your test or have your test start it in a suite setup.

Once the server is running, you use it much like you use any other library. For example, assuming you have your keyword server running on port 8270, you would import it into a test suite like this:

Library    Remote    http://127.0.0.1:8270

Once you've done that, any keywords implemented by the server can be run as if they were normal or Python keywords.

If you need to call these functions from Python, you can use the BuiltIn keyword 'run keyword' via the Python bindings. For example:

from robot.libraries.BuiltIn import BuiltIn
def custom_keyword():
    BuiltIn().run_keyword("my java keyword", "arg1", "arg2")

Useful links:

Sign up to request clarification or add additional context in comments.

1 Comment

I'll make sure to accept this answer as soon as I try it and it works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.