我正在尝试编写Selenium Library扩展以减轻一些负担,但是我碰壁了。这是我的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。我也尝试设置库搜索顺序,但是没有用。您有任何想法如何实现这一目标吗?
谢谢!
答案 0 :(得分: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方法。
答案 1 :(得分:0)
您正在导入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
关键字的工作原理,即使我没有在设置中仅引用python脚本的设置中也引用了SeleniumLibrary。
有关哪种方法被视为关键字的更多信息,请查看此1