Selenium是否支持数据库测试?如果是的话,该怎么做?
答案 0 :(得分:5)
浏览器测试工具不适合进行数据库测试。为此,您使用常规单元测试框架,因为所有数据库访问都在您的服务器端代码中。
除非您的数据库访问权限基于浏览器,否则您遇到的问题比选择测试框架要大。
答案 1 :(得分:1)
如果要连接到数据库,请使用数据库连接API,例如JDBC for Java。
答案 2 :(得分:0)
考虑使用TestPlan,它允许您将Web UI测试与自定义Java测试单元结合使用。然后,这些测试单元可以访问您的数据库并在UI脚本中使用它。
答案 3 :(得分:0)
For database testing youcan make a connection to the database using JDBC driver and then can fetch data or execute sql queries to perform content testing.
For example-
import java.sql.*;
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
//Now here you can test wheather the data you have entered through User Interface gets entered into the database
--Write the code for comparision
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
//end finally try
}
//end try
}
//end main
}
//end