Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass existing Webdriver object to custom Python library for Robot Framework

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?

like image 792
Anastasya Lundquist Avatar asked May 16 '14 20:05

Anastasya Lundquist


People also ask

How do I import a Python library into Robot Framework?

To import the Python script inside Robot, we use the keyword Library in the Robot file under ***settings*** . To call the function, we use <file_name> <dot> <function name> . To add keywords inside the function, we use the keyword decorator. Here, BuildIn().

How do I import selenium library into Robot Framework?

The solution is to add robot nature (Right click on the project > Robot Framework > Add Robot Nature to the project and add the selenium library in the red.

How do I import chrome drivers into Robot Framework?

From inside the test Show activity on this post. Download Chromedriver.exe from its official website and keep this .exe file in 'C:\Python27\Scripts' directory. Now mention this path as your environment variable eg. C:\Python27\Scripts\chromedriver.exe Now Restart your computer and execute your test case.

How do I import files into Robot Framework?

Starting from Robot Framework 5.0, variable files implemented using Python can also be imported using the module name `similarly as libraries`__. When using this approach, the module needs to be in the `module search path`_. All variables from a variable file are available in the test data file that imports it.


2 Answers

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}
like image 145
Bryan Oakley Avatar answered Sep 19 '22 16:09

Bryan Oakley


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.

like image 37
2 revs, 2 users 89% Avatar answered Sep 20 '22 16:09

2 revs, 2 users 89%