Selenium2(webdriver)入门之TestNG的使用
一、在Eclipse中安装TestNG
1、打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK。
TestNG官方下载地址:http://testng.org/doc/download.html
2、然后选中TestNG,单击Next安装
3、安装好TestNG后重启eclipse查看是否安装好,Help-->About Eclipse-->Installation Details,如图:
二、使用TestNG来运行单个测试案例:
1、新建TestHelloWorldTestNG.java类,目录结构如下:
2、测试代码:

1 package com.selenium;
2
3 import org.openqa.selenium.By;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.WebElement;
6 import org.openqa.selenium.firefox.*;
7 import org.testng.annotations.*;
8 import org.testng.Assert;
9
10
11 public class TestHelloWorldTestNG {
12
13 WebDriver driver;
14 @Test
15 public void helloWorld() throws Exception {
16 //如果火狐浏览器没有默认安装在C盘,需要制定其路径
17 //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe");
18 driver = new FirefoxDriver();
19 driver.get("http://www.baidu.com/");
20
21 driver.manage().window().maximize();
22
23 WebElement txtbox = driver.findElement(By.name("wd"));
24 txtbox.sendKeys("Glen");
25
26 WebElement btn = driver.findElement(By.id("su"));
27 btn.click();
28
29 String expectedTitle = "Glen_百度搜索";
30 String actualTitle = driver.getTitle();
31
32 Assert.assertEquals(actualTitle,expectedTitle);
33 }
34
35 @AfterTest
36 public void tearDown(){
37 driver.quit();
38 }
39
40 }

3、然后右键Run As-->TestNG Test,运行结果如下:

[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-332204777\testng-customsuite.xml PASSED: helloWorld ===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@15d56d5: 34 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@19106c7: 11 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1632c2d: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@cdedfd: 11 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@13caecd: 22 ms

三、使用TestNG来运行多个测试案例:
1、增加一个失败的测试类TestHelloWorldTestNG_Fail.java:

1 package com.selenium;
2
3 import org.openqa.selenium.By;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.WebElement;
6 import org.openqa.selenium.firefox.*;
7 import org.testng.annotations.*;
8 import org.testng.Assert;
9
10
11 public class TestHelloWorldTestNG_Fail {
12
13 WebDriver driver;
14 @Test
15 public void helloWorld() throws Exception {
16 //如果火狐浏览器没有默认安装在C盘,需要制定其路径
17 //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe");
18 driver = new FirefoxDriver();
19 driver.get("http://www.baidu.com/");
20
21 driver.manage().window().maximize();
22
23 WebElement txtbox = driver.findElement(By.name("wd"));
24 txtbox.sendKeys("Glen");
25
26 WebElement btn = driver.findElement(By.id("su"));
27 btn.click();
28
29 String expectedTitle = "Glen_百度";
30 String actualTitle = driver.getTitle();
31
32 Assert.assertEquals(actualTitle,expectedTitle);
33 }
34
35 @AfterTest
36 public void tearDown(){
37 driver.quit();
38 }
39
40 }

2、在项目下新建一个Suite.xml文件:

<suite name="seleniumcn.cn.demo">
<test name="test_seleniumcn" >
<classes>
<class name="com.selenium.TestHelloWorldTestNG"/>
<class name="com.selenium.TestHelloWorldTestNG_Fail"/>
</classes>
</test>
</suite>

3、目录结构:
4、右键Suite.xml文件,Run As->TestNG Suite,如此就会运行suite.xml文件中所有的案例。

[TestNG] Running:
F:\workspace\WebDriverDemo\Suite.xml ===============================================
seleniumcn.cn.demo
Total tests run: 2, Failures: 1, Skips: 0
===============================================

5、右键WebDriverDemo刷新项目,目录中会新增加一个test.output文件夹,打开 index.html可以看一个简单的报告。
目录:
报告:
Selenium2(webdriver)入门之TestNG的使用的更多相关文章
- Selenium2(webdriver)入门之TestNG的安装与简单使用
上一篇已经搭建好了Eclipse+selenium2的环境,这一篇主要记录下TestNG的使用. 一.在Eclipse中安装TestNG 1.打开eclipse-->help-->Inst ...
- webdriver入门-Java
webdriver入门-Java 如何用webdriver打开一个浏览器,我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器,很多新的特性都会在fi ...
- selenium2 Webdriver + Java 自动化测试实战和完全教程
selenium2 Webdriver + Java 自动化测试实战和完全教程一.快速开始 博客分类: Selenium-webdriverselenium webdriver 学习selenium ...
- selenium2 WebDriver 在asp.net项目中的应用
selenium2 WebDriver是一款跨平台的 自动化测试工具,它可以操纵浏览器,模拟用户行为,非常方便用户进行自动化测试. .net项目使用它,首先要通过 Visual Studio 的 nu ...
- Parallel WebDriver executions using TestNG
In this post, we will see how does one make use of TestNG to kick off parallel UI tests using WebDri ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- python selenium webdriver入门基本操作
python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...
- Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)
本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...
- Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件
本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...
随机推荐
- 创建实体数据模型需要注意的,不要选单复数形式,否则AddObject出问题
//这个测试太不容易了,总是出错,addInfo 方法进去,最后调用context对象.AddObject(),也就是context.AddObject(entitySetName, entity); ...
- ORACLE SQL单行函数(三)【weber出品必属精品】
16.L:代表本地货币符,这个和区域有关.这个时候我们想来显示一下人民币的符号:¥ $ vi .bash_profile ---写入如下内容: export NLS_LANG='SIMPLIFIED ...
- JQuery 中的Ajax
JQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...
- vs2008下使用libcurl
网上找了半天,总算找到一个比较好用的C++ 网络库,老实说,完全用Socket操作网络对于需要开发网络应用程序的人员来说还是很蛋疼很繁琐的.好在有这么一个给力的库.这个库的介绍可以自己百度一下,就我所 ...
- 多线程08-Callable和Future
1.简介 Callable是一个接口,与Runnable类似,包含一个必须实现的call方法,可以启动为让另一个线程来执行,执行Callable可以得到一个Future对象 该对象可以监听Callab ...
- EIGRP认证 配置 (仅仅是命令 原理自己去看书) 转自:http://blog.163.com/s_u/blog/static/13308367201111771831631/
EIGRP认证 目的:掌握EIGRP的MD5认证 拓扑:这里IP配置我就不写出来了,应该对大家来说是非常简单的事了,就要细心一点就可以了.首先我们在R1上启用MD5认证R1(config)#key c ...
- MySQL重构查询的方式
在优化有问题的查询时,目标应该是找到一个更优的方法获得实际需要的结果--而不一定总要从MySQL获取一模一样的结果集.有时候可以查询转换一种写法让其返回一样的结果,但是性能更好.但也可以通过修改应用代 ...
- VS2010 IE10 调试时报“未能将脚本调试器附加到计算机”,已经附加了一个进程
解决办法:以管理员身份打开CMD,运行:regsvr32.exe "%ProgramFiles(x86)%\Common Files\Microsoft Shared\VS7Debug\ms ...
- 3.3.2 嵌入汇编(摘自<linux内核完全剖析>)
内核C语言程序嵌入式汇编代码又叫内联汇编,具有输入和输出参数的嵌入汇编语句的基本格式为: ************************************************** asm( ...
- Zend Studio 8.0.1 新建远程项目无法展示远程项目列表的问题
PHP的开发工具还是不少的,有用VI,有用eclipse.Netbean.sublime Text,当然用的比较多的还是Zend Studio,这次试用Zend Studio 8.0.1 开发几个PH ...