</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How to perform database testing in Selenium?

Selenium does not support databases directly. Use JDBC (Java Database Connectivity) driver to connect to any database and validate data as part of your Selenium test.

Answer

Database Testing in Selenium

Selenium itself cannot interact with databases — it''s a browser automation tool. However, you can combine Selenium with JDBC (Java Database Connectivity) to validate database state as part of your tests.

Using JDBC to connect to a database:

Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DatabaseTest {

    @Test
    public void verifyUserInDatabase() throws Exception {
        // JDBC connection
        String dbUrl = "jdbc:mysql://localhost:3306/testdb";
        String dbUser = "root";
        String dbPassword = "password";

        Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        Statement stmt = conn.createStatement();

        // Query the database
        ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE email=''test@example.com''");

        // Validate result
        if (rs.next()) {
            String name = rs.getString("name");
            String email = rs.getString("email");
            System.out.println("User found: " + name + " — " + email);
            Assert.assertEquals(email, "test@example.com");
        } else {
            Assert.fail("User not found in database!");
        }

        rs.close();
        stmt.close();
        conn.close();
    }
}

End-to-end test with Selenium + JDBC:

Java
@Test
public void registrationCreatesUserInDB() throws Exception {
    // Step 1: Register via browser (Selenium)
    driver.get("https://example.com/register");
    driver.findElement(By.id("name")).sendKeys("Test User");
    driver.findElement(By.id("email")).sendKeys("testuser@example.com");
    driver.findElement(By.id("password")).sendKeys("Password123");
    driver.findElement(By.id("registerBtn")).click();

    // Wait for success message
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.titleIs("Registration Successful"));

    // Step 2: Verify in database (JDBC)
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/app", "root", "pass");
    ResultSet rs = conn.createStatement().executeQuery(
        "SELECT COUNT(*) AS cnt FROM users WHERE email=''testuser@example.com''"
    );
    rs.next();
    int count = rs.getInt("cnt");
    Assert.assertEquals(count, 1, "User should exist in database after registration");
    conn.close();
}

JDBC drivers by database:

DatabaseJDBC Driver ClassMaven Artifact
MySQLcom.mysql.cj.jdbc.Drivermysql-connector-java
PostgreSQLorg.postgresql.Driverpostgresql
Oracleoracle.jdbc.driver.OracleDriverojdbc8
SQL Servercom.microsoft.sqlserver.jdbc.SQLServerDrivermssql-jdbc
H2 (in-memory)org.h2.Driverh2

Key answer: We can use JDBC driver to connect to any database in Java and validate data as part of Selenium test execution. Selenium handles the UI part, JDBC handles the database validation.

Follow AutomateQA

Related Topics