javascript 为什么开发人员需要 PhantomJS 而不是仅仅使用一些测试框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12710578/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:56:35  来源:igfitidea点击:

Why would a developer ever need PhantomJS as opposed to just using some testing framework?

javascripttestingcoffeescriptphantomjs

提问by Leila Hamon

I'm not sure why PhantomJS is necessary and I'm hoping someone can help.

我不确定为什么 PhantomJS 是必要的,我希望有人可以提供帮助。

回答by anotherdave

JavaScript that requires a DOM needs someplace to run, normally in the browser. So test frameworks often fire up a browser which they then control in order to run the tests & carry out asserts. The test frameworks themselves often can't control the browser directly, so you end up with three layers to your test:

需要 DOM 的 JavaScript 需要在某个地方运行,通常在浏览器中。所以测试框架通常会启动一个浏览器,然后他们控制浏览器来运行测试和执行断言。测试框架本身通常无法直接控制浏览器,因此您的测试最终分为三层:

  • Framework: E.g. Jasmine
  • Driver: E.g. Selenium
  • Browser: E.g. Firefox
  • 框架:例如茉莉花
  • 驱动程序:例如硒
  • 浏览器:例如火狐

Simply, PhantomJS is a browser which runs headlessly (i.e. doesn't draw out the the screen). The benefits that brings is speed — if you're controlling an actual programme on your computer, you've a certain overhead in booting up the browser, configuring a profile etc.

简单地说,PhantomJS 是一个无头运行的浏览器(即不绘制屏幕)。带来的好处是速度——如果你在你的计算机上控制一个实际的程序,你在启动浏览器、配置配置文件等方面会有一定的开销。

PhantomJS is much faster at this (I've seen tests run in less than 1/3 the time). The only trade-off is that you cannot see your tests running, though that's not much of an issue for an automated suite! You could also take screenshots if needed (e.g. on failures). If you really wanted to, you could take screenshots at 24fps & send to ffmpeg to render out a video of your tests, though you may undermine your speed savings then :)

PhantomJS 在这方面要快得多(我已经看到测试运行的时间不到 1/3)。唯一的权衡是您看不到正在运行的测试,尽管这对于自动化套件来说不是什么大问题!如果需要(例如失败),您还可以截取屏幕截图。如果您真的想要,您可以以 24fps 的速度截取屏幕截图并将其发送到 ffmpeg 以呈现您的测试视频,尽管您可能会破坏您的速度节省 :)

回答by Ariya Hidayat

This is a common misunderstanding. Spending some time with the documentationusually clears the confusion. For example, PhantomJS is often used as a headless test runnerfor tests written in dozens of frameworks: Buster.JS, Capybara, Mocha, Jasmine, QUnit, WebDriver, YUI Test, and many more. As you can see here, PhantomJS does not replace those frameworks at all.

这是一个普遍的误解。花一些时间在文档上通常可以消除混乱。例如,PhantomJS 经常被用作无头测试运行器,用于用数十种框架编写的测试:Buster.JS、Capybara、Mocha、Jasmine、QUnit、WebDriver、YUI Test 等等。正如你在这里看到的,PhantomJS 根本没有取代这些框架。

Of course, there are many other uses of PhantomJS, everything from programmatically capture web pageto automated network monitoring. There is a listof various blog posts and articles which demonstrate different reported use cases of PhantomJS.

当然,PhantomJS 的用途还有很多,从编程捕获网页到自动网络监控应有尽有。有一个各种博客文章和文章的列表,它们展示了 PhantomJS 的不同报告用例。

回答by manzoid

PhantomJS is not meant to replace a testing framework, it will work in conjunction with one.

PhantomJS 并不是要取代一个测试框架,它会与一个测试框架一起工作。

For example, here's a random example PhantomJS used in conjunction with Jasmine.

例如,这是一个与 Jasmine 结合使用的随机示例PhantomJS

Also note that PhantomJS isn't just for testing, you can use it for things like screen-scraping or for off-loading heavy rendering load (say, for CPU-challenged mobile browsers) onto the server, as in this example.

另请注意,PhantomJS 不仅用于测试,您还可以将其用于屏幕抓取或将繁重的渲染负载(例如,对于 CPU 受到挑战的移动浏览器)卸载到服务器上,如本例所示

回答by BlaM

Another use case that has not been mentioned here is rendering a webpage and generating image or PDF snapshots of that page.

此处未提及的另一个用例是呈现网页并生成该页面的图像或 PDF 快照。

For example you could render an invoice, report, document, ... as HTML webpage and render it to PDF to send it to your end user as downloadable document.

例如,您可以将发票、报告、文档等呈现为 HTML 网页,并将其呈现为 PDF 以将其作为可下载文档发送给您的最终用户。

Or maybe you want screenshot thumbnails of a webpage? Another possible use case.

或者您可能想要网页的屏幕截图缩略图?另一个可能的用例。