Selenium HtmlUnit驱动程序异常

时间:2013-11-02 17:46:48

标签: java selenium htmlunit htmlunit-driver

我正在使用JUnit,Selenium WebDriver并尝试使用HtmlUnit和http://vk.com网站进行一些测试但是失败了。 看起来FirefoxDriver对我很有用。
这是代码:

@Before
public void setUp() {
    DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();      
    capabilities.setBrowserName("Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0");
    capabilities.setVersion("24.0");
    capabilities.setJavascriptEnabled(true);
    driver = new HtmlUnitDriver(capabilities);  
    driver.manage().timeouts()
            .implicitlyWait(Integer.parseInt(TIMEOUT), TimeUnit.SECONDS);
    driver.get("http://vk.com");
}

@Test
public void SomeTest() {
    /* LoginPage - described class in PageObject traditions */
    LoginPage start = new LoginPage(driver);
    LOG.info(driver.getPageSource());   
    //...     
}

当我开始它时,我得到一个例外:

    org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking setInnerHTML
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info: host: 'hera', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10-3-amd64', java.version: '1.6.0_27'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:484)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:463)
    at vk.Execution.setUp(Execution.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

有什么建议吗? PS。这是黑盒测试我无法访问现场更改内部代码。

2 个答案:

答案 0 :(得分:0)

Different drivers have different ways to handle javascript. 

Htmlunit使用Rhino,firefox-driver使用Firefox的默认javascript处理程序,因此,您会看到不同驱动程序的不同结果。

答案 1 :(得分:0)

htmlUnit()替换为firefox()以修复此问题。

DesiredCapabilities capabilities = DesiredCapabilities.firefox()
  capabilities.setBrowserName("Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0");
  capabilities.setVersion("24.0");      
  driver = new HtmlUnitDriver(capabilities);