We have seen how we can work with Selenium Library. The detailed installation/importing of Selenium Library is discussed in chapter “Working with Browsers using Selenium Library”.
In this chapter, we will discuss database library and how to connect and test database using Robot Framework.
Go to Robot framework site https://robotframework.org/ and click Libraries as shown below −
Upon clicking Libraries, you will be redirected to a screen as shown below −
The Libraries are categorized as Standard, External and Other.
We will now take a look at the external library in this chapter. Upon clicking External, the following screen appears −
It shows the list of external libraries supported by Robot Framework. Here, we will focus more on the Database Library (Python). The same has been highlighted in the screenshot above.
Upon clicking the Database Library (Python), you will be redirected to the screen where the instruction for installation are listed as shown in the following screenshot −
We can install the database library using pip and the command is −
pip install -U robotframework-databaselibrary
Run the above command in the command line as shown below −
The Library is stored in python lib folder as shown below −
Once the installation is done, the next step is to import the library inside the project and use it with test cases.
Open ride using ride.py from command line and create the project for testing database.
Click New Project and give a name to the project.
Click OK to save the project.
Click Library below Add Import.
Enter the Name of the Library as DatabaseLibrary as shown below and click OK.
Once saved, the library is as shown below −
We are going to work with MySQL Database. To work with MySQL, we need to install the module.
pip install pymysql
Now create test case under the project created.
Click New Test Case −
Enter the name of the test case and click OK.
We are going to use the existing database called customers available with us.
We will use phymyadmin to show the customer database −
We have a table called customer, which has data distributed in 6 rows. Now will write test-case which will connect to MySQL database customers and fetch the data from customer table.
Before we start, we will create scalar variables which will hold the data for dbname, dbuser, dbpasswd, dbhost, dbport and queryresult to store data, etc. Here are the variables created with values −
The command to connect to database is −
Connect To Database pymysql ${dbname} ${dbuser} ${dbpasswd} ${dbhost} ${dbport}
We will add some more test cases as shown below −
Here are the details −
*** Settings *** Library DatabaseLibrary *** Variables *** ${dbname} customers ${dbuser} root ${dbpasswd} admin ${dbhost} localhost ${dbport} 3306 @{queryResults} *** Test Cases *** TC1 Connect To Database pymysql ${dbname} ${dbuser} ${dbpasswd} ${dbhost} ${dbport} Table Must Exist customer Check If Exists In Database SELECT * FROM customer @{queryResults} Query SELECT * FROM customer Log @{queryResults}[0]
We have connected to the database, checked if table customer exists in database, got the query executed and logged the details of the query.
We will execute the test case and see the output
The results from the table are shown for the queryResults.
We have seen how to import database library, and the installation of it. We now know how to connect to MySQL database in Robot Framework and test the tables.