RobotFramework Selenium2库和外部库-是否通过webdriver?
我创建了一个利用SeleniumWeb驱动程序实例的Java库。我想运行我用这个库编写的测试,以及Selenium2库。在某种程度上,Java库会添加一些我需要的功能(使用Ajax元素),但大部分测试都可以使用Selenium2关键字编写 有没有办法将Selenium2Library中实例化的webdriver传递给我的外部库,以便它们可以运行相同的测试RobotFramework Selenium2库和外部库-是否通过webdriver?,robotframework,Robotframework,我创建了一个利用SeleniumWeb驱动程序实例的Java库。我想运行我用这个库编写的测试,以及Selenium2库。在某种程度上,Java库会添加一些我需要的功能(使用Ajax元素),但大部分测试都可以使用Selenium2关键字编写 有没有办法将Selenium2Library中实例化的webdriver传递给我的外部库,以便它们可以运行相同的测试 谢谢你的意见 当前浏览器存储在受保护的WebDriverCache字段中。您可以扩展Selenium2库并公开WebDriver,但我认为在这
谢谢你的意见 当前浏览器存储在受保护的WebDriverCache字段中。您可以扩展Selenium2库并公开WebDriver,但我认为在这个简单的用例中,您最好使用反射。通过这种方式,您可以使用原始的Selenium2库。其他人可能会有不同的感觉。我将演示这两种方法 这两种解决方案都提供了一个Get Current Browser关键字,您可以从中获取结果并将其传递到库的构造函数中,等等 这里是一个带有关键字的库,该关键字将使用反射来访问WebDriverCache并将其公开:
// default package
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 {
public static void goToGoogle() {
getCurrentBrowser().get("http://www.google.com");
}
public static WebDriverCache getWebDriverCache() {
try
{
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);
}
}
public static WebDriver getCurrentBrowser() {
return getWebDriverCache().getCurrent();
}
private static Object getLibraryInstance(String library) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
engine.put("library", library);
engine.eval("from robot.libraries.BuiltIn import BuiltIn");
engine.eval("instance = BuiltIn().get_library_instance(library)");
return engine.get("instance");
}
}
下面您可以看到它是如何使用的,将Selenium2Library关键字与Misc中的关键字混合使用:
*** Settings ***
Test Teardown Close All Browsers
Library Selenium2Library
Library Misc
*** Test Cases ***
Get Current Browser Test
Open Browser http://slashdot.org
Go To Google
Title Should Be Google
如果您想改用自定义Selenium2库(继承),下面是一个示例:
// default package
import org.openqa.selenium.WebDriver;
public class MySelenium2Library extends Selenium2Library
{
public WebDriver getCurrentBrowser() {
return this.webDriverCache.getCurrent();
}
}
直接从Robot Framework与WebDriver实例交互,以简化示例:
*** Settings ***
Test Teardown Close All Browsers
Library MySelenium2Library
*** Test Cases ***
Get Current Browser Test
Open Browser http://slashdot.org
${driver}= Get Current Browser
Call Method ${driver} get http://www.google.com
Title Should Be Google
我能问一下做这件事的首选方式是什么(继承还是反射)吗?为什么?