Sunday, June 29, 2014

How to run PowerShell script using QTP

There are two ways to run PowerShell script from QTP as shown below,
  • ·         Exec command
  • ·         Run Command
The above two methods use WshShell Object to launch other programs.

Run Method:

Run method runs a program in a new process.
'Creates WshShell object and returns it to objShell
Set  objShell=CreateObject("Wscript.shell")

'Runs the PowerShell script
objShell. Run ("powershell -noexit -file c:\fso\email.ps1")


The Run method returns an integer. The Run method starts a program running in a new Windows process. You can have your script wait for the program to finish execution before continuing. This allows you to run scripts and programs synchronously.

Exec Method:

Exec method runs the application or program in child command line.

Syntax:

Object. Exec (strCommand)
Object is WshShell object.

strCommand-string value indicating the command line used to run the application.

Friday, June 27, 2014

QTP Functions- Part 1

EXIT Statements
The following are the exit statements used in different scenarios,

  1. ExitTest
  2. ExitTestIteration
  3. ExitAction
  4. ExitActionIteration
  5. ExitComponent
  6. ExitComponentEx
  7. ExitComponentIteration
ExitTest:

Exits the entire UFT test or ALM business process test, regardless of the run-time iteration settings.

Syntax:
ExitTest[(Return Value)]

Argument:  Return Value
Type: Variant
Description: Optional. The Return Value.

Example:

Result = Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("username").Check (CheckPoint("username") )
If Result = False Then
ExitTest
End If
The above test exits if the checkpoint on the username edit box fails.

ExitTestIteration:

Exits the current iteration of the UFT test or ALM business process test and proceeds to the next iteration, or exits the test run if there are no additional run-time parameter iterations.

Syntax
ExitTestIteration [(RetunValue)]

Argument : RetunValue
Type : Variant
Description : Optional. The return value.

Example:

Result = Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Check ( CheckPoint("userName") )
If Result = False Then
ExitTestIteration
End If

The above example uses the ExitTestIteration statement to end the test run if a checkpoint on the userName edit box fails

ExitAction:

Exits the current action, regardless of its local (action) iteration attributes. The ExitAction statement and its return value are displayed in the Run Results.

Syntax
ExitAction[(ReturnValue)]

Argument : ReturnValue
Type : Variant
Description: Optional. The action's return value. The value can be a string or number, but it must be a constant value. It cannot be the name of a variable. The value is returned to the statement that called the action.

Example:

' Action "CheckForm"
FormVersion = RunAction("GetFormVersion", oneIteration)
If FormVersion = 2 Then
Call RunAction("CheckOldForm", oneIteration)
Else
Call RunAction("CheckNewForm", oneIteration)
End If
' Action "GetFormVersion"
If IsNewForm Then
ExitAction(2)
Else
ExitAction(1)
End If

In the above example,the CheckForm action calls the GetFormVersion action. The GetFormVersion action checks whether the form is in the new version or the old version. If IsNewForm is True, then the action uses the ExitAction function to return the value 2. If IsNewForm is False, then the action uses the ExitAction function to return the value 1. The CheckForm action stores the value returned by the ExitAction statement in the FormVersion variable, and uses it in the following steps.

ExitActionIteration:

Exits the current iteration of the action. The ExitActionIteration statement and its return value are displayed in the Run Results. Using this statement overrides other end-of-iteration settings.

Syntax
ExitActionIteration[(ReturnValue)]

Argument : ReturnValue
Type : Variant
Description : Optional. The action iteration's return value. The return value is displayed in the run results.

Example

Name = DataTable("Name", dtLocalSheet)
If Name = "Ram" Then ExitActionIteration("Skipping Ram")

The above example uses the ExitActionIteration function to stop running the current action iteration when the value in the current row of the Name column in the action's data sheet is Ram.


Instr Function:

Instr-Returns the first occurrence of one string in another string.

Syntax:

Instr(start, string 1,string 2,compare)

strat: starting position for each search and its optional.
string1: string being searched and its mandatory argument
string2: string searched for in string 1 and its mandatory argument.
compare-it’s a numeric value indicating the type of comparison.

Compare argument can have the following values,

0-for binary comparison
1-for text comparison


Instr function returs the following values,

0 -if string1 is zerolenth
Null- if string1 is null
Start position-if string2 is zero length
Null-if string2 is Null
0-if string2 is not found
Postion at which string is found-if string2 is found within string1
0-if start>len(string2)

Example:

Dim string1, string2, Result
string1="Quick Test Professional"
string2="Test"
Result=instr(1, string1,string2)

msgbox Result


Output:




Wednesday, June 25, 2014

VB Script Sample Programs-Part 1

STRING REVERSE

Dim Str
Str="Quick Test Professional"
Rstr=""

 For i = Len(Str) To 1 Step -1
     Rstr=Rstr&Mid(Str,i,1)
 Next
 msgbox Rstr

STRING PALINDROME

Dim Str
Str="MadaM"
Rstr=""

 For i = Len(Str) To 1 Step -1
     Rstr=Rstr&Mid(Str,i,1)
 Next
 msgbox Rstr
 If Str=Rstr Then
     msgbox "Palindrome"
     Else
     msgbox "Not Palindrome"
 End If
 STRING SWAPPING
 Str1="Quick"
 Str2="Test"
 Msgbox "Str1::"&Str1
 Msgbox "Str2::"&Str2
 l1=Len(Str1)
 l2=Len(Str2)
 Str1=Str1&Str2
 Str2=Mid(Str1,1,l1)
 Str1=Mid(Str1,l1+1,l2) 
 Msgbox "Str1::"&Str1
 Msgbox "Str2::"&Str2


PRIME NUMBER LISTING 
 'Prime Number Listing
l=15
For i = 1 To l Step 1
count=0
For j = 2 To l Step 1
    If i mod j=0 Then
        count=count+1
    End If
Next
If count =1 Then
    msgbox i&" is Prime Number"

Else
msgbox i&" is not Prime Number"
End If
Next

Tuesday, June 24, 2014

Database Connection Using QTP


We can connect to Database using QTP in Two Ways.
·         DSN approach
·         DSN less approach

DSN approach:

What is DSN?

DSN stands for Data Source Name.DSN allows you to connect to a database using ODBC driver. ODBC stands for Open Database connectivity which provides a standard interface that allows one application to access many different data sources.

DSN creation for Oracle database:

Step 1:

Go to your Control Panel\Administrative Tools\Data Sources (ODBC)


Step 2:

Go to User DSN and click on Add. The Create New Data Source dialog opens.


Step 3:

Select a driver for which you want to set up a data source and click finish button. The Oracle ODBC Driver Configuration window opens.


Step 4:

Specify the following information in the Oracle ODBC Driver Configuration window:

·         In the Data Source Name field, enter a name of your choice to identify the data source. If your DBA has supplied a common DSN for use across the enterprise, enter it here.
·         In the Description field, enter an optional description for the data source.
·         In the TNS Service Name drop-down, select the TNS Service Name for the database your workspace repositories will be stored in. If no choices are shown, or if you are unsure which name to select, contact your DBA.
·         In the User ID field, enter the database user ID supplied by your DBA

Step 5:

Click Test Connection. The Oracle ODBC Driver Connect window opens.


Step 6:

·         In the Oracle ODBC Driver Connect window the Service Name and User ID fields are prefilled with the information you supplied in the Oracle ODBC Driver Configuration window. Enter the password for your user ID and click OK. You are notified that the connection was created successfully. Click OK.

Note: If you receive an error, verify the driver configuration settings and retest the connection. See your DBA if the problem persists.

·         Click OK to exit the Driver Configuration window. Then click OK again to exit the ODBC Data Source Administrator window.

We are done with DSN creation with this step.

What is ADO?

ADO stands for ActiveX Data Object and allows you to access a database from inside QTP using VBScript.
Now use the following VB script code to connect to database using DSN,

Dim dbCon, dbRecordset, ConnectionString
Set dbCon = Createobject("ADODB.Connection")
Set dbRecordset = CreateObject("ADODB.Recordset")

'Please replace 'Name fo the DSN' with actual name of DSN which you craeted,
'ServerName with server ip address or server name and password with the value of the password to connect to database


ConnectionString = "DSN=Name fo the DSN;Server=ServerName;Pwd=Password"

'Open Connection
dbObj.Open ConnectionString


DSN less approach:

What is DSN-less approach?
Here you have to specify all the connection related info directly in the QuickTest pro.

Note:
We have to make sure that the machine has the correct drivers installed.

VB Script Code to Connect to different databases:

MS Access:

Dim dbCon, dbRecordset, ConnectionString 

Set dbCon = Createobject("ADODB.Connection")
Set dbRecordset = CreateObject("ADODB.Recordset")

ConnectionString ="Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"

con.open ConnectionString