Working With External Database libraries



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 −

Database Using Robot Framework

Upon clicking Libraries, you will be redirected to a screen as shown below −

Redirected Screen

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 −

Libraries categorized

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 −

Database Library

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 −

Command line Library

The Library is stored in python lib folder as shown below −

Python Library

Once the installation is done, the next step is to import the library inside the project and use it with test cases.

Import Database Library

Open ride using ride.py from command line and create the project for testing database.

testing database

Click New Project and give a name to the project.

Name database

Click OK to save the project.

Click Library below Add Import.

 Add Import database

 Import database

Enter the Name of the Library as DatabaseLibrary as shown below and click OK.

Database Library Name

Once saved, the library is as shown below −

Saved Library Name

We are going to work with MySQL Database. To work with MySQL, we need to install the module.

Command

pip install pymysql
install pymysql

Now create test case under the project created.

install pymysql

Click New Test Case −

Test Case pymysql

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 −

phymyadmin

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 −

queryresult

The command to connect to database is −

Connect To Database pymysql ${dbname} ${dbuser} 
${dbpasswd} ${dbhost} ${dbport}
Connect To Database

We will add some more test cases as shown below −

more test cases

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

See The Output

The results from the table are shown for the queryResults.

Log Details

Query Results

Details of TC1

Details of TC1

Conclusion

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.

Advertisements