selenium使用更快的Driver--HtmlUnit Driver
2016-12-11 22:39
316 查看
最近使用selenium时,发现driver打开浏览器挺慢的,先后使用过firefox,chrome都感觉很慢,比如淘宝首页,手工打开浏览器访问,全部渲染完成,2-3s就完成了,而使用driver来打开,一般都得要5-10s的样子,效率确实挺低的。现在可以使用HtmlUnit Driver了,它基于htmlunit,用java来模拟一个浏览器,速度很快。
官方说明如下:
A pure Java solution and so it is platform independent. //纯java实现,所以是跨平台的
Supports JavaScript //支持js
使用HtmlUnit Driver时,需要使用selenium-server,
启动selenium-server,然后:
java:WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.htmlUnitWithJs());
python:driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS)
http://docs.seleniumhq.org/docs/03_webdriver.jsp#htmlunit-driver
官方说明如下:
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.这个driver是当前最快的driver实现Usage
WebDriver driver = new HtmlUnitDriver(); //java的使用方式
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNIT) //python的使用方式
Pros
Fastest implementation of WebDriver //最快的A pure Java solution and so it is platform independent. //纯java实现,所以是跨平台的
Supports JavaScript //支持js
Cons
Emulates other browsers’ JavaScript behaviour (see below)JavaScript in the HtmlUnit Driver //如何开启javascript的支持
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.Enabling JavaScript
If you can’t wait, enabling JavaScript support is very easy:HtmlUnitDriver driver = new HtmlUnitDriver(true); //java的调用方式
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS) //python的调用方式This will cause the HtmlUnit Driver to emulate Firefox 3.6’s JavaScript handling by default.//上述代码会使得 HtmlUnit Driver 模拟 Firefox3.6 对 JavaScript 的处理。
使用HtmlUnit Driver时,需要使用selenium-server,
启动selenium-server,然后:
java:WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.htmlUnitWithJs());
python:driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS)
http://docs.seleniumhq.org/docs/03_webdriver.jsp#htmlunit-driver
相关文章推荐
- Selenium AndroidDriver使用(一)
- Selenium WebDriver使用经验杂记
- selenium web driver 使用JS修改input属性
- Selenium WebDriver使用经验杂记
- selenium 使用WebDriverWait等待条件正确
- Selenium-webdriver—使用Page Object设计模式
- Selenium WebDriver 使用IE
- selenium-webdriver用例批量运行和测试套件使用 ------之我见
- Selenium2(webdriver)入门之TestNG的使用
- Selenium 2.0 WebDriver使用指南
- Selenium ide及webDriver使用
- 在Selenium WebDriver中使用By.Xpath快速定位页面元素
- selenium 使用close和quit关闭driver的不同点
- Selenium 2.0 WebDriver 使用指南
- 在Selenium WebDriver中使用By.Xpath快速定位页面元素
- WebDriver拾级而上·之十一 在selenium2.0中使用selenium1.0的API
- 使用Selenium-webdriver对页面进行截图操作
- Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考
- Selenium web driver 使用JS修改input属性
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用