上面的Python代碼打開一個文本文件,這個文件每行包含不同的搜索字符串。然后代碼保存字符串到一個字符串數組,對數值進行遍歷,使用搜索字符串進行查詢,并進行斷言。
這是一個非常簡單的例子,但其中的思路表明,可以很簡單的使用編程、腳本語言進行數據驅動的測試。有關更多示例,請參閱 Selenium RC wiki 來了解如何從電子表格讀取數據或使用TestNG的提供數據。此外,這是一個在自動化測試的專業(yè)人士圈內眾所周知的話題之一,包括那些不使用Selenium的自動化圈子,因此搜索互聯(lián)網上的“數據驅動測試”,會得到許多關于這一主題的博客。
數據庫驗證
另一種常見的測試類型是,比較用戶界面上的數據和存儲在后臺數據庫中的數據。因為你也可以使用一種編程語言進行數據庫查詢,假設你有數據庫相關的函數,你可以用它們來檢索數據,然后使用這些數據來驗證頁面上所顯示的數據是正確的。
考慮如下例子,從數據庫中進行檢索注冊電子郵件地址,然后再和界面上的數據進行比較。代碼如下,先建立一個數據庫連接,并從數據庫中檢索數據,使用的是Java語言:
// 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");
// Fetch value of "email_address" from "result" object.
String emailaddress = result.getString("email_address");
// Use the emailAddress value to login to application.
selenium.type("userID", emailaddress);
selenium.type("password", secretPassword);
selenium.click("loginButton");
selenium.waitForPageToLoad(timeOut);
Assert.assertTrue(selenium.isTextPresent("Welcome back" +emailaddress), "Unable to log in for user" +emailaddress)
這是一個簡單的Java例子從數據庫中檢索數據。
本文轉載自:http://www.loggingselenium.com/