How to perform an Insert operation in Leapwork using a Database Block?

How to perform an Insert operation in Leapwork using a Database Block?

Introduction

There are various scenarios where a user wants to connect to any database (using DSN) in order to validate the data getting stored in the database. There are also scenarios where a user needs to insert data into the database through automation. In this article, we will be talking about how we can achieve this scenario by executing different database commands using a Leapwork Database block.

How to perform an Insert operation in Leapwork using a Database Block?

In this scenario, we will learn how to perform an INSERT operation in Leapwork using a Database Block.

To start, we need to first check some of the prerequisites. For example - setting up ODBC drivers.

Please refer to this article for more information - Access SQL-Database via ODBC driver in Leapwork

Please note that we will be using SQL Server Database to execute the commands in this article.

The two input fields viz. Connection & Query are mandatory fields and should be populated as below:

Connection:

The "Data Source Name" of configured ODBC connection on the computer where the test case will run.

Query:

The query statement will be executed on the excel file. It has a special syntax which is an ODBC-compliant SQL statement. The dynamic fields can be specified and used as Tokens while creating query statements.

Note: - We can execute the below query (either through Leapwork or directly in the SQL Server Query Manager) to create a new table in the Database

create table LW_USER_DETAILS([ID][int],[FirstName]varchar,[LastName]varchar,[Email]varchar,[Address]varchar,[Department]varchar)

Once the user provides both the above details correctly and clicks on the "Refresh" button on the Database blocks, it loads up the respective columns present in the table.

Please refer to the below image:

In case, any of the 2 provided input fields, i.e. Connection or Query is incorrect, Leapwork throws an error after clicking on the "Refresh" button.

Please refer to the image below:

So, once the connection is successfully connected, it is time to say that the user is up and ready to execute the INSERT query through Leapwork.

Let's see how that can be achieved below:

As per the above screenshot, the method used is First row which will insert the values in the database. The other 2 options like Iterate and Row Index can also be used as per the scenarios which will read/insert the values from/to and any specific row index provided respectively.

In the above screenshot, the Log message block is fetching dynamic values from the Database block.

The dynamic fields have been specified and used as Tokens in the Log message block while fetching dynamic contents from the Database block.

If the query is not provided correctly, then the Error connector will be triggered.

Quick Start

  1. Download the attached INSERT.zip file.
  2. Import the attached use case, INSERT.zip, in the Leapwork Studio.

Please refer to How to Export and Import automation flows. 3. Open the use case with the title, "Insert Operation".

Configuration and Use

After the flow has been imported, you can move the flow to any specific folder and then execute the same to see the behavior.

Please refer to the following article for more: Organizing Daily Work in Leapwork.

Attachments

INSERT.zip

If you have any questions, please reach out to our Priority Support team.