Not able to create custom keyword in robot using python

29 Views Asked by At
from robot.api.deco import library,keyword
from robot.libraries.BuiltIn import BuiltIn

@library
class Shop:
    def __init__(self):
        self.selLib = BuiltIn().get_library_instance("SeleniumLibrary")

The last line is not working it is giving some error in robot file getting this error in the robot file

Should be able to create custom keyword

1

There are 1 best solutions below

0
rasjani On

That error does not come from Robot Framework. It is coming from robotcode LSP which is just a feature for your IDE. You wont get this error when actually running your code.

So, when robotcode scans your code - it will try to initialize your class in order to find out what keywords it will expose - But because its lsp, not robot framework, robot isnt running and thus and you are calling a feature that needs robot framework internals, you get that exception.

You could:

  1. catch RobotNotRunningError and not care about it because, essentially, you dont need a reference to SeleniumLibrary when Robot ain't running, something like this;
try:
  self.selLib = BuiltIn().get_library_instance("SeleniumLibrary")
except RobotNotRunningError:
  # safe to ignore in most cases
  pass
  1. Use SeleniumLibrary's plugin feature; https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#Plugins