Skip to content

Instantly share code, notes, and snippets.

@customcommander
Created May 25, 2017 09:13
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save customcommander/fefa2e1a921d2646f5257f86a4caa18a to your computer and use it in GitHub Desktop.
Save customcommander/fefa2e1a921d2646f5257f86a4caa18a to your computer and use it in GitHub Desktop.
Capture console.log() output from a web page with Selenium WebDriver 2.53 and Chrome
require('chromedriver');
const path = require('path');
const wd = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
var builder = new wd.Builder();
var options = new chrome.Options();
var prefs = new wd.logging.Preferences();
var driver;
prefs.setLevel(wd.logging.Type.BROWSER, wd.logging.Level.ALL);
options.setLoggingPrefs(prefs);
driver = builder
.forBrowser(wd.Browser.CHROME)
.setChromeOptions(options)
.build();
driver
.get(`file://${path.resolve(__dirname, './page.html')}`)
.then(() => driver.manage().logs().get(wd.logging.Type.BROWSER))
.then((logs) => {
console.log(logs);
})
.then(() => driver.quit());
[ Entry {
level: Level { name_: 'INFO', value_: 800 },
message: 'file:///private/tmp/sample/page.html 3:20 "Apple"',
timestamp: 1495701249904,
type: '' },
Entry {
level: Level { name_: 'INFO', value_: 800 },
message: 'file:///private/tmp/sample/page.html 4:20 "Pear"',
timestamp: 1495701249904,
type: '' },
Entry {
level: Level { name_: 'WARNING', value_: 900 },
message: 'file:///private/tmp/sample/page.html 5:20 "Strawberry"',
timestamp: 1495701249904,
type: '' },
Entry {
level: Level { name_: 'SEVERE', value_: 1000 },
message: 'file:///private/tmp/sample/page.html 6:20 "Banana"',
timestamp: 1495701249904,
type: '' },
Entry {
level: Level { name_: 'SEVERE', value_: 1000 },
message: 'file:///private/tmp/sample/page.html 7:18 Uncaught Error: Orange',
timestamp: 1495701249904,
type: '' } ]
<html>
<body>
<script>
console.info('Apple');
console.log('Pear');
console.warn('Strawberry');
console.error('Banana');
throw new Error('Orange');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment