Selenium2(webdriver)入门之TestNG的安装与简单使用
上一篇已经搭建好了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的安装与简单使用的更多相关文章
- Selenium2(webdriver)入门之TestNG的使用
一.在Eclipse中安装TestNG 1.打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK. ...
- CentOS7 下Docker最新入门教程 超级详细 (安装以及简单的使用)
转载https://blog.csdn.net/wzsy_ll/article/details/82866627 1.为什么使用Docker(本人) 最近总是频繁的在新服务器发布项目, 每次发布都需要 ...
- 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 ...
- Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件
本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...
- Maven 入门 (1)—— 安装
Maven 入门 (1)—— 安装 http://blog.csdn.net/kakashi8841/article/details/17371837 1.下载maven安装包 http://mave ...
- 【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0 +VS 2013 开发环境配置
图片太多,具体过程参照: [OpenCV入门教程之一] 安装OpenCV:OpenCV 3.0.OpenCV 2.4.8.OpenCV 2.4.9 +VS 开发环境配置 说下我这边的设置: 选择deb ...
- 单元测试框架-TestNG的安装
一.在eclipse中安装TestNG插件 1)打开eclipse,选择help--> Install New Software
随机推荐
- AutoMapper在MVC中的运用02-Decimal转String、集合、子父类映射
本篇AutoMapper使用场景: ※ Decimal转换成String类型 ※ 源数组转换成目标数组 ※ 源中的集合(数组)属性转换成目标中的集合(数组)属性 ※ 子类父类间的映射 Decimal转 ...
- iPhone开发过程中调试多次Release问题 message sent to deallocated
初级:第一步 为程序添加符号断点 malloc_error_break 方法如下. 目标效果:让程序崩溃时跳转到出错到那一行.但是往往达不到这个效果.不行就继续往下看. At times, wh ...
- android的logcat详细用法!
from://http://www.miui.com/article-272-1.html [技术交流]android的logcat详细用法! logcat是Android中一个命令行工具,可以用于得 ...
- windows下PHP不能开启pgsql扩展的解决方法
Tip: 环境 windows8.1 64位 + xampp1.8.1 + postgresql 9.3.6-2 第一步: php.ini中开启pgsql扩展 extension=php_pgsql ...
- 利用ViewStub来延迟加载视图
很多情况下,我们的视图可能会随着用户的操作的不同而变化,比如一个新的页面包含多个控件,但仅仅在用户点击这个按钮后,所有的控件才能完全显示.也就是说一上来可能就显示一个控件,点击按钮后把其他隐藏的控件再 ...
- Easyui layout设置满屏效果
html文件: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- 简单实用UML关系图解
一句话UML,再记不住就要DPP了: 关系 图解 代码 备注 1:继承关系(Generalization) 2:实现关系(Realization) 3:依赖关系(Dependency) ...
- SKU与SPU
首先,搞清楚商品与单品的区别.例如,iphone是一个单品,但是在淘宝上当很多商家同时出售这个产品的时候,iphone就是一个商品了. 商品:淘宝叫item,京东叫product,商品特指与商家有关的 ...
- [转]php curl经典最常用的5个例子
转自: http://www.jb100.net/html/content-22-821-1.html php curl常用的5个例子 我用php ,curl主要是抓取数据,当然我们可以用其他的方法来 ...
- Fedora 18/19没有注销
sudo yum install dconf-editor -y 定位到org > gnome > shell 勾选always-show-log-out http://www.ryan ...