zoukankan      html  css  js  c++  java
  • TestNG的简单使用

    二、使用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:UsersAdministratorAppDataLocalTemp	estng-eclipse-332204777	estng-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:workspaceWebDriverDemoSuite.xml
    
    ===============================================
    seleniumcn.cn.demo
    Total tests run: 2, Failures: 1, Skips: 0
    ===============================================
    复制代码

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

    目录:

    报告:

    作者:Glen.He 
    出处:http://www.cnblogs.com/puresoul/ 

    http://www.cnblogs.com/puresoul/p/3483092.html
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

  • 相关阅读:
    实现JavaScript自定义函数的整合、链式调用及类的封装
    jQuery事件
    jQuery特效
    jQuery基础细节
    CSS3盒模型
    主流清浮动方法
    JavaScript 执行环境及作用域
    JavaScript 参数传递与变量复制
    PHP雪花背景验证码
    Linkis 0.9.2 版本发布
  • 原文地址:https://www.cnblogs.com/111testing/p/6166305.html
Copyright © 2011-2022 走看看