What is database

Data base is nothing but the stack of details, placed in organized manner. It is like School Attendance Book, we can get the studen and details about the student like that database stores different data with different datatypes.

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 database and retrievs in the required data in seconds.

Why do we need database automation ?

  • Test Data - We can store the data in database instead of excel or properties, JSON/xml files, retrieval woud be faster incase of database if we are trying to get large set of data.
  • Front and Back end connectivity - Sometimes we have to verify the details we submitted / deleted / updated in 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 database
  • Faster Testing - If we are testing simple database tasks manually it may take 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 feature available in python programming language.

For example, if you are using python, you need to add appropriate .bindings file & write 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.

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 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

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions