Search code examples
python-2.7robotframeworkwebautomation

how to instantiate the webdriver object from the custom library when doing web automation using robot framework


while defining user keywords in custom library for web automation,which library should be imported?selenium2library or importing webdriver from selenium.How to use the webdriver to click on some elements.Kindly explain with an example


Solution

  • In most scenarios you do not need to instantiate the webdriver object. Usually you use the webdriver instance that Selenium2Library already has. How you access that instance depends on how you plan on interacting with Selenium2Library. See the "Extending existing test libraries" section in the user guide for options. Each options have pros and cons.

    If you inherit Selenium2Library, then you would access the driver via self._current_browser().

    If you plan on using the Selenium2Library directly instead of inheriting, you would declare both Selenium2Library and your custom libraries. The most convenient way to access the driver is through a private property as demonstrated below.

    from robot.libraries.BuiltIn import BuiltIn
    
    class Selenium2LibraryExt(object):
    
        @property
        def _s2l(self):
            return BuiltIn().get_library_instance('Selenium2Library')
    
        @property
        def _driver(self):
            return self._s2l._current_browser()
    
        def perform_search(self, criteria):
            textbox = self._driver.find_element_by_name('q')
            textbox.send_keys(criteria)
            textbox.submit()
    

    Test suite file:

    *** Settings ***
    Test Teardown     Close All Browsers
    Library           Selenium2Library
    Library           c:/ws/Selenium2LibraryExt.py
    
    *** Test Cases ***
    Do a search
        Open Browser    http://www.google.com/    gc
        Perform Search    happiness