I am trying to make some data-driven tests using database data and then using TestNG @DataProvider. I have created the below table in MySQL that has the following columns and values. I am trying to use the browser, followed by the username and password in each row to log into a website, for a total of 3 tests.
scenario, username, password
chrome johnsmith password1
firefox janesmith password2
edge username3 password3
I have the below code to loop through the ResultSet:
@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;
}
However, it seems it is having issues trying to loop. The tests run, but it is only using each column of the first row as the variables for each browser run. Do I have to use rs.next() somewhere here instead to iterate through the rows? What else is wrong with my logic here? Output/results seen below:
[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")
I was able to achieve this with this code:
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;