一、在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. 问题 I: 闪闪发光

    [提交] [状态] [命题人:外部导入] 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计——成为偶像, 只要她 ...

  2. Html5与本地App资料整理分析

    最近开发了一个小的手机网站,重新了解了本地应用,html5,混合应用,webApp等概念,整理下相关的资料,略带自己的思考 用户固有的使用习惯 在<2012年度 HTML5状况及发展形势报告.p ...

  3. javascript中prototype与__proto__

    1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...

  4. java中为什么重写equals时必须重写hashCode方法?

    在上一篇博文Java中equals和==的区别中介绍了Object类的equals方法,并且也介绍了我们可在重写equals方法,本章我们来说一下为什么重写equals方法的时候也要重写hashCod ...

  5. HDU 6225 Little Boxes

    Little Boxes Little boxes on the hillside.  Little boxes made of ticky-tacky.  Little boxes.  Little ...

  6. MYSQL5.7 sql_mode=only_full_group_by

    错误提示: .... which is not functionally dependent on columns in GROUP BY clause; this is incompatible w ...

  7. 图解ARP协议(五)免费ARP:地址冲突了肿么办?

    一.免费ARP概述 网络世界纷繁复杂,除了各种黑客攻击行为对网络能造成实际破坏之外,还有一类安全问题或泛安全问题,看上去问题不大,但其实仍然可以造成极大的杀伤力.今天跟大家探讨的,也是技术原理比较简单 ...

  8. js 继承介绍

    js中继承的方式并不是明确的,这里介绍常用的几种 一.对象冒充(构造函数绑定) 原理:使用对象冒充继承基类,实质上是使用call或apply方法改变this 指针的指向 function Monkey ...

  9. 理解Flexbox:你需要知道的一切

    这篇文章介绍了Flexbox模块所有基本概念,而且是介绍Flexbox模块的很好的一篇文章,所以这篇文章非常的长,你要有所准备. 学习Flexbox的曲线 @Philip Roberts在Twitte ...

  10. Jquery 搜索框自动提示

    为文本框增加自动提示下拉功能,比如输入 1,则从后台数据库查询出包含1 的字段,在文本框增加下拉列表供用户选择 ajax 返回数据为搜索查询字段的json集合 <script src=" ...