Vinaykumar Patel
Vinaykumar Patel

Reputation: 884

issue in creating custom keyword in python with Selenium2library

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.

Upvotes: 0

Views: 955

Answers (1)

Pekka
Pekka

Reputation: 2280

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

Upvotes: 2

Related Questions