22

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.

Uziii
  • 763
  • 7
  • 11
  • 26

4 Answers4

34

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

Gryu
  • 2,102
  • 2
  • 16
  • 29
  • 7
    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 Jun 22 '18 at 11:47
  • 3
    With `ChromeOptions` -> `options.addArguments("--auto-open-devtools-for-tabs");` – Tucker Aug 21 '20 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.

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • @Uziii logging!! no other go – Vignesh Paramasivam Mar 30 '16 at 14:37
  • 4
    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 Jul 03 '18 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 Jul 04 '18 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 Jul 03 '20 at 08: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.

Community
  • 1
  • 1
Shakespeare
  • 1,286
  • 9
  • 15
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'
    }
  },
}
Aaron
  • 1,024
  • 11
  • 11