gpt4 book ai didi

java - HtmlUnitDriver 浏览器模拟模式

转载 作者:行者123 更新时间:2023-12-01 22:11:47 25 4
gpt4 key购买 nike

我创建了一个简单的工厂方法,它以指定的模式创建 HtmlUnitDriver。例如

public static HtmlUnitDriver createHtmlUnitDriver(String browserMode) {
switch(browserMode.toLowerCase()) {
case "chrome":
return new HtmlUnitDriver(DesiredCapabilities.chrome());
case "firefox":
return new HtmlUnitDriver(DesiredCapabilities.firefox());
case "ie":
case "internet explorer":
return new HtmlUnitDriver(DesiredCapabilities.internetExplorer());
case default:
return new HtmlUnitDriver(true);
}
}

我想编写此方法的快速单元测试,但我不知道如何判断返回的 HtmlUnitDriver 所处的“模式”。我该怎么做?

最佳答案

使用反射,可以获取私有(private)字段webClient

@Test
public void test() throws Exception {
WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);
WebClient webClient = (WebClient) get(driver, "webClient");
System.out.println(webClient.getBrowserVersion());
System.out.println(webClient.getBrowserVersion().isIE());
driver.close();
}

private static Object get(Object object, String field) throws Exception {
Field f = object.getClass().getDeclaredField(field);
f.setAccessible(true);
return f.get(object);
}

关于java - HtmlUnitDriver 浏览器模拟模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com