在使用WebDriver(HtmlUnit、Ruby绑定)时是否可以忽略JavaScript异常

在使用WebDriver(HtmlUnit、Ruby绑定)时是否可以忽略JavaScript异常,javascript,ruby,selenium,webdriver,htmlunit,Javascript,Ruby,Selenium,Webdriver,Htmlunit,HtmlUnit在加载页面时引发异常并使测试崩溃 caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true) driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps) driver.navigate.то url ReferenceError:未定义“x”。 (net.sourcef

HtmlUnit在加载页面时引发异常并使测试崩溃

caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)
driver.navigate.то url
ReferenceError:未定义“x”。 (net.sourceforge.htmlunit.corejs.javascript.EcmaError)

如果我使用Firefox驱动程序,则不会引发异常

caps = Selenium::WebDriver::Remote::Capabilities.firefox
或者禁用HtmlUnit驱动程序的JavaScript

caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => false)
我无法更改测试页面上的代码并修复问题,因此我需要忽略它,或者以任何方式使用Firefox JavaScript引擎而不是标准的HtmlUnit JavaScript引擎

有没有可能在不更改测试页面代码的情况下解决我的问题

更新: 尝试将水豚+WebKit作为Selenium+HtmlUnit的替代品-效果良好,没有错误。但是我仍然希望在不改变框架的情况下解决这个问题。

在查看之后,似乎不可能定制您想要改变的行为。解决这个问题最简单的方法是修补和重新编译Selenium服务器(这可能是一个选项,也可能不是)。您需要添加以下行:

--- HtmlUnitDriver.java 2012-01-05 17:45:22.779579136 +0100
+++ HtmlUnitDriver.java 2012-01-05 18:14:51.415106195 +0100
@@ -255,6 +255,7 @@
     WebClient client = newWebClient(version);
     client.setHomePage(WebClient.URL_ABOUT_BLANK.toString());
     client.setThrowExceptionOnFailingStatusCode(false);
+    client.setThrowExceptionOnScriptError(false);
     client.setPrintContentOnFailingStatusCode(false);
     client.setJavaScriptEnabled(enableJavascript);
     client.setRedirectEnabled(true);

我在.net世界发现了同样的问题

我在c#中通过使用反射和扩展方法解决了这个问题:

public static void SetThrowOnScriptErrors(this HtmlUnitDriver driver, 
                                          bool throwScriptErrors )
{
    object webClient =  driver.GetType().InvokeMember("_webClient",
                                    BindingFlags.GetField | 
                                    BindingFlags.NonPublic | 
                                    BindingFlags.Instance, null,
                                    driver, new object[0]);

    webClient.GetType().InvokeMember("throwExceptionOnScriptError_",
                                        BindingFlags.SetField | 
                                        BindingFlags.NonPublic | 
                                        BindingFlags.Instance,
                                        null, webClient, 
                                        new object[] {throwScriptErrors});
}

仅适用于Java: 在最新版本的
WebClient
(由
HTMLUnitDriver
包装)
客户端中,不推荐使用setThroweExceptionOnScriptError(false)
方法。如果子类化HTMLUnitDriver,则需要覆盖modifyWebClient方法:

public class MyHtmlUnitDriver extends HtmlUnitDriver {

...

 @Override
    protected WebClient modifyWebClient(WebClient client) {
        //currently does nothing, but may be changed in future versions
        WebClient modifiedClient = super.modifyWebClient(client);

        modifiedClient.getOptions().setThrowExceptionOnScriptError(false);
        return modifiedClient;
    }
}

我使用
HPUnit\u Extensions\u Selenium2TestCase
v1.4.0
解决了这个问题,如下所示:

class TestBase extends PHPUnit_Extensions_Selenium2TestCase
{
    public function setUp()
    {
        $this->setHost(<your-host>);
        $this->setPort(<your-port>);
        $this->setDesiredCapabilities(Array("javascriptEnabled"=>"false"));
类TestBase扩展了PHPUnit\u扩展\u Selenium2TestCase
{
公共功能设置()
{
$this->setHost();
$this->setPort();
$this->setDesiredCapabilities(数组(“javascriptEnabled”=>“false”);

此方法将永远关闭所有记录器

static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }


setFinalStatic(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.class.getDeclaredField("LOG"), new org.apache.commons.logging.Log() {

            });

基于@Vitaly的回答

    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import com.gargoylesoftware.htmlunit.WebClient;
    import java.util.logging.Logger;
    import java.util.logging.Level;
    public class MyHtmlUnitDriver extends HtmlUnitDriver {
        protected void modifyWebClient() {
            /* turn off annoying htmlunit warnings */
            Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
            WebClient newWebClient = getWebClient();
            newWebClient.getOptions().setThrowExceptionOnScriptError(false);
            newWebClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            newWebClient.getOptions().setPrintContentOnFailingStatusCode(false);
            modifyWebClient(newWebClient);
        }
    }

或者,您可以将HtmlUnitDriver子类化并覆盖受保护的newWebClient()。您好,我也有同样的问题。帮助([)[1]:基于设置最终字段的解决方案