What is database

The database is nothing but the stack of details, placed in an organized manner. It is like School Attendance Book; we can get the student and details about the student like that database stores different data with different data types.

We can filter these data using a Language called SQL, SQL (Structured Query language) helps retrieve, insert, update, and delete the data from the database. We can store large data in the database and retrieves in the required data in seconds.

Why do we need database automation ?

  • Test Data - We can store the data in a database instead of excel or properties, JSON/XML files, retrieval would be faster in case of the database if we are trying to get a large set of data.
  • Front and Back end connectivity - Sometimes we have to verify the details we submitted/deleted/ updated in the front end is reaching the back end for future usage purpose. There are scenarios where details may fail during the parsing, in such cases details will not reach the database
  • Faster Testing - If we are testing simple database tasks manually it may take a lot of time and effort, so to avoid that we can go for automation testing of database

We cannot perform database testing using selenium python, but we can use language (java/python...) APIs to automate the database in selenium.

Selenium does not provide any utility to do database testing; You will have to utilize features available in a python programming language.

For example, if you are using python, you need to add appropriate .bindings file & write your own method to read/write/manipulate data in database tables.

For database programming, the Python DB API is a widely used module that provides a database application programming interface.

API testing in Selenium Python

Connecting to the database with selenium python

Before we start using our database with python, we must connect to it. There are four steps of database communication using python:

  • Create a connection object to the DB using credentials.
  • Create a cursor object to read/write the tables.
  • Interact with the database tables.
  • Close the connection.
# importing the module
import MySQLdb

# cerate a database connection
db = MySQLdb.connect("localhost","userName","Password","DB_Name")

# define a cursor object
cursor = conn.cursor

# drop table if exists
Cursor.execute("if STUDENT table exists drop it")

# query
sql = "create table STUDENT (NAME CHAR(30) not null,
		CLASS char(5),
		AGE int,
		GENDER char(8),
		MARKS int"

# execute query
cursor.execute(sql)

# close object
cursor.close()

# close connection
conn.close()

You can use a connection object for performing operations like commit(), rollback() and close() as shown below:

Database Operations Description
cur = conn.cursor() creates new cursor object for executing SQL statements
conn.commit() Roll back the transactions
conn.rollback() Roll back the transactions
conn.close() closes the connection, After interacting with the database you need to close the database connection to give up resources.
conn.callproc(proc,param) call stored procedure for execution
conn.getsource(proc) fetches stored procedure code

CSV file reading in Selenium

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions