我正在尝试使用带有Java的Automation Selenium WebDriver将MySQL数据库中的值传递给UI?代码只返回第一行。
以下是数据库值:
我已经多次使用int循环更改了代码,并收到了不同类型的错误,例如:
我无法弄明白。我不确定我做错了什么。
基本上,在这种情况下,我有firstName和lastName通过数据库传递给UI。我想将这些值传递给我的测试用例。我不想硬编码。目前,我有一个包含2列的表,一列是firstName,第二列是lastName。
我希望遍历数据库并在任何我想要的地方选择这些值。的SendKeys。
这是我的测试用例代码文件。
下面的FacebookTest文件代码。
import static org.testng.Assert.assertThrows;
import java.sql.Array;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import selenium.ReadExcel;
public class FacebookTest
{
@SuppressWarnings({ "static-access", "unchecked" })
public static void main(String[] args) throws Exception
//public static void facebookTest()
{
// pointing to the file, now FF must have pointer as well in Selenium 3.0
// WebDriver
System.setProperty("webdriver.gecko.driver","geckodriver.exe");
// OPEN FF
FirefoxDriver driver = new FirefoxDriver();
DBTestData db = new DBTestData();
// ENTER URL
driver.get("http://www.facebook.com/");
try
{
ResultSet testData = db.data("","");
for(int i = 0; i < 10; i++)
{
String firstName = testData.getString("firstName");
String lastName = testData.getString("lastName");
System.out.println(firstName);
System.out.println(lastName);
driver.findElement(By.id("u_0_e")).clear();
driver.findElement(By.id("u_0_e")).sendKeys(firstName);
driver.findElement(By.id("u_0_g")).clear();
driver.findElement(By.id("u_0_g")).sendKeys(lastName);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
driver.quit();
}
}
}
这是我的数据库文件,我从数据库中获取值并将其返回到测试用例101 - 请参阅下面的代码。
下面的DBTestData代码文件。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
public class DBTestData {
// public static void main(String[] args) throws Exception
@SuppressWarnings({ "unused" })
// public static String data() throws Exception
public static ResultSet data(String firstName, String lastName) throws ClassNotFoundException, SQLException
{
// Accessing driver from the JAR file
Class.forName("com.mysql.jdbc.Driver");
// Creating a variable for the connection called "con"
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
// after localhost:3306 is where you will put the name of the database
// jdbc:mysql://localhost:3606/testdb ---> this is a database
// root is a database user and password too
// our query below
PreparedStatement statement = con.prepareStatement("SELECT firstName, lastName FROM test_data");
// creating variable to execute query
ResultSet result = statement.executeQuery();
// List sqlData = new ArrayList();
ArrayList<String> sqlData = new ArrayList<String>();
while (result.next())
{
sqlData.add(firstName = result.getString(firstName));
sqlData.add(lastName = result.getString(lastName));
}
return result;
}
}
让我知道如何实现这一概念。一旦我掌握了基础知识,我将使用相同的文件来测试其他测试用例。为什么我们不能使用相同的概念2D数组将值从数据库传递到UI?
答案 0 :(得分:0)
以下是我解决问题的方法。我在我的测试用例中使用了do while循环,它能够从所有行获取所有数据并在我的网页中迭代它。
以下是代码。
FacebookTest代码文件:
import static org.testng.Assert.assertThrows;
import java.sql.Array;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import selenium.ReadExcel;
public class FacebookTest
{
@SuppressWarnings({ "static-access", "unchecked" })
public static void main(String[] args) throws Exception
{
// pointing to the file, now FF must have pointer as well in Selenium 3.0 WebDriver
System.setProperty("webdriver.gecko.driver","geckodriver.exe");
// OPEN FF
FirefoxDriver driver = new FirefoxDriver();
DBTestData db = new DBTestData();
// ENTER URL
driver.get("http://www.facebook.com/");
try
{
ResultSet testData = db.data("","");
do
{
String firstName = testData.getString(1);
String lastName = testData.getString(2);
driver.findElement(By.id("u_0_e")).clear();
driver.findElement(By.id("u_0_e")).sendKeys(firstName);
driver.findElement(By.id("u_0_g")).clear();
driver.findElement(By.id("u_0_g")).sendKeys(lastName);
}
while(testData.next());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
driver.quit();
}
}
}
这是我的DBTestData文件:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
public class DBTestData
{
public static ResultSet data(String firstName, String lastName) throws ClassNotFoundException, SQLException
{
// Accessing driver from the JAR file
Class.forName("com.mysql.jdbc.Driver");
// Creating a variable for the connection called "con"
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
// after localhost:3306 is where you will put the name of the database
// jdbc:mysql://localhost:3606/testdb ---> this is a database
// root is a database user and password too
// our query below
PreparedStatement statement = con.prepareStatement("SELECT firstName, lastName FROM test_data");
// creating variable to execute query
ResultSet result = statement.executeQuery();
// store all the rows data into one variable
ArrayList<String> sqlData = new ArrayList<String>();
while(result.next())
{
sqlData.add(firstName= result.getString("firstName"));
sqlData.add(lastName = result.getString("lastName"));
break;
}
return result;
}
}
那么,如何正确关闭数据库连接?
如何迭代n行?
由于
答案 1 :(得分:0)
con.close();是关闭数据库的命令