Selenium2+TestNG的使用

转载自:http://www.cnblogs.com/puresoul/archive/2013/12/20/3483092.html

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

目录:

报告:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值