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

简介: 上一篇已经搭建好了Eclipse+selenium2的环境,这一篇主要记录下TestNG的使用。 一、在Eclipse中安装TestNG 1、打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK。

 上一篇已经搭建好了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、测试代码: 

 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可以看一个简单的报告。

目录:

报告:

 

目录
相关文章
|
4月前
selenium 实战
selenium 实战
26 0
|
移动开发 前端开发 算法
Selenium和Appnium、Airtest的底层原理
Selenium和Appnium、Airtest的底层原理
1040 0
|
5月前
|
Web App开发 数据采集 前端开发
Selenium超级详细的教程
Selenium是一个用于自动化测试的工具,它可以模拟用户在浏览器中的各种操作。除了用于测试,Selenium还可以用于爬虫,特别是在处理动态加载页面时非常有用。本文将为您提供一个超级详细的Selenium教程,以帮助您快速入门并了解其各种功能和用法。
|
11月前
|
API
selenium的简单实用以及实战
selenium的简单实用以及实战
|
JavaScript
web自动化之selenium的特殊用法(一)
文章目录 web自动化之selenium的特殊用法(一) 1、get_attribute() 2、js滚动页面 3、Tab键点击页面未展示元素 4、通过空格键执行页面滚动操作 1.摁空格键 2.报错:TypeError: list indices must be integers or slices, not WebElement ———————————————— 版权声明:本文为CSDN博主「梦无矶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_46158060/article/details/
311 0
|
Web App开发 JavaScript 测试技术
Selenium Webdriver 简易教程2
Selenium Webdriver 简易教程2
92 0
|
Web App开发 前端开发 JavaScript
Selenium Webdriver 简易教程
Selenium Webdriver 简易教程
95 0
|
JavaScript 前端开发 Java
Selenium【Selenium环境搭建与Junit5】
Selenium【Selenium环境搭建与Junit5】
Selenium【Selenium环境搭建与Junit5】
|
前端开发
web自动化之selenium的特殊用法(二)
文章目录 web自动化之selenium的特殊用法(二) selenium-键盘操作,keys的的使用 (1)keys包的导入 (2)组合键使用 (4)常用组合键 (5)常用功能键 Python-Selenium:如何通过click在新的标签页打开链接? selenium 带有空格的class name且不唯一的元素定位
124 0
|
Web App开发 JavaScript 前端开发