1

I'm new to robotframework and python. Recently I'd try to create custom keyword in python file and then import it by robotframework. It can import normally but I can't call method in python file as a keyword. Here is my example

from robot.api import logger    
from robot.utils.asserts import fail    
from robot.utils import get_link_path    
from robot.libraries.BuiltIn import BuiltIn    
from robot.api.deco import keyword

class MyCustomClass():

    ROBOT_LIBRARY_VERSION = 1.0
    
    def __init__(self):
        pass

    def justprint(self):
        print('HelloWorld')

I try to call justprint as a keyword but can't.Here is the path in robotframework to call this .py file (../Web/02_RobotScriptExtract/Custom_Selenium_Keywords.py) It doesn't turn red, and I call it as library.

I assume that it is because of library which I imported, any ideas?

1 Answers1

0

You are trying to import the library as a Robotframework library, hence the proper way to do it in a test case file will be: (Note that the class have to be in the PYTHONPATH)

*** Settings ***
Library  MyCustomClass

*** Test Cases ***
Test case name
    justprint

If you just want to add a python file and use the functions from this file as keywords, then do:

*** Settings ***
Library  path_to_file/MyCustomPythonFile

*** Test Cases ***
Test case name
    justprint

In this case, your MyCustomPythonFile file will only contain the function. There is no need to have a class:

def justprint(self):
        print('HelloWorld')

Please note that the fact that you do Ctrl + Space and there is no autocompletion doesn't mean that the keyword is not imported. It could be a misconfiguration with your IDE. Instead, run the test case to verify.

hfc
  • 614
  • 1
  • 5
  • 13