Actually seleniumhq gives a good start point for such database-validation approach.
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.
Simple example code:
// 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)