16
votes

I am trying to create a custom Python library for Robot Framework, but I'm new to Python and Robot and I'm not sure how to accomplish what I'm trying to do. I want to pass the Webdriver object that Robot creates using Selenium2Library to my custom Python library so that I could use the Webdriver's methods, such as find_element_by_id. I've seen some suggestions about how to do it here and here, but they're for Java libraries - I can't find any Python instructions.

How would I go about doing this in Python? Or would I want to do this differently, without passing the Webdriver object?

3

3 Answers

25
votes

There's nothing built into the library to let you do what you want per se. However, you can create your own library that can access selenium features. There are two ways to accomplish this, both which require creating your own library in python. These methods are to to subclass Selenium2Library, or to get a reference to the Selenium2Library instance.

Creating a custom library that subclasses Selenium2Library

One way to access the internals of Selenium2Library is to write a library class that inherits from Selenium2Library. When you do that, you have access to everything in the original library. You can then return a reference to the WebDriver object, or you can just write your own keywords in python.

As an example, here's a custom selenium library that has a new keyword that will return the current WebDriver instance. It does this by calling the private (to the original Selenium2Library) method _current_browser. Since that's a private method, there's no guarantee it will stand the test of time, but at the time that I write this it exists.

Create a custom selenium library

First, create a new python file named CustomSeleniumLibrary.py. Put it where robot can find it -- the easiest thing is just put it in the same folder as a test suite that is going to use it. Put the following into that file:

from Selenium2Library import Selenium2Library

# create new class that inherits from Selenium2Library
class CustomSeleniumLibrary(Selenium2Library):
    # create a new keyword called "get webdriver instance"
    def get_webdriver_instance(self):
        return self._current_browser()

Create a testcase that uses the library

Next, write a test case that uses this instead of Selenium2Library. For example:

*** Settings ***
| Library | CustomSeleniumLibrary.py
| Suite Teardown | close all browsers

*** Test Cases ***
| Example using custom selenium library
| | Open browser | http://www.example.com | browser=chrome
| | ${webdriver}= | Get webdriver instance
| | log | webdriver: ${webdriver}

Run the test

Run the test as you would any other test. When it completes you should see something like this in the log:

16:00:46.887 INFO webdriver: <selenium.webdriver.chrome.webdriver.WebDriver object at 0x10b849410>

Using the object in a testcase

The cryptic ...<selenium....WebDriver object...> message proves that the variable actually holds a reference to the python WebDriver object. Using the extended variable syntax of robot you could then call methods and access attributes on that object if you want. I do not recommend doing it in this way, but I think it's really interesting that robot supports it:

| | log | The page title is ${webdriver.title}

Creating a custom library that references Selenium2Library

The second way to accomplish this is to use robot's method of getting an instance of a library, at which point you can access the object however you want. This is documented in the robot user guide; see Getting active library instance from Robot Framework in the Robot Framework User's Guide.

For example, the get_library_instance keyword from the above example would look like this:

from robot.libraries.BuiltIn import BuiltIn

def get_webdriver_instance():
    se2lib = BuiltIn().get_library_instance('Selenium2Library')
    return se2lib._current_browser()

Note that in this case you must include both the Selenium2Library and your custom library:

*** Settings ***
| Library | Selenium2Library
| Library | CustomSeleniumKeywords.py
| Suite Teardown | close all browsers

*** Test Cases ***
| Example using custom selenium keyword
| | Open browser | http://www.example.com | browser=chrome
| | ${webdriver}= | Get webdriver instance
| | log | webdriver: ${webdriver}
2
votes

Take a look at the Robot Framework User's Guide example:

Getting active library instance from Robot Framework

The biggest benefit over inheritance (as answered before), is that you can use the original library normally and use the new library in addition to it when needed.

-1
votes

You don't need robotf-ramework to "Open the browser" and create all steps in robot. You can just use robot as a sequencer and call from robot regular python scripts with selenium (install selenium with pip). Then, in robot you will have a test suite of regular small python scripts with selenium. This is a bit more advance regarding programming skills, but you will use more standard codding syntax in python scripts that robot keywords driven style. So, you can encapsulate all the details in python scripts and expose only high level to robot (via a test suite).