1
votes

I have some robot keywords written in FlowKeywords.txt and it is been used in my Robot Test cases.

Can I call these keywords from a Python script?

I have checked this link but it includes importing the Python file in the robot test case and then call it from robot side.

I need to use these keywords in the Python script.

This is test.robot file

*** Settings ***

*** Test Cases ***
Test
    Example keyword

*** Keywords ***
Example keyword
    log    hello, world

Below is Python file runkw.py:

from robot.libraries.BuiltIn import BuiltIn

def call_keyword(keyword):
    return BuiltIn().run_keyword(keyword)

How can I call KW 'Example keyword' from the Python file itself?

2
Can you please provide a minimal reproducible example for the issue you're having? That way we can more specifically help you and it provides us with more insight in what your problem is about.A. Kootstra
Are you asking how to call it outside the context of a running test? If so, then no, you can't call robot keywords unless you are actually running robot.Bryan Oakley
Yeah that's what I was checking. Call it outside the context of running test!. What i thought is to have something like import 'keywordfile.txt' , call keyword ("Keyword name", parameters)Pranav
@BryanOakley: Are you certain, that it is impossible? Can you point to any source saying so?TheHowlingHoaschd
@TheHowlingHoaschd Clearly it's not completely impossible -- it's just software. You could reimplement or mock out the context created by robot which is required for keywords to run. However, that would take a lot of work, and there's no documentation to help you understand what you need to do or how you need to do it. At the end of the day you would probably end up recreating what robot already does, so what's the point? To run a robot keyword you need to run run_keyword, which requires an instance of BuiltIn which requires a special context object, which requires... etc.Bryan Oakley

2 Answers

1
votes

There seems to be no official support for running Robot keywords outside of a running Robot suite.

If you just want access to a console-like environment to execute commands for debugging, you can of course patch that into a keyword and run it from Robot. Here is my (rather clunky) implementation using the Dialogs library:

from robot.libraries.Dialogs import get_value_from_user
from robot.libraries.BuiltIn import BuiltIn

def keyword_console():
    """Console for executing keywords"""    
    while True:
        keyword = get_value_from_user("Enter a keyword to run.")
        result = BuiltIn().run_keyword(keyword)
        print(result)

Plug this keyword into a test case, and you will be prompted for keywords to run. This barebones version does not work with arguments. This keyword might also be of use:

def python_console():
    """Console for arbitrary Python code"""
    run_keyword = BuiltIn().run_keyword
    while True:
        statement = get_value_from_user("Enter a Python expression to execute.")
        result = exec(statement)
        print(result) 
0
votes

i am not sure if you can directly call a KW of robotfile from python. May be other can answer it.

However in case of not knowing how to do it , i can use python subprocess module to execute the command for me

so if you want to execute the KW 'Example keyword' of test.robot file from a python file , you can achieve it like below

runkw.py

from subprocess import Popen

#-t option help you to run specific  test , shell=true  will allow the command to run as a single statment i.e. pybot -t test running_kw_from_python.robot

p1=Popen(['pybot','-t','Test','running_kw_from_python.robot'],shell=True)

This will run test case 'Test' , which eventually run 'Example keyword'.