Sunday 28 July 2013

VB / QTP scripts with examples

QTP scripts with examples, VB scripts with examples



VBScript Program to reverse a string without using strreverse
'Program to reverse a string without using strreverse

MyStr = inputbox("Enter the String:")
nCnt = Len(MyStr)

For i = 1 to nCnt

   RevStr = Mid(MyStr,i,1)&RevStr

Next

Msgbox RevStr

QTP code to terminate all instances of Internet  explorer using collections

'Code to close all Internet explorer browser process using QTP

Dim oWMIService, allIExplorer,iEItem
Dim strComputer

strcomputer="."

'Get the WMI object
Set oWMIService=GetObject("Winmgmts:\\"& strcomputer & "\root\cimv2")

'Get collection of processes for with name iexplore.exe
set allIExplorer=oWMIService.execquery("Select * from win32_process where name='iexplore.exe'")
name
'Loop through each process and terminate it
For each iEItem in allIExplorer
iEItem.terminate
Next

Set allIExplorer= Nothing
Set oWMIService= Nothing

QTP script to send an email from outlook

'Call to the function for sending the mail on mail id 'abc@abc.com'
Call SendMail("abc@abc.com","Hi","Hi","")

' THis function will create an email and send it using outlook

Function SendMail(SendTo, Subject, Body, Attachment)

'CreateObject method to create an Outlook Application object
Set ol=CreateObject("Outlook.Application")

'Create a new outlook mail object
Set Mail=ol.CreateItem(0)

'Add the email address to the recipient list of the message
Mail.to=SendTo

'Add the subject of the message
Mail.Subject=Subject

'Add the body of the mail message
Mail.Body=Body

'Add the attachment to the mail message
If (Attachment <> "") Then
Mail.Attachments.Add(Attachment)
End If

'Send the mail message
Mail.Send

'Free up the memory
ol.Quit
Set Mail = Nothing
Set ol = Nothing

End Function

public void ConnectToDatabase()
{

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3

Set objConn=CreateObject("ADODB.Connection")
Set objRecordset=CreateObject("ADODB.Recordset")

objConn.Open "DRIVER={Oracle in OraHome92};SERVER={Servername};UID={Username};PWD={Password};DBQ={Dbnmae}"
objRecordset.CursorLocation = adUseClient
objRecordset.Open "Select {Col name} from tablename",objConn,adOpenStatic,adLockOptimistic

While not (objRecordset.EOF)

   objRecordset.MoveNext
   Msgbox "Result" & objRecordset.Fields.Item("{Col name}")& vbCrLf 

Wend

objRecordset.Close
objConn.Close

Set objRecordset=Nothing
Set objConn=Nothing

}

GetTOproperties, SetTOproperty, GetTOproperty & GetROproperty - methods explained with sample scripts.

Before we get started, I would like to take some time explaining about the TO, RO, Get and Set part of the methods.
TO : Stands for Test Object (The object that we have recorded in our object repository)
RO : Stands for Runt-time Object (The application object we are testing against the object in the Object repository)
Get : Is used to get (return) the current property/ies of a Run-time or a test object.
Set : Is used to set (return) the property of a test object.
GetTOproperties:
This method is used to return get the properties and their corresponding values from the recorded object in our object repository.
The following example explains the usage of GetTOproperties method:
'################################
'### Get TO Properties Script ###
'################################

systemutil.Run "iexplore.exe", "http://www.google.com"

Set oWebEdit=Browser("Google").Page("Google").WebEdit("q") 'Create webedit object

Set TOProperties=oWebEdit.GetTOProperties 'Get TO Properties

For i = 0 to TOProperties.count - 1 'Looping through all the properties
    sName=TOProperties(i).Name ' Name of the property
    sValue=TOProperties(i).Value 'Value of the property
    isRegularExpression=TOProperties(i).RegularExpression 'Is it a Regular expression
    Msgbox sName & " -> " & sValue & "->" & isRegularExpression 
Next

'##############
'### Result:###
'##############
' type -> ext -> true
' name -> q -> true
'### End of Script ####

SetTOproperty :
This method is used to set the value of a particular property of an object in the object repository.
GetTOproperty:
This method is used to get the current value of a property of an object in the object repository. If we had used the SetTOproperty method to change the existing property of an object in the OR, GetTOproperty method would only retrieve the current value of the property (ie, what ever value was set using SetTOproperty).
Note : Whatever value we set during the script execution is not retained after the execution is complete. The values in the OR automatically gets back to what it was at the beginning of the execution just like the run-time data tables.
'#############################################
'### Set TO Property & Get TO property ###
'#############################################

oldName = oWebEdit.GetTOProperty("name")
msgbox "Old Name ->" & oldName
oWebEdit.SetTOProperty "name","New Value"
NewName = oWebEdit.GetTOProperty("name")
msgbox "New Name ->" & NewName

'##############
'### Result:###
'##############
' Old Name ->q
' New Name ->New Value
'### End of Script ###

GetROProperty:
This method is used to get the runtime value of a property in an application object.
Note : SetROproperty method is not supported by QTP, hence it is unavailable.
'#######################
'### Get RO Property ###
'#######################

oWebEdit=Browser("Google").Page("Google").WebEdit("q").GetROProperty("value") 
msgbox oWebEdit

'###############
'### Result: ###
'###############

'### The text in the webedit control is displayed
'### End of Script ###

No comments:

Post a Comment