Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.logs should be exposed #26

Open
jleonard-r7 opened this issue Mar 17, 2021 · 1 comment
Open

console.logs should be exposed #26

jleonard-r7 opened this issue Mar 17, 2021 · 1 comment

Comments

@jleonard-r7
Copy link

See this issue on a downstream project: quickstrom/quickstrom#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., quickstrom/quickstrom#64 (comment)

@nbloomf
Copy link
Owner

nbloomf commented Mar 28, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants