一、在Eclipse中安装TestNG

1、打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK。

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.chrome.*;

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", "C:/Program Files(x86)/Mozilla Firefox/firefox.exe");

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

driver = new ChromeDriver();

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 = "百度一下,你就知道";

String actualTitle = driver.getTitle();

Assert.assertEquals(actualTitle,expectedTitle);

}

@AfterTest

public void tearDown(){

driver.quit();

}

}

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

 

三、使用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.chrome.*;

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");

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

driver = new ChromeDriver();

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 = "百度一下";

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文件中所有的案例。

  

5、右键WebDriverDemo刷新项目,目录中会新增加一个test.output文件夹,打开 index.html可以看一个简单的报告。

目录:

报告:

Selenium之TestNG安装的更多相关文章

  1. Selenium+Java+TestNG环境配置

    1. JDK 2.eclipse+TestNG >TestNG安装.   Name:testng  Location:http://beust.com/eclipse.如图: 3.seleniu ...

  2. IDEA+Java:Selenium+Maven+TestNG基本WebUI自动化测试环境搭建

    IDEA+java:Selenium+Maven+TestNG 本文介绍的测试环境,应该是最基本的测试环境了,也是很多文章都有写,这里做一个完整的图文配置整理,方便阅读理解! 使用maven的好处,由 ...

  3. selenium webdriver testng自动化测试数据驱动

    selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...

  4. 自动化测试框架selenium+java+TestNG——配置篇

    最近来总结下自动化测试 selenium的一些常用框架测试搭配,由简入繁,最简单的就是selenium+java+TestNG了,因为我用的是java,就只是总结下java了. TestNG在线安装: ...

  5. Selenium IDE和Selenium RC的安装

    1       安装FireBug和FirePath 1.在火狐浏览器中,点击”添加附件”按钮,弹出”附加组件管理器”页面 2.在弹出页面中,输入“fireBug”,点击“搜索”按钮,弹出fireBu ...

  6. 【转】selenium简介及安装方法

    转自:http://www.cnblogs.com/fnng/p/3157639.html 1. selenium 介绍 selenium 是一个web 的自动化测试工具,不少学习功能自动化的同学开始 ...

  7. python selenium 自动化测试环境安装

    注意:2.7和3.0版本的语法有些不一样 安装自动化测试软件 selenium(地址http://www.seleniumhq.org/download/) 安装步骤: 1.安装pythone运行环境 ...

  8. maven+selenium+java+testng+jenkins自动化测试

    最近在公司搭建了一套基于maven+selenium+java+testng+jenkins的自动化测试框架,免得以后重写记录下 工程目录 pom.xml <project xmlns=&quo ...

  9. 【selenium】- selenium IDE的安装以及使用

    本文由小编根据慕课网视频亲自整理,转载请注明出处和作者. 1. 自动化测试工程师的任务 一个合格的自动化测试工程师,需要把框架搭建起来.让不是自动化测试的人,一个普通功能化测试的人,可以完成自动化测试 ...

随机推荐

  1. Winform控件的问题汇总

    2014-01-19号 用户控件中的子控件(Btn控件),想要暴露到用户控件之外,以供其它其他控件使用的解决方法 1.在用户控件中定义一个委托和这个委托的事件. public delegate voi ...

  2. linux 下 vi 编辑器 使用

    命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 1.进入插入模式 按「i」切换进入插入模式「insert mode」,按“i”进入插 ...

  3. html转图片,java库cssbox

    引入依赖包 <dependency> <groupId>net.sf.cssbox</groupId> <artifactId>cssbox</a ...

  4. Cobbler无人值守安装linux系统

    简介 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等. Cobbler可以使用命令行方式管理,也提 ...

  5. 剑指offer(31-35)编程题

    整数中1出现的次数(从1到n整数中1出现的次数) 把数组排成最小的数 丑数 第一个只出现一次的字符位置 数组中的逆序 31.求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数 ...

  6. Maven 打包的时候报 Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin

    错误信息: [ERROR] Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin:1.0-alpha-1:native2 ...

  7. CentOS 7_64位系统下搭建Hadoop_2.8.0分布式环境

    准备条件: CentOS 7 64位操作系统 | 选择minimal版本即可(不带可视化桌面环境),也可以选择带完整版Hadoop-2.8.0 | 本文采用的是Hadoop-2.8.0版本.JDK1. ...

  8. Windows下安装PHP开发环境

    一.Apache 因为Apache官网只提供源代码,如果要使用必须得自己编译,这里我选择第三方安装包Apache Lounge. 进入Apachelounge官方下载地址:http://www.apa ...

  9. Android studio的调试方法

    1. DDMS DDMS 全称 Dalvik Debug Monitor Service, dalvik虚拟机调试监控服务. 可以进行的操作有:为测试设备截屏,查看特定行程中正在运行的线程以及堆信息. ...

  10. DateReader读取数据

    DateReader对象提供了用循序的.只读的方式读取Command对象获取的数据结果集正是因为DateReader是以循序的方法连续地读取数据,所以DateReader会以独占的方式打开数据库连接. ...