Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Chrome Developer console in Selenium WebDriver using JAVA

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.

like image 603
Uziii Avatar asked Mar 30 '16 14:03

Uziii


People also ask

How do I open Chrome Developer Tools in selenium?

As the name indicates it is a tool for developers which helps to analyse and debug. Chrome Developer Tool is available for Chrome and Chromium-based browser you can invoke this developer tool by using keyboard shortcut CTRL + SHIFT + I or F12, you can also invoke from User interface in Chrome Browser.

How do I open Java console in Chrome?

Chrome. Step 1: To open the console in Chrome, use this keyboard shortcut: Cmd + Option + J (on a Mac) or Ctrl +Shift +J (on Windows). As an alternative, you can right-click on the webpage and click "Inspect" to open the developer console.

How do I open developer console in Chrome?

To open the developer console in Google Chrome, open the Chrome Menu in the upper-right-hand corner of the browser window and select More Tools > Developer Tools. You can also use Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux).


3 Answers

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

like image 107
Алексей Запруднов Avatar answered Oct 17 '22 08:10

Алексей Запруднов


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.

like image 12
JimEvans Avatar answered Oct 17 '22 07:10

JimEvans


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.

like image 2
Shakespeare Avatar answered Oct 17 '22 07:10

Shakespeare