Pranav
Pranav

Reputation: 115

Call Robot Keyword from Python script

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?

Upvotes: 1

Views: 2844

Answers (2)

TheHowlingHoaschd
TheHowlingHoaschd

Reputation: 706

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) 

Upvotes: 1

pankaj mishra
pankaj mishra

Reputation: 2615

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'.

Upvotes: 0

Related Questions