Disclaimer: This post is related to IBM Rational Functional Tester With Java. It needs some slight modifications to work with other tools.

Sometimes, your automated test script needs to do some strange stuff. Like accessing the database, extracting some value and using this value in subsequent U.I steps in your script.

Suppose that you have hundreds of scripts that needs the following block to access the DB and read a certain value:

try{
Connection db_connection = DriverManager.getConnection("jdbc:odbc:DBNAME",
"username","password");


Statement stm2 = db_connection.createStatement();
stm2.executeQuery("select * from TableName");

}
catch( SQLException x ){

JOptionPane.showMessageDialog(null,
x.getLocalizedMessage().toString(),"Error",
JOptionPane.ERROR_MESSAGE);

}
What if the username and password is changed after a period of time for a certain reason. This means that you need to modify all your scripts and change the credentials. And this could be a pain and very time consuming.

A good practice is creating a class that will prompt the user to enter the credentials and validates them before running the script. This will save time and headache in subsequent releases.

The following is the code for the class with the proprietary information removed:


import javax.swing.JOptionPane;
import resources.SendValidUNandPWHelper;

import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Description : Functional Test Script
* @author Ashehadeh
*/
class SendValidUNandPW extends SendValidUNandPWHelper
{
/**
* Script Name : CheckDBDialog
* Generated : Feb 19, 2008 8:31:06 AM
* Description : Functional Test Script
* Original Host : WinNT Version 5.1 Build 2600 (S)
*
* @since 2008/02/19
* @author Ashehadeh
*/
/**
* @param args
*/





public String[] sendValidCredentials(){

String[] result=null;
String[] k =validateInput();

while (k==null){
k= validateInput();
}

return k;


}


//check if the provided credential can access the DB
public static String[] validateInput(){

String[] result=null;
String[] k =isDBonline();

if (k==null){
result =null;
System.exit(0);

}

else {


try{
Connection db_connection = DriverManager.getConnection("jdbc:odbc:DBNAME",k[0],k[1]);

Statement stm2 = db_connection.createStatement();
stm2.executeQuery("select * from TABLEName");
result =k;

}
catch( SQLException x ){

JOptionPane.showMessageDialog(null,
"Please Enter correct Uasername/Password!","Error",
JOptionPane.ERROR_MESSAGE);
/* JOptionPane.showMessageDialog(null,
x.getLocalizedMessage().toString(),"Error",
JOptionPane.ERROR_MESSAGE);*/

result=null;

}

}
return result;
}


//check if the db is online from the first place

public static String[] isDBonline(){


String[] result=null;
String[] k =isOneEmpty();


if (k==null){
result =null;
System.exit(0);


}


//now check for DB connectivity
else {

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

result =k;

}



catch(ClassNotFoundException c){
JOptionPane.showMessageDialog(null,
"DB Unavailable!","Error",
JOptionPane.ERROR_MESSAGE);
result=null;
System.exit(0);

}

}
return result;
}


//is one of the fields empty? if so, exit
public static String[] isOneEmpty(){

String[] k=showDialog();
String[] result;

// if one of the username or passwords are empty, exit.
if (k[0]==null || k[1].equals("") ||
k[1].equals("") || k[1]==null){

JOptionPane.showMessageDialog(null,
"Error. Fill the two fields. Click ok to exit!","Error",
JOptionPane.ERROR_MESSAGE);

result=null;
System.exit(0);

}


//if both of them are not empty continue
else{
result=k;

}

return result;

}

//prompt the user for username and password
public static String[] showDialog() {

String DBUsername=JOptionPane.showInputDialog(null,
"Enter DB username:", "Input", JOptionPane.QUESTION_MESSAGE);
String DBpassword=JOptionPane.showInputDialog(null,
"Enter DB password:", "Input", JOptionPane.QUESTION_MESSAGE);
String[] UPArray={DBUsername,DBpassword};
return UPArray;

}



}

6 Comments:

  1. David Burns said...
    This is probably a stupid question but what are you trying to test or show?

    I think that you have put some helpful code on getting people to get round the issue of expired passwords.

    Sorry if this has come across harsh.

    David
    adel shehadeh said...
    Yes you are correct. This class does not test anything. It may help you in your test if the DB password expires instead of having it hard coded in the script.

    Am i being inarticulate?

    Thanks :)
    David Burns said...
    I had assumed by the title that you were going to show how to enhance with RFT with UI tests. We all know what assumptions lead to! :)

    My next question and this because I don't know much about Java but why not use trusted connection strings with JDBC like you would with c#? Would that get round the whole database login running out? Sorry if that's a stupid question.

    P.S. Thanks for your comment on my blog!
    adel shehadeh said...
    This is not stupid at all?

    I will look into it and get back to you :)


    I am new to java by the way.
    adel shehadeh said...
    I am using a special JDBC driver that interfaces RFT to a DB2 database. Actually i dug the web for something similar to what you are talking about for java. I also consulted the both the dot net and the Java team in my workplace. The dot net guys said it's applicable, but the java team said that they are not sure if such thing exists.

    Please tell me if you find anything :)

    keep posting new ideas here :)
    Anonymous said...
    I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

    Sorry for offtopic

Post a Comment