【问题标题】:Retrieving console log from Firefox 43 with Selenium without Firefox extensions在没有 Firefox 扩展的情况下使用 Selenium 从 Firefox 43 检索控制台日志
【发布时间】:2015-12-23 14:08:41
【问题描述】:

是否可以使用 Selenium 和 Firefox 43 检索浏览器的 console.log?如果有,怎么做?

这是我的设置:

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.ALL);
logs.enable(LogType.DRIVER, Level.ALL);
logs.enable(LogType.CLIENT, Level.ALL);
logs.enable(LogType.PERFORMANCE, Level.ALL);
logs.enable(LogType.PROFILER, Level.ALL);
logs.enable(LogType.SERVER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);

FirefoxBinary binary = new FirefoxBinary(new File(...));
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(binary, profile, capabilities);

//...doing things with the driver ...

driver.manage().logs().get(LogType.BROWSER) // already tried every LogType

我从中得到的唯一输出是:

1450878255029   addons.xpi  DEBUG   startup
...
Error in parsing value for 'display'.  Declaration dropped.

但不是写在浏览器 javascript 控制台日志中的输出。

我已经尝试了几种 FF 配置文件设置,例如:

profile.setPreference("extensions.sdk.console.logLevel", "all");
profile.setPreference("webdriver.log.file",tempfile.getAbsolutePath());
profile.setPreference("webdriver.firefox.logfile", othertempfile.getAbsolutePath());
profile.setPreference("webdriver.log.driver", "ALL");

到目前为止没有任何帮助。 在 Chrome 中,这可以完美运行。

硒版本:2.48.2 火狐版本:43.0.2

【问题讨论】:

    标签: javascript java firefox selenium console.log


    【解决方案1】:

    我有同样的问题。我只得到了关于 CSS、安全、网络等的所有日志噪音,但没有得到应用程序通过 console.log 实际记录的内容。我正在为 webdriver 和 firefox 使用相同的版本。对于其他浏览器,这不是问题。

    我最终通过自定义日志记录扩展了我的客户端代码。用有线协议术语来说:

    • 使用execute 将类似以下内容放入客户端
    window.recordedLogs = [];
    
    console.log = function (message) {
    
        if (typeof message === 'object') {
            message = JSON.stringify(message);
        }
    
        window.recordedLogs.push(message);
    };
    
    • 使用execute 检索window.recordedLogs

    警告:上面的代码非常简单,它不处理传递给 log 方法的多条消息,也不处理其他 log 方法,如 info、error 等。

    但是,它可以是有线协议log 方法的一个很好的多浏览器兼容替代方案。

    【讨论】:

    • 问题:如果您正在测试多页流程,如何确保此代码实际注入每个页面?这就是我正在努力解决的问题
    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2019-09-07
    • 2022-06-11
    相关资源
    最近更新 更多