问题描述
在我的Robot框架测试中,我需要一些自定义的python关键字(例如,按住CTRL键),并且在重构“大”自定义类之前一切正常(但是在保持CTRL的这一部分中,我并没有真正更改任何内容)。
现在我得到AttributeError: 'Selenium2Library' object has no attribute 'execute'
我的代码是:
class CustomSeleniumLibrary(object):
def __init__(self):
self.driver = None
self.library = None
def get_webdriver_instance(self):
if self.library is None:
self.library = BuiltIn().get_library_instance('Selenium2Library')
return self.library
def get_action_chain(self):
if self.driver is None:
self.driver = self.get_webdriver_instance()
self.ac = ActionChains(self.driver)
return self.ac
def hold_ctrl(self):
self.get_action_chain().key_down(Keys.LEFT_CONTROL)
self.get_action_chain().perform()
然后我直接在robot关键字中调用“ hold ctrl”,然后关键字文件将我的自定义类导入为Library(其他自定义关键字起作用)...知道为什么它在“ execute”上失败了吗?
1楼
问题出在ActionChains中,因为它需要webdriver实例,而不是Se2Lib实例。 可以通过调用_current_browser()获得Webdriver实例。 我以这种方式对其进行了重做,并且有效:
def get_library_instance(self):
if self.library is None:
self.library = BuiltIn().get_library_instance('Selenium2Library')
return self.library
def get_action_chain(self):
if self.ac is None:
self.ac = ActionChains(self.get_library_instance()._current_browser())
return self.ac
def hold_ctrl(self):
actionChain = self.get_action_chain()
actionChain.key_down(Keys.LEFT_CONTROL)
actionChain.perform()
2楼
像这样的事情呢:
class CustomSeleniumLibrary(Selenium2Library):
def __init__(self):
super(CustomSeleniumLibrary, self).__init__()
def _parent(self):
return super(CustomSeleniumLibrary, self)
def hold_ctrl(self):
ActionChains(self._current_browser()).send_keys(Keys.LEFT_CONTROL).perform()