使用 java 和 selenium webdriver 进行完整的端到端测试
Full end to end testing using java and selenium webdriver
我最近创建了一个框架,可以成功测试我项目的 GUI。现在我想通过创建功能测试的完整端到端测试来扩展我的测试。这些测试将执行以下核心步骤。
- 使用 GUI 创建配置文件(这是使用 Java selenium webdriver 完成的)
- 自动调用存储在我将使用 putty 连接到的服务器上的 unix 脚本 运行
- 脚本运行验证数据库中的预期信息后
我已经完成了第一步 我想知道是否应该将步骤 2 和 3 集成到我现有的框架中?我可以用 Java 完成所有这些吗?是否有任何文档可以帮助我完成第 2 步和第 3 步?
在 Java 代码中使用以下命令 运行 脚本。
Runtime.getRuntime().exec(myShellScript);
之后,使用
JDBC 验证数据库中数据的连接。
第二步使用下面的代码
你真的应该看看Process Builder。真是为这种事而生。
ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();
步骤3可以参考this reference。
实际上 seleniumhq 为这种数据库验证方法提供了一个良好的起点。
Another common type of testing is to compare data in the UI against the data actually stored in the AUT’s database. Since you can also do database queries from a programming language, assuming you have database support functions, you can use them to retrieve data and then use the data to verify what’s displayed by the AUT is correct.
简单示例代码:
// Load Microsoft SQL Server JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Prepare connection url.
String url = "jdbc:sqlserver://192.168.1.180:1433;DatabaseName=TEST_DB";
// Get connection to DB.
public static Connection con =
DriverManager.getConnection(url, "username", "password");
// Create statement object which would be used in writing DDL and DML
// SQL statement.
public static Statement stmt = con.createStatement();
// Send SQL SELECT statements to the database via the Statement.executeQuery
// method which returns the requested information as rows of data in a
// ResultSet object.
ResultSet result = stmt.executeQuery
("select top 1 email_address from user_register_table");
// Move cursor from default position to first row of result set.
result.next();
// Fetch value of "email_address" from "result" object.
String emailaddress = result.getString("email_address");
// Use the emailAddress value to login to application.
driver.findElement(By.id, "userID").sendKeys(emailaddress);
driver.findElement(By.id, "password").sendKeys(secretPassword);
driver.findElement(By.id, "loginButton").click();
WebElement element = driver.findElement(By.xpath, "//*[contains(.,'Welcome back ')]");
Assert.assertTrue(element.getText().contains(emailaddress), "Unable to log in for user" + emailaddress)
我最近创建了一个框架,可以成功测试我项目的 GUI。现在我想通过创建功能测试的完整端到端测试来扩展我的测试。这些测试将执行以下核心步骤。
- 使用 GUI 创建配置文件(这是使用 Java selenium webdriver 完成的)
- 自动调用存储在我将使用 putty 连接到的服务器上的 unix 脚本 运行
- 脚本运行验证数据库中的预期信息后
我已经完成了第一步 我想知道是否应该将步骤 2 和 3 集成到我现有的框架中?我可以用 Java 完成所有这些吗?是否有任何文档可以帮助我完成第 2 步和第 3 步?
在 Java 代码中使用以下命令 运行 脚本。
Runtime.getRuntime().exec(myShellScript);
之后,使用 JDBC 验证数据库中数据的连接。
第二步使用下面的代码
你真的应该看看Process Builder。真是为这种事而生。
ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();
步骤3可以参考this reference。
实际上 seleniumhq 为这种数据库验证方法提供了一个良好的起点。
Another common type of testing is to compare data in the UI against the data actually stored in the AUT’s database. Since you can also do database queries from a programming language, assuming you have database support functions, you can use them to retrieve data and then use the data to verify what’s displayed by the AUT is correct.
简单示例代码:
// Load Microsoft SQL Server JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Prepare connection url.
String url = "jdbc:sqlserver://192.168.1.180:1433;DatabaseName=TEST_DB";
// Get connection to DB.
public static Connection con =
DriverManager.getConnection(url, "username", "password");
// Create statement object which would be used in writing DDL and DML
// SQL statement.
public static Statement stmt = con.createStatement();
// Send SQL SELECT statements to the database via the Statement.executeQuery
// method which returns the requested information as rows of data in a
// ResultSet object.
ResultSet result = stmt.executeQuery
("select top 1 email_address from user_register_table");
// Move cursor from default position to first row of result set.
result.next();
// Fetch value of "email_address" from "result" object.
String emailaddress = result.getString("email_address");
// Use the emailAddress value to login to application.
driver.findElement(By.id, "userID").sendKeys(emailaddress);
driver.findElement(By.id, "password").sendKeys(secretPassword);
driver.findElement(By.id, "loginButton").click();
WebElement element = driver.findElement(By.xpath, "//*[contains(.,'Welcome back ')]");
Assert.assertTrue(element.getText().contains(emailaddress), "Unable to log in for user" + emailaddress)