Skip to main content

Oracle DB Testing with Selenium



1. For Database Verification in To use Selenium Webdriver, we need to use the JDBC ("Java Database Connectivity"). Its API provides following classes and interfaces:


  • Driver Manager
  • Connection
  • Statement
  • ResultSet
  • SQLException


2. In order to test our Database using Selenium, we need to perform the following steps:

a. Make a connection to the Database. Syntax is:
Connection DB_Con= DriverManager.getConnection(URL, "USERID", "PASSWORD" )

And load JDBC Driver class using the syntax:
Class.forName("oracle.jdbc.driver.OracleDriver");

b. Execute Queries to the Database

Statement stmt = DB_Con.createStatement();

stmt.executeQuery(select *  from employee;);

ResultSet rs= stmt.executeQuery(query);

c. Process the result set based on your need:

while (rs.next()){

String FisrtName= rs.getString(1);         
String LastName= rs.getString(2);
System.out.println("First Name is: " +FisrtName);
System.out.println("LastName is: " +LastName);
}

d. close DB connection

DB_Con.close();

Note* We should download an Oracle Jar from Oracle website like for Oracle11 (Classes12.jar) we can get it from http://www.oracle.com/technetwork/apps-tech/jdbc-10201-088211.html  and add this JAR in our project Build Path.


Sample code:

package com.qa.Util;

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

public class ConnectDB {

public static ResultSet getData(String query, String environment) {
String JDBC_DRIVER = null;
String DB_URL = null;
String PASS = null;
String USER = null;

if (environment.equalsIgnoreCase("QA")) {
JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
DB_URL = "jdbc:oracle:thin:@dbtest.site.com:1001:<TEST>";
PASS = "testpwd";
USER = testuser";

}

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {

Class.forName(JDBC_DRIVER);

conn = DriverManager.getConnection(DB_URL, USER, PASS);

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}





package com.qa.Util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;


public class FetchDBData {


String querytoFetch = "select rcv_ser_no from testDB.activation where BOX_NUMBER like 'DEF%' and (to_char(contract_eff_dt) >= to_date('01-MAR-18') and to_char(contract_eff_dt) <= to_date('01-MAY-18')) and rownum=1";

public static String FREE_BOX;

public static String GetTSN() throws SQLException {
ResultSet rs = ConnectDB.getData(querytoFetch, "QA");
while (rs.next()) {
FREE_BOX= rs.getString("box_no");
}

return FREE_BOX;
}

}

Comments

Popular posts from this blog

ARIA Snapshot in Playwright

  What is an ARIA Snapshot in Playwright? An  ARIA snapshot  in Playwright is a structured representation of a page’s  accessibility tree , which is used by assistive technologies (e.g., screen readers) to interpret the content of a web page. This snapshot helps verify if elements have the correct  roles, names, and properties  required for accessibility. Playwright provides the page.accessibility.snapshot() API to capture this accessibility tree at any given moment during test execution. How Does ARIA Work? ARIA ( Accessible Rich Internet Applications ) is a set of attributes that help improve accessibility by defining roles, states, and properties for elements that are not natively accessible. Example: In this case, the aria-label ensures that screen readers identify the button as “Submit Form.” How to Use ARIA Snapshots in Playwright? Playwright’s  accessibility.snapshot()   method retrieves the  accessible structure  of the page. Ex...

Bruno vs Postman: Which API Client Should You Choose?

  As API testing becomes more central to modern software development, the tools we use to test, automate, and debug APIs can make a big difference. For years, Postman has been the go-to API client for developers and testers alike. But now, Bruno , a relatively new open-source API client, is making waves in the community. Let’s break down how Bruno compares to Postman and why you might consider switching or using both depending on your use case. ✨ What is Bruno? Bruno is an open-source, Git-friendly API client built for developers and testers who prefer simplicity, speed, and local-first development. It stores your API collections as plain text in your repo, making it easy to version, review, and collaborate on API definitions. 🌟 What is Postman? Postman is a full-fledged API platform that offers everything from API testing, documentation, and automation to mock servers and monitoring. It comes with a polished UI, robust integration, and support for collaborati...

🔧 Self-Healing Selenium Automation with Java — A Smarter Way to Handle Broken Locators

  How to build smarter, more resilient automated tests? We’ve all been there — our Selenium test cases start failing because of minor UI changes like updated element IDs, renamed classes, or even reordered elements. It’s frustrating, time-consuming, and often the most dreaded part of maintaining automated tests. But what if your automation could heal itself? 💡 What is Self-Healing Automation? Self-healing automation  refers to the capability of a test automation framework to recover from minor UI changes by automatically trying alternative locators when the primary one fails. It’s like giving your test scripts a survival instinct. 🔨 🛠️ Implementation in Java + Selenium: Step by Step Step 1: Create a Self-Healing Wrapper We start by creating a custom class called SelfHealingDriver. This class wraps the standard WebDriver and handles locator failures gracefully. public   class   SelfHealingDriver { private   WebDriver driver ; public   SelfHealingDri...