27

I want to ask how to open the Chrome developer Console during selenium tests execution. Currently, when tests are executing, and I open the console manually hitting F12, the tests stop responding immediately and fails after some time.

Can anyone tell me how can I initiate my tests with developer console opened, so I can catch/observe the console errors that occur during test execution.

4 Answers 4

37

Use --auto-open-devtools-for-tabs:

This flag makes Chrome auto-open DevTools window for each tab. It is intended to be used by developers and automation to not require user interaction for opening DevTools.

Source

How to use

2
  • 8
    So how can I use this flag when launching chrome webDriver from my .js selenium script? Can I pass it somehow to the .withCapabilities() method of webdriver builder?
    – Jen
    Commented Jun 22, 2018 at 11:47
  • 4
    With ChromeOptions -> options.addArguments("--auto-open-devtools-for-tabs");
    – Tucker
    Commented Aug 21, 2020 at 20:01
12

Note: this answer does not apply to current versions of Chrome.

You can't. The Chrome driver uses the Chrome remote debugging protocol to communicate with the browser. This is the same protocol that the developer console uses also. Unfortunately, Chrome is designed so that only one client can be attached using the protocol at a time, so that means either the developer tools, or the driver, but not both simultaneously.

4
  • @Uziii logging!! no other go Commented Mar 30, 2016 at 14:37
  • 5
    You sure about this? I can press F12 as my Selenium script is running and it will open Chrome Dev Tools and the script will keep running...
    – mpen
    Commented Jul 3, 2018 at 23:16
  • 8
    The information in this answer was correct when it was written over two years ago. The Chrome team has, of course, continued to enhance the browser and its development tools in the intervening time, and I believe the debugging protocol now supports multiple clients.
    – JimEvans
    Commented Jul 4, 2018 at 13:52
  • 1
    @JimEvans I added a note to that effect to the answer, hope you don't mind, feel free to reword/whatever, but I thought it could do with something other than comments pointing out that things have moved on since, particularly given that the question is still relevant and this answer is relatively well upvoted.
    – CupawnTae
    Commented Jul 3, 2020 at 8:25
2

Have you tried simulating the key press events for the shortcut of opening the dev tools in Chrome?

String openDevTools = Keys.chord(Keys.ALT, Keys.CONTROL, "i");
driver.findElement(By.ByTagName("body")).sendKeys(openDevTools);

This is not ideal and in a rigorous testing regime you would need platform detection to ensure you are covering both Mac and Windows. I would absolutely recommend avoiding this (even if it works), but it's a possible as a work-around if you really must.

I have a feeling it may also lose focus of the window itself if you do this. If this is the case, you'd need something like the following: -

String parentHandle = driver.getWindowHandle(); // get the current window handle
// do your dev tool stuff here
driver.switchTo().window(parentHandle); // switch back to the original window

Hope this helps.

Useful link if it does get you anywhere: How to handle the new window in Selenium WebDriver using Java?

Edit: Just re-read the question and don't think this will work anyway. Your unit tests should capture errors in the logic of your code. Your selenium tests should only test user journeys and capture errors when the user journey is cut short. You should never be testing code logic/error throwing through a selenium test.

2

This is working for me in webdriver.io (wdio.conf.js)

const configs = {
  chrome : {
    maxInstances: "5",
    browserName: "chrome",
    chromeOptions: {
      args: ['--window-size=1280,800', '--auto-open-devtools-for-tabs'],
      binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
    }
  },
  firefox : {
    maxInstances: "5",
    browserName: "firefox"
  },
  headless : {
    maxInstances: "5",
    browserName: "chrome",
    chromeOptions: {
      args: ['--headless', '--disable-gpu', '--window-size=1280,800'],
      binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
    }
  },
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.