迭代 DataProvider 的 JDBC 个结果集?
Iterating through JDBC ResultSet for DataProvider?
我正在尝试使用数据库数据进行一些数据驱动的测试,然后使用 TestNG @DataProvider。我在 MySQL 中创建了下面的 table,它具有以下列和值。我正在尝试使用浏览器,然后在每一行中输入用户名和密码来登录网站,总共进行了 3 次测试。
scenario, username, password
chrome johnsmith password1
firefox janesmith password2
edge username3 password3
我有以下代码循环遍历结果集:
@DataProvider
public Object[][] getData() throws SQLException {
String host = "localhost";
String port = "3306";
Connection con = DriverManager.getConnection(
"jdbc:mysql://" + host + ":" + port + "/qadbdemo2" + "?useSSL=false", "root", "password");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select * from credentials;");
int colCount = rs.getMetaData().getColumnCount();
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
rs.next();
Object data[][] = new Object[rowCount][colCount];
for (int rNum = 1; rNum <= rowCount; rNum++) {
for (int cNum = 0; cNum < colCount; cNum++) {
System.out.print(rs.getObject(rNum) + " ");
data[rNum - 1][cNum] = rs.getObject(rNum);
}
System.out.println();
}
return data;
}
但是,它似乎在尝试循环时遇到了问题。测试 运行,但它仅使用第一行的每一列作为每个浏览器的变量 运行。我是否必须在此处的某处使用 rs.next() 而不是遍历行?我的逻辑在这里还有什么问题? Output/results 如下所示:
[RemoteTestNG] detected TestNG version 6.14.2
chrome chrome chrome
johnsmith johnsmith johnsmith
password1 password1 password1
Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 26256
Only local connections are allowed.
May 03, 2018 1:17:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
PASSED: doLogin("chrome", "chrome", "chrome")
FAILED: doLogin("johnsmith", "johnsmith", "johnsmith")
FAILED: doLogin("password1", "password1", "password1")
我能够使用以下代码实现此目的:
Object[][] data = new Object[rowCount][colCount];
int row = 0;
while (rs.next()) {
for (int i = 0; i < colCount; i++) {
data[row][i] = rs.getObject(i+1);
}
row++;
}
return data;
我正在尝试使用数据库数据进行一些数据驱动的测试,然后使用 TestNG @DataProvider。我在 MySQL 中创建了下面的 table,它具有以下列和值。我正在尝试使用浏览器,然后在每一行中输入用户名和密码来登录网站,总共进行了 3 次测试。
scenario, username, password
chrome johnsmith password1
firefox janesmith password2
edge username3 password3
我有以下代码循环遍历结果集:
@DataProvider
public Object[][] getData() throws SQLException {
String host = "localhost";
String port = "3306";
Connection con = DriverManager.getConnection(
"jdbc:mysql://" + host + ":" + port + "/qadbdemo2" + "?useSSL=false", "root", "password");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select * from credentials;");
int colCount = rs.getMetaData().getColumnCount();
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
rs.next();
Object data[][] = new Object[rowCount][colCount];
for (int rNum = 1; rNum <= rowCount; rNum++) {
for (int cNum = 0; cNum < colCount; cNum++) {
System.out.print(rs.getObject(rNum) + " ");
data[rNum - 1][cNum] = rs.getObject(rNum);
}
System.out.println();
}
return data;
}
但是,它似乎在尝试循环时遇到了问题。测试 运行,但它仅使用第一行的每一列作为每个浏览器的变量 运行。我是否必须在此处的某处使用 rs.next() 而不是遍历行?我的逻辑在这里还有什么问题? Output/results 如下所示:
[RemoteTestNG] detected TestNG version 6.14.2
chrome chrome chrome
johnsmith johnsmith johnsmith
password1 password1 password1
Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 26256
Only local connections are allowed.
May 03, 2018 1:17:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
PASSED: doLogin("chrome", "chrome", "chrome")
FAILED: doLogin("johnsmith", "johnsmith", "johnsmith")
FAILED: doLogin("password1", "password1", "password1")
我能够使用以下代码实现此目的:
Object[][] data = new Object[rowCount][colCount];
int row = 0;
while (rs.next()) {
for (int i = 0; i < colCount; i++) {
data[row][i] = rs.getObject(i+1);
}
row++;
}
return data;