How to open Chrome Developer console in Selenium WebDriver using JAVA

前端 未结 4 2246
不思量自难忘°
不思量自难忘° 2020-11-28 14:05

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

4条回答
  •  [愿得一人]
    2020-11-28 14:54

    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.

提交回复
热议问题