javascript 如何在无头模式下运行浏览器(chrome、IE 和 firefox)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15289425/
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 run browsers(chrome, IE and firefox) in headless mode?
提问by Mukesh Kumar
I want to integrate JSTestDriver with jenkins-CI. problem is that the build server does not have display. is there a way to run browsers headlessly?
我想将 JSTestDriver 与 jenkins-CI 集成。问题是构建服务器没有显示。有没有办法无头运行浏览器?
Thanks
谢谢
采纳答案by ProgrammerGuy
If your server is Linux, as mentioned, you can't run IE. But you can run other browsers like Firefox. The trick is to install XVFB on your Jenkins server which allows Firefox to run headless. I've used this setup and it works well.
如果您的服务器是 Linux,如上所述,您将无法运行 IE。但是您可以运行其他浏览器,例如 Firefox。诀窍是在您的 Jenkins 服务器上安装 XVFB,它允许 Firefox 无头运行。我已经使用过这个设置并且效果很好。
http://www.alittlemadness.com/2008/03/05/running-selenium-headless/
http://www.alittlemadness.com/2008/03/05/running-selenium-headless/
回答by Facebook Staff are Complicit
Google Chrome now (Chrome 59 for Mac and Linux, Chrome 60 for Windows) has a headless mode built-in. It works cross-platform, and doesn't require any messing around with display devices, virtual machines, or anything like that. You can find a great Getting Started article by Eric Bidelman here, but here are the most key points:
Google Chrome 现在(适用于 Mac 和 Linux 的 Chrome 59,适用于 Windows 的 Chrome 60)内置了无头模式。它跨平台工作,不需要任何显示设备、虚拟机或类似的东西。您可以在此处找到Eric Bidelman 撰写的一篇很棒的入门文章,但这里是最关键的一点:
You must launch the Chrome binary (on macOS it is located inside the Google Chrome.app
bundle in the the Contents/MacOS
directory) using --headless
flag. It is currentlyalso necessary to add the --disable-gpu
flag, because GPU acceleration does not work properly in headless mode.
您必须使用标志启动 Chrome 二进制文件(在 macOS 上,它位于目录中的Google Chrome.app
包内Contents/MacOS
)--headless
。它是目前还需要添加--disable-gpu
标志,因为GPU加速不无头模式下正常工作。
chrome --headless --disable-gpu [url/path/flags...]
As with standard non-headless Chrome, your test runner can communicate with Chrome using the dev tools interface if it's enabled by the --remote-debugging-port=[SOME_PORT]
flag. However, if you'd just like to test it quickly by yourself you can use the --screenshot
or --dump-dom
flags to check out the contents of the page after loading.
与标准的非无头 Chrome 一样,您的测试运行器可以使用开发工具界面与 Chrome 通信,如果它由--remote-debugging-port=[SOME_PORT]
标志启用。但是,如果您只想自己快速测试它,您可以使用--screenshot
或--dump-dom
标志在加载后检查页面的内容。
chrome --headless --disable-gpu --dump-dom https://stackoverflow.com/
回答by rodolfojcj
Firefox, from version 55 on Linux and version 56 on Windows and macOS, now has a builtin headless mode.
Firefox 从 Linux 上的第 55 版和 Windows 和 macOS 上的第 56 版开始,现在具有内置的无头模式。
At this moment (september 2017) I'm using a GNU/Linux based distribution, with Firefox 55, and a console command like this works fine: firefox --headless http://www.wikipedia.org
, which is not something very useful, but it works because I see that the corresponding operating system process for Firefox keeps running.
此时(2017 年 9 月)我正在使用基于 GNU/Linux 的发行版,带有 Firefox 55,并且像这样的控制台命令可以正常工作:firefox --headless http://www.wikipedia.org
,这不是很有用,但它可以工作,因为我看到相应的操作系统Firefox 的进程继续运行。
This feature will gradually improve over time, so reading the official developers documentationis recommended, specially for those people interested in automated testing and continuous integration initiatives.
此功能会随着时间的推移逐渐改进,因此建议阅读官方开发人员文档,特别是那些对自动化测试和持续集成计划感兴趣的人。