neliCZka
neliCZka

Reputation: 945

Python in Robot framework - Se2Lib has no attribute 'execute'

in my Robot framework tests I need some custom python keywords (e.g. to hold CTRL key) And everything worked before I started refactoring my "big" custom class (but I did not really change anything in this part around hold CTRL). Now I am getting AttributeError: 'Selenium2Library' object has no attribute 'execute'
My code is:

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()

and I just call "hold ctrl" directly in robot keyword then, the keyword file has my custom class imported as Library (and other custom keywords work)... Any idea why it fails on the "execute" please?

Upvotes: 2

Views: 2615

Answers (2)

neliCZka
neliCZka

Reputation: 945

The problem was in ActionChains, because it needs webdriver instance, not Se2Lib instance. Webdriver instance can be obtained by calling _current_browser(). I reworked it this way and it works:

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()

Upvotes: 3

Jan Kovařík
Jan Kovařík

Reputation: 1582

What about something like this:

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()

Upvotes: 1

Related Questions