How to put sql query result into an array — Selenium Webdriver?Ask Question

问题:

I'm trying to learn Selenium WebDriver and have a question that I cannot resolve long time. These are my first steps in Java and I appreciate your help. I have a code that pools out the values from the DataBase table. This code should put that query result into an array and execute it repeatedly using every next row (For Loop), but I do not know how to do it. Currently it pools all rows, but runs only last row repeatedly (5 times). Could you please help me to create correct array with for loop? Tanks a lot in advance! Here is my code:

public class DB_TFETCHdata {
ProfilesIni listProfiles = new ProfilesIni();
FirefoxProfile profile = listProfiles.getProfile("selenium");
WebDriver oWD = new FirefoxDriver(profile); 

String dbZipCode;
String dbDOBMonth;
String dbDOBDay;
String dbDOBYear;
int i = 0;

@Before

public void setUp() throws Exception{

oWD.get("https://www.ehealthinsurance.com/");       

String ConnStr = "jdbc:sqlserver://localhost:1433;databaseName=TestData1; user=sa; password=1";     
String DatabaseDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";     
String strSQL = "Select * FROM InfoTbl";

    Class.forName(DatabaseDriver);      
    Connection conn = DriverManager.getConnection(ConnStr);     
    Statement st = conn.createStatement();      
    ResultSet rs = st.executeQuery(strSQL);     
    while(rs.next()){

        dbZipCode = rs.getString("ZipCode");                
        dbDOBMonth = rs.getString("DOB_Month");             
        dbDOBDay = rs.getString("DOB_Day");             
        dbDOBYear = rs.getString("DOB_Year");   



        System.out.println(dbZipCode + "-" + dbDOBMonth + "-" + dbDOBDay + "-" + dbDOBYear);


    //System.out.println("There were " + rowCount + " records.");
    // rs.close();
     //st.close();
     //conn.close();    
}

}

@Test
public void testLogin() throws Exception {


    for (int i = 1; i<5; i++){
        /*String strZipCode = oResultset [i][0];
        String strDOBMonth = oResultset [i][1];
        String strDOBDay = oResultset [i][2];
        String strDOBYear = ArrXlDataLocal [i][3];*/

    //new Select(oWD.findElement(By.name("insuranceType"))).selectByVisibleText("Dental");

    //new Select(oWD.findElement(By.name("insuranceType"))).selectByVisibleText("Dental");
    oWD.findElement(By.linkText("Dental")).click();
    Thread.sleep(4000);
    oWD.findElement(By.id("zipCode")).clear();
    oWD.findElement(By.id("zipCode")).sendKeys(dbZipCode);
    oWD.findElement(By.id("goBtn")).click();
    oWD.findElement(By.id("census_primary_genderMALE")).click();

    oWD.findElement(By.id("census_primary_month")).clear();
    oWD.findElement(By.id("census_primary_month")).sendKeys(dbDOBMonth);
    oWD.findElement(By.id("census_primary_day")).clear();
    oWD.findElement(By.id("census_primary_day")).sendKeys(dbDOBDay);
    oWD.findElement(By.id("census_primary_year")).clear();
    oWD.findElement(By.id("census_primary_year")).sendKeys(dbDOBYear);
    oWD.findElement(By.id("census_primary_tobacco")).click();
    oWD.findElement(By.id("continue-btn")).click();
    Thread.sleep(10000);
    String strNumOfPlans = oWD.findElement(By.cssSelector("span.text-pink")).getText();
    String strNumOfPlans2 = oWD.findElement(By.xpath("//*[@id='quote-title']/strong")).getText();

    System.out.println("Here are the " + strNumOfPlans +" bestselling plans. Plans start at " + strNumOfPlans2);

}
}



@After
public void TearDown(){

}

}

回答1:


You can use dataProvider feature of TestNG. Create a new function for dataProvider and do the database fetch steps inside this function, while reading each recordset, store the values in the object array. See below example code, did not test this code for errors.

@DataProvider
public Object[][] getData()
{

// open DB connection, get record set and store values in array object

//Rows - Number of times your test has to be repeated.
//Columns - Number of parameters in test data.
Object[][] data = new Object[3][2];

// you can dynamically read the rows/columns of recordset instead of hardcoding(new Object[3][2])

// 1st row
data[0][0] ="value1";
data[0][1] = "value2";

// similarly for all rows in the record set

return data;
}


@Test(DataProvider="getData")
 public void testLogin(){

}
标签: arrays selenium webdriver
© 2014 TuiCode, Inc.