代码之家  ›  专栏  ›  技术社区  ›  Vince

是否遍历DataProvider的JDBC结果集?

  •  0
  • Vince  · 技术社区  · 6 年前

    我正在尝试使用数据库数据进行一些数据驱动测试,然后使用TestNG@DataProvider。我在MySQL中创建了下表,其中包含以下列和值。我正在尝试使用浏览器,然后是每行的用户名和密码来登录网站,总共进行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()来迭代行?我的逻辑还有什么问题吗?输出/结果如下所示:

        [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")
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Vince    6 年前

    我能够通过以下代码实现这一点:

        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;