selenium使用TestNG实现DDT

7 篇文章 0 订阅
6 篇文章 0 订阅

TestNG和excel文件实现DDT(Data Driver Test)

准备一个test-data.xlsx文件,并且放到eclipse项目下。

1.准备test_data.xlsx文件

2.利用POI读取excel文件内容,并保存到一个二维数组中。

package MavenDemo.Test;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DDT2 {
	@Test(dataProvider="testdata")  
    public void TestLogin(String username, String password) throws InterruptedException{  
  
        System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver();  
        driver.manage().window().maximize();  
        driver.get("http://baidu.com");  
          
        // click login link  
        driver.findElement(By.xpath("//ul/li[contains(text(), '账号登录')]")).click();  
        Thread.sleep(2000);  
          
        driver.findElement(By.xpath("//input[@type ='text']")).clear();  
        driver.findElement(By.xpath("//input[@type ='text']")).sendKeys(username);  
        driver.findElement(By.xpath("//input[@type ='password']")).clear();  
        driver.findElement(By.xpath("//input[@type ='password']")).sendKeys(password);  
        
        driver.findElement(By.xpath("//button[contains(text(), '立即登录')]")).click(); 
                
    }  
      
    @DataProvider(name="testdata") 
    public Object[][] TestDataFeed() throws Exception{
   
        File src = new File(".\\Files\\test_data.xlsx");  
        FileInputStream fis = new FileInputStream(src);  
           
        @SuppressWarnings("resource")  
        XSSFWorkbook wb=new XSSFWorkbook(fis);    
        XSSFSheet sh1= wb.getSheetAt(0);  
    	int numberrow = sh1.getPhysicalNumberOfRows();
    		
    	Object [][] baidudata=new Object[numberrow][2];
    	
    	for(int i=0;i<numberrow;i++){  
            
            	baidudata[i][0] = sh1.getRow(i).getCell(0).getStringCellValue();
            	baidudata[i][1] = sh1.getRow(i).getCell(1).getStringCellValue();
            }
    	
    	return baidudata;
    	
    }
}

3.如果是maven项目需要在pom.xml中添加依赖关系

               <dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.8.1</version>
		</dependency>


		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>4.1.0</version>
		</dependency>

运行脚本,发现启动了两次chrome浏览器,并可以看到测试了两次登录,分别用了不同用户名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值