如何将sql查询结果放入数组 - Selenium Webdriver?

时间:2016-04-15 03:55:28

标签: sql arrays selenium webdriver

我正在尝试学习Selenium WebDriver,并且有一个我无法解决很长时间的问题。这是我在Java中的第一步,感谢您的帮助。 我有一个代码,用于汇总DataBase表中的值。这段代码应该将查询结果放入一个数组中并使用每一行(For Loop)重复执行它,但我不知道该怎么做。目前它汇集所有行,但重复运行最后一行(5次)。你能帮我用for循环创建正确的数组吗?坦克提前很多!这是我的代码:

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 个答案:

答案 0 :(得分:0)

您可以使用TestNG的dataProvider功能。为dataProvider创建一个新函数,并在此函数内执行数据库获取步骤,同时读取每个记录集,将值存储在对象数组中。请参阅下面的示例代码,没有测试此代码是否存在错误。

@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(){

}