如何使用 JAVA 在 Selenium WebDriver 中打开 Chrome 开发者控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36311191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Chrome Developer console in Selenium WebDriver using JAVA
提问by Uziii
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.
我想问一下如何在selenium测试执行期间打开Chrome开发者控制台。目前,当测试正在执行时,我手动按 F12 打开控制台,测试会立即停止响应并在一段时间后失败。
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.
谁能告诉我如何在打开开发人员控制台的情况下启动我的测试,以便我可以捕获/观察测试执行期间发生的控制台错误。
回答by JimEvans
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.
你不能。Chrome 驱动程序使用 Chrome 远程调试协议与浏览器进行通信。这也与开发者控制台使用的协议相同。不幸的是,Chrome 被设计为一次只能使用该协议连接一个客户端,因此这意味着要么是开发人员工具,要么是驱动程序,但不能同时连接。
回答by Shakespeare
Have you tried simulating the key press events for the shortcut of opening the dev tools in Chrome?
您是否尝试过模拟按键事件作为在 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.
这并不理想,在严格的测试制度中,您需要平台检测以确保您覆盖 Mac 和 Windows。我绝对建议避免这种情况(即使它有效),但如果您真的必须这样做,它也可以作为一种解决方法。
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?
有用的链接,如果它确实可以带您到任何地方: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.
编辑:只需重新阅读问题,不要认为这无论如何都行得通。您的单元测试应该捕获代码逻辑中的错误。您的 selenium 测试应该只测试用户旅程并在用户旅程缩短时捕获错误。您永远不应该通过 selenium 测试来测试代码逻辑/错误抛出。
回答by Алексей Запруднов
Use --auto-open-devtools-for-tabs
:
使用--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.
此标志使 Chrome 为每个选项卡自动打开 DevTools 窗口。它旨在供开发人员和自动化使用,无需用户交互即可打开 DevTools。
回答by Aaron
This is working for me in webdriver.io (wdio.conf.js)
这在 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'
}
},
}