I prefer to not have to subclass a "stock" library.
Below you will find a custom library that allows you to put a WebDriver instance into Selenium2Library's cache as well as retrieve the current WebDriver instance. Not thoroughly tested, but it does work. The register method is the one you need.
Hopefully Markus will not be angry at me for posting this.
import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.robotframework.selenium2library.keywords.BrowserManagement;
import org.robotframework.selenium2library.utils.WebDriverCache;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Misc
{
private static WebDriverCache cache = null;
public static String register(WebDriver webDriver, String alias)
{
return getWebDriverCache().register(webDriver, alias);
}
public static void goToGoogle()
{
}
public static WebDriverCache getWebDriverCache()
{
try
{
if (cache != null)
{
return cache;
}
else
{
BrowserManagement bm = (BrowserManagement) getLibraryInstance("Selenium2Library");
Field cacheField = BrowserManagement.class.getDeclaredField("webDriverCache");
cacheField.setAccessible(true);
return (WebDriverCache) cacheField.get(bm);
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
private static WebDriver getCurrentBrowser() {
return getWebDriverCache().getCurrent();
}
private static Object getLibraryInstance(String library) throws ScriptException
{
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
engine.eval("from robot.libraries.BuiltIn import BuiltIn");
engine.eval(String.format("lib = BuiltIn().get_library_instance('%s')", library));
return engine.get("lib");
}
}