在我的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()
我直接在机器人关键字中调用“hold ctrl”然后,关键字文件将我的自定义类导入为Library(以及其他自定义关键字工作)... 知道为什么它在“执行”失败了吗?
答案 0 :(得分:3)
问题出现在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()
答案 1 :(得分:1)
这样的事情:
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()