Selenium之读取CSV配置文件

Selenium参数化之读取CSV文件:

       我们还是以登录软件测试论坛为例,先创建封装类,封装打开论坛的方法和读取CSV文件的方法,再创建测试类:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CSV {

    public static WebDriver driver; //封装driver方法

    public static void get51Testing() throws InterruptedException {   //封装打开软件测试论坛首页的方法
        System.setProperty("webdriver.firefox.marionette", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        String Url = "http://bbs.51testing.com/forum.php";
        driver =new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(Url);
        driver.manage().window().maximize();
        Thread.sleep(2000);
    }

    public static Object [][] readCSV(String fileName) throws IOException {
        //封装读取CSV文件的的静态方法,使用绝对路径作为参数
        List<Object[]> records = new ArrayList<Object[]>();
        String record;
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
        file.readLine();                           //忽略第一行
        while ((record=file.readLine())!=null){        //遍历读取文件中除第一行以外的所有行的内容
            String fields[] =  record.split(",");  //存储在名为records的ArrayList中
            records.add(fields);                    //每一个recods中存储的对象为一个String数组
        }
        file.close();  //关闭文件对象

        Object[][] results = new Object[records.size()][]; //定义函数返回值,即Object[][]
        for (int i=0; i<records.size();i++){            //将存储测试数据的List转换为一个人Object的二维数组
            results[i] = records.get(i);               //设置二维数组每行的值,每行是一个Object对象
        }
        return results;
    }

}

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class Login51Testing {
    @BeforeMethod
    public void beforeMethod() throws InterruptedException {
        CSV.get51Testing(); //调用打开软件测试论坛首页的方法
    }

    @Test(dataProvider = "login")   //登录的测试用例
    public void login(String username, String password, String example, String response) throws InterruptedException {
        CSV.driver.findElement(By.xpath(".//*[@id='ls_username']")).sendKeys(username);
        //输入用户名
        CSV.driver.findElement(By.xpath(".//*[@id='ls_password']")).sendKeys(password);
        //输入密码
        CSV.driver.findElement(By.xpath(".//*[@id='lsform']/div/div[1]/table/tbody/tr[2]/td[3]/button")).click();
        //点击登录
        Thread.sleep(2000);

        Assert.assertTrue(CSV.driver.getPageSource().contains(response));
        //断言
        Reporter.log(example);
        //打印日志
    }

    @AfterMethod
    public void afterMethod() {
        CSV.driver.quit();
    }

    @DataProvider(name = "login")   //文件名
    public Object[][] dp() throws IOException {
        return CSV.readCSV("D:\\login.csv");    //文件的路径
    }

}

这是csv格式的配置文件:


手机号 密码 用例名称 断言
AAAAAA,123456,用户名错误的测试用例,登录失败
abcdef,111111,密码错误的测试用例,登录失败
,123456,用户名为空的测试用例,登录失败
abcdef,,密码为空的测试用例,抱歉
,,用户名和密码都为空的测试用例,抱歉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值