Created
May 25, 2017 09:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ 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: '' } ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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