webdriver-w3c
webdriver-w3c copied to clipboard
console.logs should be exposed
See this issue on a downstream project: https://github.com/quickstrom/quickstrom/issues/64
I'm not sure if this is obtainable purely via the API as all examples I've seen claiming to support this are using some sort of language SDK. Perhaps that would be an option for us if the API doesn't support it?
i.e., https://github.com/quickstrom/quickstrom/issues/64#issuecomment-757042812
Thank you for reporting this!
Because it isn't in the WebDriver spec proper, I'm hesitant to add built in support for getting console logs -- it will be too dependent on the remote end.
However, for chrome at least, it is possible to get these logs using existing features without too much extra work. We can specify a data directory in the capabilities passed to chromedriver with the --user-data-dir
flag (docs), and enable logging with --enable-logging --v=1
(docs). This will log everything chrome does (including console.log
output) to the file chrome_debug.log
in the specified data directory, which on my macos with Chrome 89 appears to be relative to the user's home directory. The console lines are distinguished by CONSOLE
appearing in the prefix.
Here is an example that works for me:
consoleLogChrome :: Capabilities
consoleLogChrome = defaultChromeCapabilities
{ _chromeOptions = Just $ defaultChromeOptions
{ _chromeArgs = Just ["--user-data-dir=datadir", "--enable-logging", "--v=1"]
}
}
demoConsoleLogChrome :: WebDriverT IO ()
demoConsoleLogChrome = do
navigateTo "https://www.google.com"
executeScript "console.error('im in ur logs')" []
logLines <- fmap lines $ liftIO $ readFile "~/datadir/chrome_debug.log" -- you'll need to expand ~ here
let lines = filter ("CONSOLE" `isInfixOf`) logLines
liftIO $ print lines
withChromedriver :: Capabilities -> WebDriverT IO () -> IO ()
withChromedriver caps acts = do
execWebDriverT chromeConfig $
runIsolated_ caps acts
return ()
Now running
withChromedriver consoleLogChrome demoConsoleLogChrome
should dump the JS log lines to stdout. Of course we don't have to print them; we could do whatever we want at that point.