开发者

Getting Error generating libspec: Initializing library 'Shop' with no arguments failed in Robot Framework

I am new to Robot Framework and I am trying to import python custom library file named "Shop.py" saved in CustomeLibrary folder. I am getting this error in pycharm IDE. I am getting below error on this line "Library ../CustomeLibrary/Shop.py"

enter image description here

Getting below error: Unresolved library: ../CustomeLibrary/Shop.py. Error generating libspec: 开发者_JS百科Initializing library 'Shop' with no arguments failed: RobotNotRunningError: Cannot access execution context.

enter image description here

Below is the python custom library file:

from robot.api.deco import library, keyword
from robot.libraries.BuiltIn import BuiltIn
from SeleniumLibrary import SeleniumLibrary


@library
class Shop:
    # method name will be converted to keyword
    def __init__(self):
        self.selLib = BuiltIn().get_library_instance("SeleniumLibrary")

    @keyword
    def add_items_to_cart_and_checkout(self, productsList):
        i = 1
        productsTitles = self.selLib.get_webelements("xpath://*[@class='card-title']")
        for productsTite in productsTitles:
            if productsTitle.text in productsList:
                self.selLib.click_button("xpath:(//*[@class='card-footer'])["+str(i)+"]/button")

            i = i + 1

Can anyone help me to resolve this issue.

  • Other details: python version = python 3.10.1 Robot Framework 6.0.1 pip 22.3.1 robotframework-seleniumlibrary 6.0.0

I am trying to import the python custom library file in robot framework using "Library ../CustomeLibrary/Shop.py"

and I am getting below error: Unresolved library: ../CustomeLibrary/Shop.py. Error generating libspec: Initializing library 'Shop' with no arguments failed: RobotNotRunningError: Cannot access execution context

I am expecting that Shop.py should get successfully imported and i should be able to us the robot custom keyword "Add items to cart and checkout"


Similar problem is described here.

In order to obtain keyword names your __init__ is executed. Inside it you are trying to get instance of built-in library of running robotframework, but robot isn't running (check exception name).

You could replace self.selLib with property so your code could look something like that:

from robot.api.deco import library, keyword
from robot.libraries.BuiltIn import BuiltIn
from SeleniumLibrary import SeleniumLibrary


@library
class Shop:

    @property
    def selLib(self):
        return BuiltIn().get_library_instance("SeleniumLibrary")

    @keyword
    def add_items_to_cart_and_checkout(self, productsList):
        i = 1
        productsTitles = self.selLib.get_webelements("xpath://*[@class='card-title']")
        for productsTitle in productsTitles:
            if productsTitle.text in productsList:
                self.selLib.click_button("xpath:(//*[@class='card-footer'])["+str(i)+"]/button")

            i = i + 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜