2
votes

I have a maven-powered Robot-framework project in java that uses selenium 3.4.0 dependency, robotframework 3.0.2 dependency, markusbernhardt's selenium2library version 1.4.0.8, and robotframework-maven-plugin version 1.4.7.

My robot tests live in the src/main/test/robotframework/acceptance folder, while in src/main/java/mypackage I created a Customized.java file to set a system property for the browser driver path (I then import this library in my tests:

*** Settings ***
Library             Selenium2Library
Library             mypackage.Customized

This works perfectly. But now I'd like to implement my own keywords to extend Selenium2Library. But I'm not sure how to obtain the current WebDriver instance that is running.

I mean, if I weren't using Robot and just plain Selenium, I'd do something like this:

WebDriver driver=new ChromeDriver();
driver.doSomething();

However, in this case I don't want to instantiate a new WebDriver but instead get the one that is currently running (which Robot automatically instantiated). How can I do that?

I've so far created a Selenium2Library object and set it with the value returned by Selenium2Library.getLibraryInstance(); but that's not giving me access to selenium's methods (e.g.: getCurrentUrl() is not listed).

2

2 Answers

4
votes

in python it can be done with following code

from robot.libraries.BuiltIn import BuiltIn


def _browser(self):
    return BuiltIn().get_library_instance('Selenium2Library')._current_browser()
0
votes

Actually, I found a solution, but I'm not sure it's the right approach:

public class Customized {

    private static Selenium2Library s;
    private BrowserManagement b;
    private WebDriver driver;

    public Customized() throws ScriptException {
        try {
            Customized.s = (Selenium2Library) Selenium2Library.getLibraryInstance();
        } catch (javax.script.ScriptException e) {
            e.printStackTrace();
        }
        b = s.getBrowserManagement();
        driver=b.getCurrentWebDriver();
    }
}

Now the selenium methods are available from the driver object. However, there might be a better way to do it, as there's this message in the javax.script.ScriptException exception: "Access restriction: The type 'ScriptException' is not API (restriction on required library 'C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar')"