上面的Python代碼打開(kāi)一個(gè)文本文件,這個(gè)文件每行包含不同的搜索字符串。然后代碼保存字符串到一個(gè)字符串?dāng)?shù)組,對(duì)數(shù)值進(jìn)行遍歷,使用搜索字符串進(jìn)行查詢,并進(jìn)行斷言。
這是一個(gè)非常簡(jiǎn)單的例子,但其中的思路表明,可以很簡(jiǎn)單的使用編程、腳本語(yǔ)言進(jìn)行數(shù)據(jù)驅(qū)動(dòng)的測(cè)試。有關(guān)更多示例,請(qǐng)參閱 Selenium RC wiki 來(lái)了解如何從電子表格讀取數(shù)據(jù)或使用TestNG的提供數(shù)據(jù)。此外,這是一個(gè)在自動(dòng)化測(cè)試的專業(yè)人士圈內(nèi)眾所周知的話題之一,包括那些不使用Selenium的自動(dòng)化圈子,因此搜索互聯(lián)網(wǎng)上的“數(shù)據(jù)驅(qū)動(dòng)測(cè)試”,會(huì)得到許多關(guān)于這一主題的博客。
數(shù)據(jù)庫(kù)驗(yàn)證
另一種常見(jiàn)的測(cè)試類型是,比較用戶界面上的數(shù)據(jù)和存儲(chǔ)在后臺(tái)數(shù)據(jù)庫(kù)中的數(shù)據(jù)。因?yàn)槟阋部梢允褂靡环N編程語(yǔ)言進(jìn)行數(shù)據(jù)庫(kù)查詢,假設(shè)你有數(shù)據(jù)庫(kù)相關(guān)的函數(shù),你可以用它們來(lái)檢索數(shù)據(jù),然后使用這些數(shù)據(jù)來(lái)驗(yàn)證頁(yè)面上所顯示的數(shù)據(jù)是正確的。
考慮如下例子,從數(shù)據(jù)庫(kù)中進(jìn)行檢索注冊(cè)電子郵件地址,然后再和界面上的數(shù)據(jù)進(jìn)行比較。代碼如下,先建立一個(gè)數(shù)據(jù)庫(kù)連接,并從數(shù)據(jù)庫(kù)中檢索數(shù)據(jù),使用的是Java語(yǔ)言:
// 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)
這是一個(gè)簡(jiǎn)單的Java例子從數(shù)據(jù)庫(kù)中檢索數(shù)據(jù)。
本文轉(zhuǎn)載自:http://www.loggingselenium.com/