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 ...
随机推荐
- hdu 1205
#include <stdio.h> int a[1005000]; int main() { int t; scanf("%d",&t); while(t-- ...
- asp.net微信开发第十篇----使用百度编辑器编辑图文消息,上传图片、微信视频
经过几天的资料收集,终于完成了该编辑器的图片上传,视频插入功能,视频插入功能主要借用了该编辑器的插入iframe功能,如原始插件图: 修改后的插件图如下(其中我隐藏掉了一些不需要使用的插件功能): 配 ...
- Ubuntu 11.10开启root用户登陆
以管理员身份运行 #sudo gedit /etc/lightdm/lightdm.conf 将里面改成 "autologin-user=root" 就可以以root用户登录了
- Oracle中包的创建
包是过程和函数的集合体,包包括创建包和创建包体,创建包的时候在可以定义过程和函数,包体中则具体实现过程和函数. eg: --创建包 create or replace package mypac1 ...
- 你好,C++(29)脚踏两只船的函数不是好函数 5.4 函数设计的基本规则
5.4 函数设计的基本规则 函数是C++程序的基本功能单元,就像一块块砖头可以有规则地垒成一座房子,而一个个函数也可以有规则地组织成一个程序.我们在大量使用他人设计好的函数的同时,也在设计大量的函数 ...
- 趣味PAT--循环-19. 币值转换(20)
One visible minute on the stage is attributed to ten years of invisible practice off the stage. &quo ...
- Oracle数据库之创建表空间与用户
Oracle数据库之创建表空间与用户 一.创建表空间 基本语法表述: CREATE TABLESPACE tablespace_name [DATAFILE datafile_spec1 [,data ...
- Oracle 序列(sequence)
序列是Oracle特有的,他可以维护一个自增的数字序列,通常从1开始增长,但可以设置. (1)创建序列: increment (2)使用序列: insert into student(sno,name ...
- DACL, NULL or not NULL
上周 hBifTs在折腾他的文件映射封装类的时候,碰到了不能在 ASP.NET 中直接打开由桌面程序创建的内核对象的问题. 内存映射文件与用户权限 他当时是的方法是修改 ASP.NET 配置文件,让 ...
- php 读取 word
---恢复内容开始--- 首先安装com扩展: php.ini php.ini 确保有此语句 [PHP_COM_DOTNET] extension=php_com_dotnet.dll php.i ...