doberkofler
doberkofler

Reputation: 10341

How can I see the browser log messages when running a selenium webdriverjs test

Is there a way to see the browser log messages (e.g. console.log) that a selenium webdriverjs test might generate?

Upvotes: 2

Views: 1723

Answers (1)

EJS
EJS

Reputation: 1071

The way I understand it you want to see the messages produced by the system under test? Try this:

    var logBrowserMessages = function(logEntries, driver, logging, cb) {
        return driver.manage().logs().get(logging.Type.BROWSER).then(function(entries) {
            entries.forEach(function(entry) {
                logEntries = logEntries + entry.level.name + ': ' + entry.message + '\n';
            });
            logEntries = logEntries + "\n";
            console.log('final entries:' + logEntries);
            return cb();
        });
    };

You will also need to require:

    var logging = require('selenium-webdriver').logging;

And set up the driver with logging preferances:

    var prefs = new logging.Preferences();
    prefs.setLevel(logging.Type.BROWSER, logging.Level.ALL);
    driver = new webdriver.Builder()
        .withCapabilities(webdriver.Capabilities.chrome())
        .setLoggingPrefs(prefs)
        .build();

Then you can use logBrowserMessages:

    var doStuff = function(driver, logging) {
        return logBrowserMessages('', driver, logging, function() {
                //Do stuff
            });
        });
    };

Upvotes: 5

Related Questions