`
SariyaLee
  • 浏览: 150402 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

三、HtmlUnitDriver

阅读更多
优点:HtmlUnitDriver不会实际打开浏览器,运行速度很快。对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnitDriver无疑是可以很好地解决这个问题。
缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。
1.使用HtmlUnitDriver访问百度,并返回页面标题信息
package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class BaiduHtmlUnitDriver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WebDriver driver=new HtmlUnitDriver();
		//打开百度首页
		driver.get("http://www.baidu.com/");
		//打印页面标题
		System.out.println("页面标题:"+driver.getTitle());
		//根据id获取页面元素输入框
		WebElement search=driver.findElement(By.id("kw"));
		//在id=“kw”的输入框输入“selenium”
		search.sendKeys("selenium");
		//根据id获取提交按钮
		WebElement submit=driver.findElement(By.id("su"));
		//点击按钮查询
		submit.click();
		//打印当前页面标题
	    System.out.println("页面标题:"+driver.getTitle());
	    //返回当前页面的url
	    System.out.println("页面url:"+driver.getCurrentUrl());
	    //返回当前的浏览器的窗口句柄
	    System.out.println("窗口句柄:"+driver.getWindowHandle());

	}

}


2.selenium官网wiki中关于HtmlUnitDriver的例子
package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}
分享到:
评论

相关推荐

    pythonselenium环境配置教程分享.pdf

    Python Selenium 环境配置教程分享 ...它还包括一个基于 HtmlUnit 的无界面实现,称为 HtmlUnitDriver。 通过本文档,用户可以轻松地配置 Python Selenium 环境,开始使用 Python 语言开发自动化测试。

    htmlunit-2.41.0-bin 官方包

    在实际项目中,开发者需要将这些文件引入到项目路径中,然后通过Java代码创建`HtmlUnitDriver`实例,设置网页编码,加载URL,执行网页操作,获取页面内容,最后进行断言或数据提取等操作。通过这种方式,HTMLUnit能...

    selenium2.0中文帮助文档.pdf

    #### 三、使用操作 **3.1 如何找到页面元素** - **定位方式**: - **By ID**: 通过ID属性定位。 - **By Name**: 通过name属性定位。 - **By XPATH**: 通过XPath表达式定位。 - **By ClassName**: 通过类名定位...

    pythonselenium环境配置教程归纳.pdf

    它还支持Android和iPhone的移动应用测试,包括HtmlUnitDriver无界面实现。 四、结论 本文档提供了详细的Python Selenium环境配置教程,涵盖了环境搭建、Windows环境安装、浏览器驱动安装等方面的内容,为读者提供...

    Selenium使用教程.pdf

    #### 三、使用Selenium进行页面操作 ##### 3.1 如何找到页面元素 找到页面元素是自动化测试的关键步骤之一。Selenium提供了多种定位方式: - **ByID**: 通过ID查找元素: ```java driver.findElement(By.id(...

Global site tag (gtag.js) - Google Analytics