硒与 HtmlUnit? [英] Selenium vs HtmlUnit?

查看:17
本文介绍了硒与 HtmlUnit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更好地了解测试框架并一直在研究 Selenium.我以前用过

了解更多详情

关于selenium webDriver的一些话:

Selenium 2.0 的主要新特性是集成了 WebDriver API.WebDriver 旨在提供更简单、更简洁的编程接口,同时解决 Selenium-RC API 中的一些限制.Selenium-WebDriver 是为了更好地支持动态网页而开发的,其中页面元素可能会更改而无需重新加载页面本身.WebDriver 的目标是提供一个设计良好的面向对象的 API,为现代高级 Web 应用测试问题提供更好的支持.

与 Selenium-RC 相比,WebDriver 如何驱动"浏览器?

Selenium-WebDriver 使用每个浏览器对自动化的原生支持直接调用浏览器.这些直接调用的方式以及它们支持的功能取决于您使用的浏览器.本章稍后将提供有关每个浏览器驱动程序"的信息.对于熟悉 Selenium-RC 的人来说,这与您习惯的完全不同.Selenium-RC 对于每个支持的浏览器都以相同的方式工作.它在浏览器加载时将 javascript 函数注入"到浏览器中,然后使用其 javascript 在浏览器中驱动 AUT.WebDriver 不使用这种技术.同样,它使用浏览器内置的自动化支持直接驱动浏览器.

WebDriver 和 Selenium-Server

您可能需要也可能不需要 Selenium 服务器,这取决于您打算如何使用 Selenium-WebDriver.如果您只使用 WebDriver API,则不需要 Selenium-Server.如果您的浏览器和测试都将在同一台机器上运行,并且您的测试只使用 WebDriver API,那么您不需要运行 Selenium-Server;WebDriver 将直接运行浏览器.使用 Selenium-Server 和 Selenium-WebDriver 有一些原因.

  • 您正在使用 Selenium-Grid 将测试分布到多个机器或虚拟机 (VM).
  • 您想连接到具有特定浏览器的远程计算机不在您当前机器上的版本.
  • 您没有使用 Java 绑定(即 Python、C# 或 Ruby)并且想使用 HtmlUnit 驱动程序

Selenium-WebDriver 的驱动程序WebDriver 是针对其编写测试的关键接口的名称,但有几种实现.其中包括:

HtmlUnit 驱动程序这是目前最快、最轻量级的 WebDriver 实现.顾名思义,这是基于 HtmlUnit.HtmlUnit 是一个基于 Java 的 WebBrowser 实现,没有 GUI.对于任何语言绑定(Java 除外),Selenium Server 都需要使用此驱动程序.

优点

  • 最快的 WebDriver 实现
  • 纯 Java 解决方案,因此与平台无关.
  • 支持 JavaScript

缺点

  • 模拟其他浏览器的 JavaScript 行为(见下文)

HtmlUnit 驱动程序中的 JavaScript没有一个流行的浏览器使用 HtmlUnit (Rhino) 使用的 JavaScript 引擎.如果您使用 HtmlUnit 测试 JavaScript,结果可能与那些浏览器有很大不同.当我们说JavaScript"时,我们实际上是指JavaScript 和 DOM".尽管 DOM 是由 W3C 定义的,但每个浏览器在 DOM 的实现以及 JavaScript 与它的交互方式方面都有自己的怪癖和差异.HtmlUnit 有一个令人印象深刻的完整的 DOM 实现,并且对使用 JavaScript 有很好的支持,但它与任何其他浏览器没有什么不同:它与 W3C 标准和主要浏览器的 DOM 实现都有自己的怪癖和差异,尽管它模仿其他浏览器的能力.使用 WebDriver,我们必须做出选择;我们是否启用了 HtmlUnit 的 JavaScript 功能并冒着团队遇到问题的风险,这些问题只在那里表现出来,或者我们是否知道有越来越多的网站依赖于 JavaScript,而禁用 JavaScript?我们采取了保守的方法,在使用 HtmlUnit 时默认禁用支持.对于 WebDriver 和 HtmlUnit 的每个版本,我们都会重新评估这个决定:我们希望在某个时候在 HtmlUnit 上默认启用 JavaScript.

要深入研究 webDriver 的设置,请参阅 this

来自 HtmlUnit 文档:HtmlUnit 不是一个通用的单元测试框架.它专门是一种模拟浏览器以进行测试的方法,旨在用于其他测试框架,例如 JUnit 或 TestNG.

所以总结一下 Selenium 和 HtmlUnit 的区别:HtmlUnit 是一个基于 java 的 WebBrowser 实现,没有 GUI一种模拟浏览器用于测试目的的方法Selenium-WebDriver 使用直接调用浏览器每个浏览器对自动化的原生支持. 我们可以看到 HtmlUnit 提供了 API,而没有 GUI 自动化的可能性,而 WebDriver 提供了内部浏览器的自动化可能性.

谈到移动自动化,Selenium 也有一个 iPhone 驱动程序iPhone 驱动程序维基文章和安卓驱动Android 驱动程序维基文章

另请参阅 此演示文稿

不幸的是,由于我处理网络自动化(非移动),因此我无法向您提供我对移动驱动程序的工作经验评估.也知道 Cucumber(自动化工具)在移动自动化中很流行.请参阅 this这个.

希望你现在更清楚一点 =)

I am trying to understand testing framework better and been looking into Selenium. I've used HTMLUnit before, mainly when I needed to scrape some information off website or the likes.

In the context of writing test automation, what's the advantage / disadvantages of Selenium vs HTMLUnit? Looks to me Selenium is more complicated to set up than HTMLUnit, although at the same time there's a HTMLUnitDriver for Selenium which I think behave the exact same way as in HTMLUnit itself?

Selenium obviously provides more robust framework, it has the Selenium RC for pararel testing, it also has different browser drivers that can be used - although when you used the browser drivers, the test will actually open/close a browser application rather than headless.

May be I am not understanding Selenium correctly. Some directions and pointers would be great!

On another note - a separate question - I am also looking at doing automated testing on mobile browser, I see that Selenium has an IPhoneDriver for it, but then this is not a headless testing either as it requires actual iOS simulator.

Is there anyway to do headless testing on mobile sites? Would changing user-agent be sufficient? I've seen a couple posts around changing user-agent that seem to have their own challenges, eg. Set user-agent in Selenium RC

Thanks a lot!

解决方案

well, would try to explain differences in detail.

Speaking about parallel testing, it better to use selenium grid. Basic concept of selenium RC and selenium grid. You can get into more details here

Some words about selenium webDriver:

The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to providing an simpler, more concise programming interface along with addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC?

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter. For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

WebDriver and the Selenium-Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly. There are some reasons though to use the Selenium-Server with Selenium-WebDriver.

  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

Selenium-WebDriver’s Drivers WebDriver is the name of the key interface against which tests should be written, but there are several implementations. These include:

HtmlUnit Driver This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation of a WebBrowser without a GUI. For any language binding (other than java) the Selenium Server is required to use this driver.

Pros

  • Fastest implementation of WebDriver
  • A pure Java solution and so it is platform independent.
  • Supports JavaScript

Cons

  • Emulates other browsers’ JavaScript behaviour (see below)

JavaScript in the HtmlUnit Driver None of the popular browsers uses the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers. When we say "JavaScript" we actually mean "JavaScript and the DOM". Although the DOM is defined by the W3C each browser has its own quirks and differences in their implementation of the DOM and in how JavaScript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using JavaScript, but it is no different from any other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers. With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave JavaScript disabled, knowing that there are more and more sites that rely on JavaScript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable JavaScript by default on the HtmlUnit at some point.

To investigate deeper into webDriver's setUp see this

From HtmlUnit documentation: HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit or TestNG.

So to conclude Selenium and HtmlUnit difference: HtmlUnit is a java based implementation of a WebBrowser without a GUI and a way to simulate a browser for testing purposes and Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. we can see that HtmlUnit provides API without GUI possibility for automation whereas WebDriver provides internal browsers' possibilities for automation.

Speaking about mobile automation, Selenium also has an iPhone Driver iPhone Driver wiki article and Android Driver Android Driver wiki article

See also this presentation

Unfortunately I can not give you my working experience evaluation of mobile drivers as I deal with web automation (no mobile). Also know that Cucumber (automation tool) is popular among mobile automators. see this and this.

Hope it come a lil bit more clear for you now =)

这篇关于硒与 HtmlUnit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆