0

One of my team member created a custom keyword in python. that keyword uses the Selenium2Library's keyword. Here is the code that is places in "C:\Python27\Lib\site-packages\Selenium2Library\keywords_browsermanagement.py"

# Public, open and close
def select_popup(self):
    BuiltIn().sleep(3)
    handles = self._current_browser().get_window_handles()
    self._info("Window Names: %s " % handles)
    self._info("Pop Up Window being selected: %s " % handles[-1])
    print "in function"
    if len(handles) >= 1:
        self._current_browser().switch_to_window(handles[-1])

Now, everything works fine as long as this keyword select_popup is in _browsermanagement.py. I want to move this keyword in a separate file as I this is modifying the file which belong to Selenium2Library which is not good practice. Now when I put this in MyLib.py it gives me error when I start the test in RIDE. Here is the error message.

[ ERROR ] Error in file 'D:\Automation\My_Resource.robot': Importing test library 'D:\Automation\MyResources\my.py' failed: NameError: global name 'self' is not defined
Traceback (most recent call last):
  File "D:\Automation\MyResources\my.py", line 15, in <module>
    select_popup();
  File "D:\Automation\MyResources\my.py", line 8, in select_popup
    handles = self._current_browser().get_window_handles()

I think it is not finding reference to selenium2library's object. Can someone help me here isolating the custom python keyword to different file.

0

1 Answer 1

2

You should create your own library and inherit Selenium2Library. Something like this:

*** Settings ***
Library           MySelenium2Library
Suite Teardown    Close All Browsers

*** Test Cases ***
StackOverflow
    Open Browser    http://www.google.com/    Chrome

MySelenium2Library can be in same folder as your robot script and it would look like this:

from Selenium2Library import Selenium2Library

class MySelenium2Library(Selenium2Library):
    def select_popup(self):
        BuiltIn().sleep(3)
        handles = self._current_browser().get_window_handles()
        self._info("Window Names: %s " % handles)
        self._info("Pop Up Window being selected: %s " % handles[-1])
        print "in function"
        if len(handles) >= 1:
            self._current_browser().switch_to_window(handles[-1])

Update Aug-31-18

New versions of SeleniumLibrary seem to require @keyword decorator: Example in GitHub

New version of this library would look like this:

from SeleniumLibrary import SeleniumLibrary
from SeleniumLibrary.base import keyword

class MySeleniumLibrary(SeleniumLibrary):
    @keyword
    def select_popup(self):
        BuiltIn().sleep(3)
        handles = self._current_browser().get_window_handles()
        self._info("Window Names: %s " % handles)
        self._info("Pop Up Window being selected: %s " % handles[-1])
        print "in function"
        if len(handles) >= 1:
            self._current_browser().switch_to_window(handles[-1])
Sign up to request clarification or add additional context in comments.

4 Comments

One question though, in this case will it use the same object which was created by Selenium2Library before to open browser. I worked on TestNg +Selenium, in in such cases, I used to pass WebDriver object in the method. How is it going to work here since there is no objected created by user.
Because MySelenium2Library inherits Selenium2Library, it should work just as Selenium2Library except that it has your new keyword.
I implemented it as you suggested to override press_key but it still fetches the old keyword. It also doe not get new keyword from Selenium2Library import Selenium2Library class MySeleniumLibrary(Selenium2Library): def press_key(self, locator, key): if key.startswith('\\\\') and len(key) > 1: key = getattr(Keys,key[2:]) elif key.startswith('\\') and len(key) > 1: key = self._map_ascii_key_code_to_key(int(key[1:])) element = self.find_element(locator) element.send_keys(key) def anotherKw(self, locator, key): print "hello"
If you are using the latest versions of SeleniumLibrary, you need to decorate keywords. Look at my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.