如何使用Selenium Web Driver + Test NG将SQL查询结果写入excel

时间:2019-09-19 22:05:53

标签: excel selenium-webdriver apache-poi

如何使用Selenium Web Driver + Test NG将SQL查询结果写入excel

示例:

@Test(priority=5) 
    public void VerifyDataBase() {


 try {
        String query = "SELECT *  FROM [xxx].[dbo].[xxx_FEINProductionCompany] where ProductionCompanyName like'%xxx%'";
        System.out.println(query);
        statement = connection.createStatement();
        rs = statement.executeQuery(query);

        while (rs.next()) {

            int FEIN_ID = rs.getInt("ACA_FEINProductionCompany_ID");
            int FEIN_No = rs.getInt("FEIN");
            String ProductionCompanyName = rs.getString("ProductionCompanyName");
            String ProductionCompanyShortName = rs.getString("ProductionCompanyShortName");
            String  ModifiedDate = rs.getString("ModifiedDate");
            String Username = rs.getString("Username");
            String AddressLine1 = rs.getString("AddressLine1");
            String AddressLine2 = rs.getString("AddressLine2");
            String City = rs.getString("City");
            String State = rs.getString("State");
            int Zip = rs.getInt("Zip");
            String PhoneNumber = rs.getString("PhoneNumber");
            String ContactName = rs.getString("ContactName");
            String ContactPhone = rs.getString("ContactPhone");
            System.out.println("\nProductionCompanyName is: " + ProductionCompanyName);
            System.out.println("\nFEIN is: " + FEIN_No); 
            System.out.println("\nAddressLine2 is: " + AddressLine2);
            System.out.println("\nUsername is: " + Username);
            TestUtil.assertEquals(Username, "xxxx","FEINScreen_Username"); 
            TestUtil.assertEquals(AddressLine2, "2500 xx","FEINScreen_AddressLine2"); 
            System.out.println("\nQuery Result Set is: "+ ProductionCompanyName+"\t"+FEIN_No+"\t"+AddressLine2+"\t"+Username);
            System.out.println("\t");
        }

    }   
    catch (SQLException ex) {
        ex.printStackTrace();
    } 

}

1 个答案:

答案 0 :(得分:0)

您需要使用Java库/依赖项(如apache poi)。 Selenium或TestNG没有与Excel交互的功能。

使用以下链接代码并将您的sql查询数据作为输入参数传递给其中

引用:

https://howtodoinjava.com/library/readingwriting-excel-files-in-java-poi-tutorial/