selenium webdriver 学习总结-数据驱动(六)

晒酷学院qq群:979438600
webdriver可以结合junit中的Parameterized运行器完成数据驱动的目的,数据驱动的方式很多,可以结合csv文件,excel文件,jdbc等,下面我将结合csv,jdbc来展示如何完成数据驱动测试。

一、先给大家介绍一下如何使用Parameterized运行器,两种方式实现。
1、第一种方式,通过构造方法初始化测试数据
代码示例:

package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;

@RunWith(value = Parameterized.class)
public class ParamConstructor {
	private String username; 
	private String password;
	public ParamConstructor(String username, String password) {
		this.username = username;
		this.password = password;
	}

	@Parameters
	public static Collection<String[]> getData() {
	return Arrays.asList(new String[][] { { "ray", "123" }, "venus","123" },{ "jupiter", "123" }, { "mercury", "123" } });
	}
	
	@Test
	public void test() {
		System.out.println(this.username + " " + this.password);
	}
}        

2、第二种方式,通过Annotation Parameter指定需要参数化的属性

package junit.parameters;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;


@RunWith(value = Parameterized.class)
public class ParamNoConstructor {

	@Parameter(value = 0) //value 的值指定该属性对应getData返回数组中的序数
	public String username; // 此处须声明为public
	
	@Parameter(value = 1)
	public String password;


	@Parameters
	public static Collection<String[]> getData() {
		return Arrays
			.asList(new String[][] { { "ray", "123" }, { "venus","123" },{ "jupiter", "123" }, { "mercury", "123" } });
	}


	@Test
	public void test() {
		System.out.println(this.username + " " + this.password);
	}
}

通过以上两个例子大家应该对Parameterized有了充分的认识
二、使用csv文件完成数据驱动
首先,我们定义一个DBUtils的数据驱动类,并实现csv驱动的方法,代码示例如下:

csv文件路径:src/test/resources/userInfo.csv,该文件中的文本内容如下:

username,password
ray,123
venus,456
jupiter,789
mercury,369
mars,258
saturn,147

DBUtils类:

package junit.parameters;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class DBUtils {

@SuppressWarnings("resource")
public static List<String[]> driverCSV(String path) {
	List<String[]> list = new ArrayList<String[]>();
	InputStream in = null;
	BufferedReader buf = null;
	try {
		in = new FileInputStream(path);
		buf = new BufferedReader(new InputStreamReader(in));
		String temp;
		String info[];
		int count = 0;
		while ((temp = StringUtils.trimToEmpty(buf.readLine())) != "") {
			if (count == 0) {
				count = 1;
				continue;
			}
			info = temp.split(",");
			list.add(info);
		}


	} catch (Exception e) {
		e.printStackTrace();
	}
	return list;
	}
}

测试类:

package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Collection;

@RunWith(value = Parameterized.class)
public class TestCSVDriver {
	private String username;
	private String password;
	public TestCSVDriver(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	@Parameters
	public static Collection<String[]> getData() {
		return DBUtils.driverCSV("src/test/resources/userInfo.csv");
	}
	
	@Test
	public void test() {
		System.out.println(this.username + " " + this.password);
	}
}

三、使用JDBC完成数据驱动测试
这里我们假定一个User的数据库,数据库中有名为t_user的用户信息表,表中的内容与以上csv文件中定义的相同
1、首先配置mysql-connector-java-*.jar到环境变量中
2、
DBUtils类:

package junit.parameters;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class DBUtils {
	public static List<String[]> driverJDBC(String URL, String username,String password) {
		List<String[]> list = new ArrayList<String[]>();
		String[] tmp = new String[] {};
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select username,password from t_user where id <= ?";
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			con = DriverManager.getConnection(URL, username, password);
			ps = con.prepareStatement(sql);
			ps.setInt(1, 5);
			rs = ps.executeQuery();
			while (rs.next()) {
				tmp[0] = rs.getString(username);
				tmp[1] = rs.getString(password);
				list.add(tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}

测试类:

package junit.parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Collection;

@RunWith(value = Parameterized.class)
public class TestJDBCDriver {
	private String username; 
	private String password;
	
	public TestJDBCDriver(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	@Parameters
	public static Collection<String[]> getData() {
		return DBUtils.driverJDBC("jdbc:mysql://localhost:330/User","ray","123456");
	}

	@Test
	public void test() {
	System.out.println(this.username + " " + this.password);
	}
}
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页