HtmlUnitDriver(HtmlUnit)和GhostDriver(PhantomJS)?

编程入门 行业动态 更新时间:2024-10-28 13:14:03
本文介绍了HtmlUnitDriver(HtmlUnit)和GhostDriver(PhantomJS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在选择 headless浏览器驱动程序解决方案,该解决方案将是Selenium WebDriver的某些实现. 有 GhostDriver ,它利用了 PhantomJS 在一侧的后端和 HtmlUnitDriver (基于 HtmlUnit .

PhantomJS使用Safari的呈现引擎WebKit来呈现页面,而HtmlUnitDriver使用其他浏览器都没有使用的Rhino引擎(它只是模拟"浏览器的行为.最后一个事实被认为是一个缺点,因为呈现行为可以与流行的浏览器明显不同.

我们认为,PhantomJS是更强大的候选人.但是,我们并不是一无所知:)在做出决定时,我们还需要考虑其他因素和折衷吗? HtmlUnitDriver可以成为更好解决方案的其他方案?

解决方案

根据我在许多无头浏览器中的经验,我会说:

HtmlUnitDriver :我遇到的所有实现中最快的,非常适合简单的静态页面,尤其是那些没有JavaScript的页面.任何遥远复杂的页面似乎都会产生问题-即使我无法详细说明理由,这也是我的实践经验.非常适合在演示页,抓取状态页等上测试Selenium功能.

PhantomJSDriver (PhantomJS + GhostDriver):与台式机浏览器相比,速度没有您希望的要快,但是比Firefox + xvfb设置起来容易得多.默认情况下,屏幕截图看起来有些奇怪,但这通常是因为PhantomJS除非明确设置,否则默认情况下是狭窄的窗口(有关原因,请阅读下文).

更新:有关PhantomJS版本的更多详细信息,来自我的其他答案. /p>

与Safari一样,PhantomJS使用 WebKit 进行渲染(例如Firefox使用Gecko)

针对不同的WebKit版本构建了不同的PhantomJS版本. PhantomJS 2.x 使用 WebKit 538.x,使其成为等同于Safari 7或8 .而PhantomJS 1.9.8 使用 WebKit 534.34 ,相当于Safari 5.

这可能对您来说是个问题,因为Google确定Safari 5为旧"浏览器,因此可能以其他方式呈现其搜索页面.

因此,确保使用PhantomJS 2.x可以减少许多人报告的渲染差异与台式机浏览器的差异.

另一个有趣的可能性是 SlimerJS .但是,我还没有使其足够可靠地工作.

我从来没有遇到过HtmlUnitDriver或PhantomJSDriver的可靠性问题(唯一的烦恼是 HttpClient 4.5/HtmlUnit 2.17不兼容问题).

(回答有关修改HTTP请求的评论,我个人建议坚持使用WebDriver API,并使用 proxy ,例如 BrowserMob 来更改请求和响应,而不是利用浏览器特定的功能.)

总而言之,建议不要创建一种工具或过程,以强迫使用户选择一个浏览器而不是另一个浏览器.如果可能,请允许他们进行配置或覆盖.在大多数情况下,我都会喜欢PhantomJS,因为它不会让您失望.但是,对于最简单的页面,应该考虑HtmlUnit的性能.

另请参阅(也许): www.guru99. com/selenium-with-htmlunit-driver-phantomjs.html 和 www.quora/Software-Testing/How-does-PhantomJS-compare-to-Selenium

We are in the middle of choosing our headless browser driver solution that will be some implementation of Selenium WebDriver. There is the GhostDriver, which leverages the PhantomJS in the backend on the one side and HtmlUnitDriver which based on HtmlUnit on the other.

PhantomJS uses WebKit, the rendering engine of Safari, to render the pages while HtmlUnitDriver uses the Rhino engine which no other browsers use (it's just "simulating" browser behaviour. The last fact considered as a con, because the rendering behavior can differ significantly from the popular browsers.

In our opinion, PhantomJS is a much stronger candidate. But, we don't know everything :) Is there other considerations and trade-offs we should take into account with our decision? other scenarios where HtmlUnitDriver can be a better solution?

解决方案

From my experience with a number of headless browsers, I'd say:

HtmlUnitDriver: the fastest of all implementations I've come across, and perfect for simple, static pages, especially those without JavaScript. Any remotely complex page seems to produce problems - that's my practical experience even if I can't justify in detail. Perfect for testing Selenium features on demo pages, scraping status pages etc.

PhantomJSDriver (PhantomJS + GhostDriver): not as much faster as you might hope vs the desktop browsers, however, much easier to set up than Firefox + xvfb. By default screenshots can look a bit odd, but that usually turns out to be because PhantomJS defaults to a narrow window unless explicitly set (read below for why).

Update: a bit more detail on PhantomJS versions, from my other answer.

Like Safari, PhantomJS uses WebKit for rendering (e.g. Firefox uses Gecko)

Different PhantomJS versions are built against different WebKit versions. PhantomJS 2.x uses WebKit 538.x, which makes it equivalent to Safari 7 or 8. whereas PhantomJS 1.9.8 uses WebKit 534.34, which is equivalent to Safari 5.

This may be an issue for you, since Google determines Safari 5 to be an "old" browser and will therefore potentially render its search pages differently.

So ensuring you use PhantomJS 2.x can reduce the rendering differences that a lot of people report vs. desktop browsers.

Another interesting possibility is SlimerJS. However, I've not got it to work reliably enough yet.

I've never had reliability issues with either HtmlUnitDriver or PhantomJSDriver (the only annoyance one was a HttpClient 4.5 / HtmlUnit 2.17 incompatibility issue).

(In answer to the comment about modifying HTTP requests, I'd personally recommend sticking to the WebDriver API and use a proxy like BrowserMob to mutate requests and responses rather than taking advantage of browser-specific features.)

All in all, I'd advise against creating a tool or process that forces users to choose one browser over another. If possible, allow them to configure or override. For the majority of cases I'd plump for PhantomJS, as it won't let you down. However, the performance of HtmlUnit should be considered for the simplest pages.

See also (perhaps): www.guru99/selenium-with-htmlunit-driver-phantomjs.html and www.quora/Software-Testing/How-does-PhantomJS-compare-to-Selenium

更多推荐

HtmlUnitDriver(HtmlUnit)和GhostDriver(PhantomJS)?

本文发布于:2023-11-04 11:29:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HtmlUnit   HtmlUnitDriver   PhantomJS   GhostDriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!