【问题标题】:SeleniumLib extension throws duplicate keyword name errorSeleniumLib 扩展引发重复的关键字名称错误
【发布时间】:2018-12-18 15:00:26
【问题描述】:

我正在尝试编写 Selenium 库扩展来简化一些事情,但我碰壁了。这是我的 python 类:

    import uuid
    import time
    import re
    from robot.api.deco import keyword
    from robot.libraries.BuiltIn import BuiltIn
    from SeleniumLibrary import SeleniumLibrary

    class MySeleniumLibrary(SeleniumLibrary):
            # def __init__(self):
            #         BuiltIn().set_library_search_order(self, "SeleniumLibrary")

            @keyword('Select Checkbox')
            def select_checkbox(self, locator):
                    self.wait_until_page_contains_element(locator)
                    elementId = self.get_element_attribute(locator,"id")
                    if elementId=='':
                            elementId = uuid.uuid4()
                            self.assign_id_to_element(locator, elementId)
                    self.execute_javascript('$("#' + elementId + ':not(:checked)").click();')

当我运行测试时,它会在构建库时抱怨:

Creating keyword 'Select Checkbox' failed: Keyword with same name defined multiple times.

然后在尝试选中复选框时最终失败:

Keyword with same name defined multiple times.

我的设置部分中只引用了 mySeleniumLibrary.py。我也尝试设置图书馆搜索顺序,但没有奏效。请问您有什么想法可以实现吗?

谢谢!

【问题讨论】:

标签: python selenium robotframework


【解决方案1】:

实际上我想出了一个解决方案,如何覆盖 SeleniumLibary 的继承方法。技巧是删除 @keyword 行上的特定关键字名称。这是现在的样子:

    import uuid
    import time
    import re
    from robot.libraries.BuiltIn import BuiltIn
    from SeleniumLibrary.base import keyword
    from SeleniumLibrary import SeleniumLibrary

    class MySeleniumLibrary(SeleniumLibrary):

            def get_unique_element_id(self, locator):
                    self.wait_until_page_contains_element(locator)
                    elementId = self.get_element_attribute(locator,"id")
                    if elementId=='':
                            elementId = uuid.uuid4()
                            self.assign_id_to_element(locator, elementId)
                    return elementId

            @keyword
            def select_checkbox(self, locator):
                    elementId = self.get_unique_element_id(locator)
                    self.execute_javascript('$("#' + elementId + ':not(:checked)").click();')

=> 当我现在调用 Select Checkbox 关键字时,会调用我的方法而不是原始 SeleniumLibrary 的方法。

【讨论】:

    【解决方案2】:

    您正在导入from SeleniumLibrary import SeleniumLibrary

    其中包含 Select Checkbox 关键字,这就是您得到的原因:

    Creating keyword 'Select Checkbox' failed: Keyword with same name defined multiple times.
    

    您可以更改关键字的名称

    import uuid
    import time
    import re
    from robot.api.deco import keyword
    from robot.libraries.BuiltIn import BuiltIn
    from SeleniumLibrary import SeleniumLibrary
    
    
    class MySeleniumLibrary(SeleniumLibrary):
        # def __init__(self):
        #         BuiltIn().set_library_search_order(self, "SeleniumLibrary")
    
        @keyword('Select Checkbox Custom')
        def some_name(self, locator):
            self.wait_until_page_contains_element(locator)
            elementId = self.get_element_attribute(locator, "id")
            if elementId == '':
                elementId = uuid.uuid4()
                self.assign_id_to_element(locator, elementId)
            self.execute_javascript('$("#' + elementId + ':not(:checked)").click();')
    

    然后你的机器人框架测试应该是这样的

    *** Settings ***
    Library  MySeleniumLibrary.py
    
    *** Test Cases ***
    Test Keyword
       Open Browser  hyyp://google.com  chrome
       Select Checkbox Custom  xpath=SomeKidOFXPATHVALUE
    

    这将按预期工作。 请注意 Open Browser 关键字的工作原理,即使我没有在设置中仅引用 SeleniumLibrary,也只有 python 脚本,该脚本会提取所有关键字。

    有关哪些方法被视为关键字的更多信息,请查看What Methods are considered Keywords

    【讨论】:

    • 感谢您的回答 - 但如果可能,我想使用“选择复选框”关键字名称。似乎我想出了一种方法 - 通过在使用 @keyword 时删除特定的关键字名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2019-06-10
    • 2017-02-14
    • 2013-12-23
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多