Skip to main content

Development

Use of VBScript with Selenium to connect to database

While most of the Automation scripts manipulate just the UI of an application, by making our scripts communicate with database we can accomplish more complicated tasks. Here is an example:-

Suppose you have to automate an application which works on online voting system where in once the vote is casted using the application, it goes to external system and verification process initiates to check whether the casted vote is correct or not. If the vote is correct then it will be counted else the count remains the same.

The following are the statuses of CASTED_VOTE column in VOTE_STATUS table configured in database:-

VOTE_STATUS

VOTE_STATUS

 

So usually our Automation script would be as follows:-

–       Record the steps to cast the vote

–       Wait till we receive the verification response from external entity.(Unknown time)

–       Go to the UI screen where total no. of votes are displayed and verify the count.

So the loophole in the above example would the unknown time (i.e. till what time script needs to wait before it executes the next command). This will create problem during execution when there are inputs provided in bulk. But if I can make my script communicate with the database, I can be sure of the unknown time as the time until I see the CASTED_VOTE_STATUS column equals 3 (i.e. Verification done).

So this will make our Automation script more realistic as shown below:-

–       Record and play the steps to cast the vote

–       Wait till we see CASTED_VOTE column status = 3

–       Go to the UI screen where total no. of votes are displayed and verify the count.

 

So let’s look at the task that needs to be performed:-

Objective: – Create an Automation script that communicates with database to provide more realistic Automation approach.

Resources: – Selenium, Java, VBScript

Solution: – I will be writing a VBScript code which will be invoked via Selenium to connect to DB.

To connect to DB using VBScript, I would require a connection string wherein I specify the machine details where my database is located. The connection string will vary according to the database server we are using.

The different types of Connection Strings can be found under the below link:-

http://www.connectionstrings.com

 

Below is the sample Connection.vbs file:-

======================================================

Dim connection

Dim RecordSet

Set connection=CreateObject(“adodb.connection”)

connection.ConnectionString=Driver={MySQL ODBC 5.1 Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;

connection.Open

Set RecordSet=CreateObject(“adodb.recordset”)

RecordSet.Open “<SQL Query to fetch or insert records from DB>”

<Operations to be performed>

Set RecordSet=nothing

Set connection=nothing

========================================================

So the entire Selenium script looks like as follows:-

/* Packages to import */

Public class DemoScript

{

// code to open the application tos caste the vote

// below is the code to invoke VBscript from Selenium script

Runtime.getRuntime().exec(“wscript <path of Connection.vbs file>”);

} // End of Java code

 

Thoughts on “Use of VBScript with Selenium to connect to database”

  1. Good stuff and and very well write up. I would appreciate if you can share any study resources on Selenium.

    Thanks for sharing.
    Cheers.

  2. Hey, But how do you use the output of VB script in selenium. Any thoughts on this please share.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sohit Kanwar

More from this Author

Categories
Follow Us