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}