一、在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的使用的更多相关文章

  1. Selenium2(webdriver)入门之TestNG的安装与简单使用

    上一篇已经搭建好了Eclipse+selenium2的环境,这一篇主要记录下TestNG的使用. 一.在Eclipse中安装TestNG 1.打开eclipse-->help-->Inst ...

  2. webdriver入门-Java

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

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

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

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

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

  5. 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 ...

  6. selenium2(WebDriver) API

    selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/  1.1  下载selenium2.0的包 官方downl ...

  7. python selenium webdriver入门基本操作

    python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...

  8. Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)

    本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...

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

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

随机推荐

  1. Android学习手记(4) BroadcastReceiver监听电池信息

    Android 中,Broadcast是一种在应用程序之间进行传输信息的机制.BroadcastReceiver对发送过来的Broadcast进行过滤和响应.根据这种机制,我们可以获取电池现有电量等信 ...

  2. ssh key报but this does not map back to the address – POSSIBLE BREAK-IN ATTEMPT!错误

    在/etc/hosts 文件加上对方的主机名  ip地址,可以ping通主机名即可.

  3. mysql复制表

    create table 表名 like 要复制的表名  //复制表结构 insert into 表名 select * from 要复制的表名  //复制数据 方法2,一般不推荐,如果后台加数据,可 ...

  4. 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了

    控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?

  5. jquery的节点查询

    jQuery.parent(expr)           //找父元素 jQuery.parents(expr)          //找到所有祖先元素,不限于父元素 jQuery.children ...

  6. iphone微信长按二维码识别不了

    在安卓版的微信长按二维码可以识别(前提是你的微信版本到支持此功能),但是到了苹果版的微信就识别不了,经个人测试发现是缩放的问题: 1.设置了初始缩放设置为1,最大缩放值要>=1,不支持缩放.-- ...

  7. ServerInfo.INI解密

    [GlobalInfo]LastServerName=000781ED2D127FBA074D97444DC82F216443034E66BB341A428B14E326A656B9LastServe ...

  8. Git 初始化配置

    先给大家推荐个很不错的GIT学习资料:廖雪峰  <Git简介> http://www.liaoxuefeng.com/wiki/0013739516305929606dd183612485 ...

  9. 【持久化框架】Mybatis与Hibernate的详细对比

        前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Myba ...

  10. windows 下安装使用ipython

    转自:https://my.oschina.net/u/1431433/blog/189337 1. 下载安装Python 下载: python-3.3.3.amd64.msi (救在Python.o ...