Selenium2(webdriver)入门之TestNG的使用
2014-02-10 13:39
363 查看
一、在Eclipse中安装TestNG
1、打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK。
TestNG官方下载地址:http://testng.org/doc/download.html
![](http://images.cnitblog.com/blog/77835/201312/20091025-99bbcd0524fa48059bb5af0e24b1e88d.jpg)
2、然后选中TestNG,单击Next安装
![](http://images.cnitblog.com/blog/77835/201312/20091217-eb8dff27c5cb40bebbededef63342115.jpg)
3、安装好TestNG后重启eclipse查看是否安装好,Help-->About Eclipse-->Installation Details,如图:
![](http://images.cnitblog.com/blog/77835/201312/20091751-d97a9298008f4327bde93edce1441651.jpg)
二、使用TestNG来运行单个测试案例:
1、新建TestHelloWorldTestNG.java类,目录结构如下:
![](http://images.cnitblog.com/blog/77835/201312/20095038-26cc5ebf51ea47cc9f4ad001774efb59.jpg)
2、测试代码:
3、然后右键Run As-->TestNG Test,运行结果如下:
三、使用TestNG来运行多个测试案例:
1、增加一个失败的测试类TestHelloWorldTestNG_Fail.java:
2、在项目下新建一个Suite.xml文件:
3、目录结构:
![](http://images.cnitblog.com/blog/77835/201312/20105013-f8bd6f2e71df4dde8b33bf250fd4303f.jpg)
4、右键Suite.xml文件,Run As->TestNG Suite,如此就会运行suite.xml文件中所有的案例。
5、右键WebDriverDemo刷新项目,目录中会新增加一个test.output文件夹,打开 index.html可以看一个简单的报告。
目录:
![](http://images.cnitblog.com/blog/77835/201312/20110317-a56ab01c753d489d9ecf4a563d7434ae.jpg)
报告:
1、打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK。
TestNG官方下载地址:http://testng.org/doc/download.html
![](http://images.cnitblog.com/blog/77835/201312/20091025-99bbcd0524fa48059bb5af0e24b1e88d.jpg)
2、然后选中TestNG,单击Next安装
![](http://images.cnitblog.com/blog/77835/201312/20091217-eb8dff27c5cb40bebbededef63342115.jpg)
3、安装好TestNG后重启eclipse查看是否安装好,Help-->About Eclipse-->Installation Details,如图:
![](http://images.cnitblog.com/blog/77835/201312/20091751-d97a9298008f4327bde93edce1441651.jpg)
二、使用TestNG来运行单个测试案例:
1、新建TestHelloWorldTestNG.java类,目录结构如下:
![](http://images.cnitblog.com/blog/77835/201312/20095038-26cc5ebf51ea47cc9f4ad001774efb59.jpg)
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、目录结构:
![](http://images.cnitblog.com/blog/77835/201312/20105013-f8bd6f2e71df4dde8b33bf250fd4303f.jpg)
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可以看一个简单的报告。
目录:
![](http://images.cnitblog.com/blog/77835/201312/20110317-a56ab01c753d489d9ecf4a563d7434ae.jpg)
报告:
![](http://images.cnitblog.com/blog/77835/201312/20110709-be38d072d54042f28c3794ff506ea562.jpg)
相关文章推荐
- Selenium2(webdriver)入门之TestNG的安装与简单使用
- Selenium2(webdriver)入门之TestNG的安装与简单使用
- selenium web driver 配合使用testng
- selenium webdriver的testNG框架的介绍及使用
- selenium使用testNG测试框架编写webdriver脚本_05
- TestNG 安装及使用入门(webDriver+java环境)
- selenium+python的WebDriverWait使用方法
- 【selenium webdriver】selenium.JavascriptExecutor.executeScript() 使用实例
- 在Jmeter中使用Selenium WebDriver完成测试
- [乐意黎原创] 使用Selenium webdriver+Firefox浏览器来登录新浪微博
- 和我一起学 Selenium WebDriver(1)——入门篇
- Selenium 2.0 WebDriver使用指南
- Selenium WebDriver使用经验杂记
- 【WebDriver】selenium使用CSS定位页面元素
- windows7 32bit node.js 使用Selenium webdriver问题记录
- 终结篇:RemoteWebDriver与Grid简介-----Selenium快速入门(十五)
- Selenium Webdriver重新使用已打开的浏览器实例
- Testng + Webdriver 入门篇
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- 使用 from selenium import webdriver