Webdriver (Selenium2)是一种用于Web应用程序的自动测试工具,它提供了一套友好的API,与Selenium 1(Selenium-RC)相比,Selenium 2的API更容易理解和使用,其可读性和可维护性也大大提高。Webdriver完全就是一套类库,不依赖于任何测试框架,除了必要的浏览器驱动,不需要启动其他进程或安装其他程序,也不必像Selenium 1那样需要先启动服务。
另外,二者所采用的技术方案也不同。Selenium 1是在浏览器中运行 JavaScript来进行测试,而Selenium 2则是通过原生浏览器支持或者浏览器扩展直接控制浏览器。
Selenium 2针对各个浏览器而开发的,它取代了嵌入到被测Web应用中的 JavaScript。与浏览器的紧密集成,支持创建更高级的测试,避免了JavaScript安全模型的限制。除了来自浏览器厂商的支持,Selenium 2 还利用操作系统级的调用模拟用户输入。WebDriver 支持
Firefox (FirefoxDriver)
IE(InternetExplorerDriver)
Opera(OperaDriver)
Chrome (ChromeDriver)
以及safari(SafariDriver)
它还支持Android(Selendroid)和iPhone(Appium)的移动应用测试。此外,Selenium 2还包括基于HtmlUnit的无界面实现,称为HtmlUnitDriver,和基于webkit的无界面浏览器phantomjs。Selenium 2 API可以通过
Java
C#
PHP
Python
Perl
Ruby
等编程语言访问,支持开发人员使用他们常用的编程语言来创建测试。
Commmand | 描述 |
---|---|
driver.get("URL") | 导航到应用程序 |
element.sendKeys("inputtext") | 输入一些文本输入框 |
element.clear() | 从输入框清空内容 |
select.deselectAll() | 这将取消选择页面上的第一个选择所有选项: |
select.selectByVisibleText("some text") | select the OPTION with the input specified by the user. |
driver.switchTo().window("windowName") | Moving the focus from one window to another |
driver.switchTo().frame("frameName") | swing from frame to frame |
driver.switchTo().alert() | Helps in handling alerts |
driver.navigate().to("URL") | Navigate to the URL |
driver.navigate().forward() | To Navigate forward |
driver.navigate().back() | To Navigate back |
driver.close() | Closes the current Browser associated with the driver |
driver.quit() | Quits the driver and closes all the associated window of that driver. |
driver.refresh() | Refreshes the current page. |