上一篇已经搭建好了Eclipse+selenium2的环境,这一篇主要记录下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、测试代码: 

 package com.selenium;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
import org.testng.annotations.*;
import org.testng.Assert; public class TestHelloWorldTestNG { WebDriver driver;
@Test
public void helloWorld() throws Exception {
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
//System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe");
driver = new FirefoxDriver();
driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("Glen"); WebElement btn = driver.findElement(By.id("su"));
btn.click(); String expectedTitle = "Glen_百度搜索";
String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle,expectedTitle);
} @AfterTest
public void tearDown(){
driver.quit();
} }

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:

 package com.selenium;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
import org.testng.annotations.*;
import org.testng.Assert; public class TestHelloWorldTestNG_Fail { WebDriver driver;
@Test
public void helloWorld() throws Exception {
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
//System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe");
driver = new FirefoxDriver();
driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("Glen"); WebElement btn = driver.findElement(By.id("su"));
btn.click(); String expectedTitle = "Glen_百度";
String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle,expectedTitle);
} @AfterTest
public void tearDown(){
driver.quit();
} }

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的安装与简单使用的更多相关文章

  1. Selenium2(webdriver)入门之TestNG的使用

    一.在Eclipse中安装TestNG 1.打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK. ...

  2. CentOS7 下Docker最新入门教程 超级详细 (安装以及简单的使用)

    转载https://blog.csdn.net/wzsy_ll/article/details/82866627 1.为什么使用Docker(本人) 最近总是频繁的在新服务器发布项目, 每次发布都需要 ...

  3. webdriver入门-Java

    webdriver入门-Java   如何用webdriver打开一个浏览器,我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器,很多新的特性都会在fi ...

  4. selenium2 Webdriver + Java 自动化测试实战和完全教程

    selenium2 Webdriver + Java 自动化测试实战和完全教程一.快速开始 博客分类: Selenium-webdriverselenium webdriver 学习selenium ...

  5. selenium2 WebDriver 在asp.net项目中的应用

    selenium2 WebDriver是一款跨平台的 自动化测试工具,它可以操纵浏览器,模拟用户行为,非常方便用户进行自动化测试. .net项目使用它,首先要通过 Visual Studio 的 nu ...

  6. Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件

    本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...

  7. Maven 入门 (1)—— 安装

    Maven 入门 (1)—— 安装 http://blog.csdn.net/kakashi8841/article/details/17371837 1.下载maven安装包 http://mave ...

  8. 【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0 +VS 2013 开发环境配置

    图片太多,具体过程参照: [OpenCV入门教程之一] 安装OpenCV:OpenCV 3.0.OpenCV 2.4.8.OpenCV 2.4.9 +VS 开发环境配置 说下我这边的设置: 选择deb ...

  9. 单元测试框架-TestNG的安装

    一.在eclipse中安装TestNG插件 1)打开eclipse,选择help--> Install New Software

随机推荐

  1. python测试开发django-36.一对一(OneToOneField)关系查询

    前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...

  2. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Java获取当前时间30天之前的时间

    //方法一 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String maxDateStr = " ...

  4. const 变量修饰 研究

    #include<stdio.h> #include<iostream> using namespace std; struct A { ;} ;} }; int main() ...

  5. Useful JVM Flags – Part 8 (GC Logging)

    The last part of this series is about garbage collection logging and associated flags. The GC log is ...

  6. verilog语法实例学习(10)

    常用的时序电路介绍 T触发器和JK触发器 在D触发器输入端添加一些简单的逻辑电路,可以生成另一种类型的存储元件.比如下图所示的T触发器.该电路有一个上升沿触发的触发器和两个与门,一个或门,以及一个反相 ...

  7. 奇怪吸引子---DequanLi

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  8. go语言之进阶篇方法值

    1.方法值 示例: package main import "fmt" type Person struct { name string //名字 sex byte //性别, 字 ...

  9. libcurl库的http get和http post使用【转】

    一.libcurl中的http get使用方法 1. 为什么要使用libcurl 1) 作为http的客户端,可以直接用socket连接服务器,然后对到的数据进行http解析,但要分析协议头,实现代理 ...

  10. 读书笔记,《深入理解java虚拟机》,第二章,java内存区域与内存溢出异常

    第二节,运行时数据区域.    在这个章节中,作者给出了一个java虚拟机运行时数据区的框图,图的左侧是方法区和堆,这两个数据区是所有的线程所共享的.然后是虚拟机栈.本地方法栈.还有程序计数器,这三个 ...