현재 브라우저는 보호 된 WebDriverCache 필드에 저장됩니다. Selenium2Library를 확장하고 WebDriver를 노출 할 수는 있지만,이 간단한 사용 사례에서는 리플렉션을 사용하는 것이 좋습니다. 이렇게하면 원래 Selenium 라이브러리로 작업 할 수 있습니다. 다른 사람들은 다르게 느낄 수 있습니다. 나는 둘 다 보여줄 것이다.
두 솔루션은에서 결과를 가지고 도서관의 생성자이 통과 할 수있는 가져 오기 현재 브라우저 키워드 등이 여기에
당신이 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 키워드를 혼합, 그것을 사용하는 방법을 볼 수 있습니다
*** 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
// default package
import org.openqa.selenium.WebDriver;
public class MySelenium2Library extends Selenium2Library
{
public WebDriver getCurrentBrowser() {
return this.webDriverCache.getCurrent();
}
}
상호 작용 직접 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
내가 무슨 일이있어 물어 봐도 대신 (상속) 사용자 정의 Selenium2Library을 사용하려면, 여기에 예입니다 그것의 바람직한 방법 (상속 또는 반영)과 왜? – Floella