How to open Chrome Developer console in Selenium WebDriver using JAVA

前端 未结 4 2228
不思量自难忘°
不思量自难忘° 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:46

    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

    0 讨论(0)
  • 2020-11-28 14:53

    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'
        }
      },
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 15:00

    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.

    0 讨论(0)
提交回复
热议问题