I am Anand Pasunoori,
you can reach me here for any info about this blog or queries on QTP /Testing anand853@gmail.com
facebook
twitter
Software Jobs
Labels: About Me
1) Open Internet Explorer and navigate to yahoo mail
Dim ie
Set ie=CreateObject("InternetExplorer.Application")
ie.Visible=True
ie.Navigate "www.yahoomail.com"
x=Browser("CreationTime:=0").GetROProperty("title")
msgbox x
Labels: IE Object
1) Create a word document and write some data
dim mw
set mw=CreateObject("Word.Application")
mw.Documents.Add
mw.selection.typetext "hello"
mw.ActiveDocument.SaveAs "e:\QTPInside.doc"
mw.quit
set mw=nothing
2) Create Script to get the list of links in Google and do spell check
Dim d
set mw=CreateObject("Word.Application")
set d=Description.Create
d("micclass").value="Link"
set a=Browser("Google").page("Google").childobjects(d)
for i=0 to a.count-1
mw.WordBasic.filenew
s=a(i).getROProperty("innertext")
mw.WordBasic.insert s
if mw.ActiveDocument.Spellingerrors.count>0 then
Reporter.ReportEvent 1,"Spelling","spelling error :"&s
end if
mw.ActiveDocument.Close(False)
next
mw.quit
set mw=nothing
3) Script to display all the doc files in all the drives in the system
Dim mw
Set mw=CreateObject("Word.Application")
Set fs=createobject("Scripting.FileSystemObject")
Set d=fs.Drives
mw.FileSearch.FileName="*.doc"
For each dr in d
msgbox dr
mw.FileSearch.LookIn=dr
mw.FileSearch.SearchSubFolders=True
mw.FileSearch.Execute
For each i in mw.FileSearch.FoundFiles
print i
Set f=fs.GetFile(i)
print f.Name&" "&f.Size&" "&f.DateCreated
print "-------------------------------------------------------------------"
Next
Next
mw.Quit
Labels: Word Object
Overview:
Generally for every object 20-25 properties information available, qtp recognizes object using 2 0r 3 important properties.
Qtp has default object identification configuration for every environment, if we feel that configuration is not sufficient for recognizing objects in our application, we can configure some more
Object Identification Types
a) Normal identification
1) Mandatory properties
2) Assistive properties
b) Smart identification
1) base filter properties
2) optional filter properties
c) Ordinal identifier
1) location
2) index
3) creation time(only for web)
QTP learns information in the following in case of normal identification:
First of all the qtp learns all the mandatory properties at a time and thinks whether these properties sufficient to identify the object uniquely. if it feels sufficient then it stops learning otherwise,
It learns first assistive property and once again stops and thinks, like this qtp learns one by one. At the end of assistive properties list also if it feels not satisfied and it finally goes to Ordinal Identifier.
QTP learns information in the following in case of Smart Identification:
Smart identification is an optional feature, if we feel normal identification is not sufficient for any object, and then we configure Smart Identification for that object, in order to avoid Ordinal Identifier.
After normal identification if qtp feels not satisfied then it goes to smart identification. in smart identification 2 types of properties available, first qtp learns all base filter properties at a time and thinks whether these properties are sufficient for identifying the object uniquely. If it feels sufficient, then it stops learning otherwise it goes Optional Filter Properties and learns one by one. Still it feels not satisfied finally it goes to Ordinal Identifier.
Ordinal identifiers:
There are 3 types of ordinal identifiers available
1) Location: is based on object location in the AUT ,location starts from zero.
2) index: it is based on sequence of the programs, index starts from zero
3) Creation time: it is based on loading time of the web objects. qtp generates 0,1,2 like numbers.
Tool Settings Globalization:
As QTP is a I-tier(Stand-alone) application,making Tool settings globally is not possible.
For making tool settings global, QTP is providing a special feature called "Generate Script".
STEPS:
1) Settings available in 3 areas.
a) File->Settings
b) Tools->Options
c) Tools->Object Identification
2) Perform required settings and generate Scripts
3) Share Script files to team members and ask them to execute those scripts.
NOTE: After executing these scripts all team members can get same settings.
ADO (Active Data Objects), this technology allows users to access data easily from many existing databases (such as Access or Paradox) or from ODBC compliant databases like Oracle or MS SQL Server. Using ADO is quite simple and allows programmers to provide flexible database front ends to users that are reliable and include many features.
Following are some of the key objects found in the ADO object model and some of their key methods and properties.
Connection Object
This object represents an open connection to the data source. This connection can be a local connection (say App.Path) or can be across a network in a client server application. Some of the methods and properties of this object are not available depending on the type of data source connected to.
Command Object
A command object specifies a specific method we intend to execute on or against the data source accessed by an open connection.
Record Set Object
The Record Set object represents a complete set of records from an executed command or from an underlying base table in the database. A key thing to note is that a Record Set object references only one record at a time as the current record.
Examples
1) Get Test Data from a Database and use in Data Driven Testing (through Scripting)
1) Dim con,rs
2) Set con=createobject("Adodb.connection")
3) Set rs=createobject("Adodb.recordset")
4) con.provider=("microsoft.jet.oledb.4.0")
5) con.open "C:\Documents and Settings\Administrator\My Documents\QTPInside.mdb"
6) rs.open "Select * From Login",con
7) While rs.eof <>True
8) SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
9) Dialog("Login").Activate
10) Dialog("Login").WinEdit("Agent Name:").Set rs.fields ("Agent")
11) Dialog("Login").WinEdit("Password:").Set rs.fields ("Password")
12) Dialog("Login").WinButton("OK").Click
13) Window("Flight Reservation").Close
14) rs.movenext
15) Wend
2) Exporting Data from a Database to an Excel Sheet
1) Dim con,rs
2) Set con=createobject("adodb.connection")
3) Set rs=createobject("adodb.recordset")
4) con.provider="microsoft.jet.oledb.4.0"
5) con.open"C:\Documents and Settings\admin\My Documents\QTPInside.mdb"
6) rs.open"select*from Login",con
7) Set ex=createobject("Excel.Application")
8) Set a=ex.workbooks.open("C:\Documents and Settings\admin\My Documents\QTPInside.xls")
9) Set b=a.worksheets("sheet1")
10) i=1
11) Do While Not rs.EOF
12) b.cells (i,1).value=rs.fields("agent")
13) b.cells(i,2).value=rs.fields("password")
14) rs.movenext
15) i=i+1
16) Loop
17) a.save
18) a.close
3) Exporting Data from a Database to a Text file
Dim objCon,objRs,ObjFso,myFile,myData,rc,r
Set objCon=createobject("Adodb.connection")
Set objRs=createobject("Adodb.Recordset")
set objFso=createobject("Scripting.Filesystemobject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr\My Documents\anand.txt",8)
objcon.provider=("Microsoft.jet.oledb.4.0")
objcon.open"C:\Documents and Settings\gcr\My Documents\QTPInside.mdb"
objrs.open "select * from login",objCon
r=1
Do until objRs.EOF
a=objRs.Fields ("Agent")
b=objRs.Fields ("Pwd")
myFile.Writeline a &","& b
r=r+1
objRs.MoveNext
Loop
myFile.Close
objCon.Close
4) Connecting to a SQL Sever database
Const adOpenStatic = 3
Const adLockOptimistic = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open _
"Provider=SQLOLEDB;Data Source=atl-sql-01;" & _
"Trusted_Connection=Yes;Initial Catalog=Northwind;" & _
"User ID=fabrikam\kenmyer;Password=34DE6t4G!;"
objRecordSet.Open "SELECT * FROM Customers", _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet.MoveFirst
Wscript.Echo objRecordSet.RecordCount
5) Open a Database Using a DSN
Const adOpenStatic = 3
Const adLockOptimistic = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open _
"Northwind;fabrikam\kenmyer;34ghfn&!j"
objRecordSet.Open "SELECT * FROM Customers", _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet.MoveFirst
Wscript.Echo objRecordSet.RecordCount
6) Open Two Record sets
Const adOpenStatic = 3
Const adLockOptimistic = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Set objRecordSet2 = CreateObject("ADODB.Recordset")
objConnection.Open _
"Provider= Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=inventory.mdb"
objRecordSet.Open "SELECT * FROM GeneralProperties Where ComputerName = 'Computer1'", _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet.MoveFirst
objRecordSet2.Open "SELECT * FROM Storage Where ComputerName = 'Computer1'", _
objConnection, adOpenStatic, adLockOptimistic
objRecordSet2.MoveFirst
Do Until objRecordset.EOF
Wscript.Echo objRecordset.Fields.Item("ComputerName")
Wscript.Echo objRecordset.Fields.Item("OSName")
objRecordSet.MoveNext
Loop
Do Until objRecordset2.EOF
Wscript.Echo objRecordset2.Fields.Item("DriveName"), _
objRecordset2.Fields.Item("DriveDescription")
objRecordSet2.MoveNext
Loop
objRecordSet.Close
objRecordSet2.Close
objConnection.Close
7) Searching a Database Using String Criteria
Const adOpenStatic = 3
Const adLockOptimistic = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = eventlogs.mdb"
objRecordSet.Open "SELECT * FROM EventTable " & _
"WHERE Type = 'Error'", objConnection, adOpenStatic, _
adLockOptimistic
objRecordSet.MoveFirst
Wscript.Echo "Number of records: " & objRecordset.RecordCount
objRecordSet.Close
objConnection.Close
8) Insert Data into a database table using Database Command Object
Dim objCon,objCom
Set objCon=Createobject("ADODB.connection")
objCon.open"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\QTPInside.mdb;"
Set objCom=Createobject("ADODB.Command")
objCom.ActiveConnection=objCon
objCom.CommandText="insert into Emp values('QTPInside',88233,30000)"
objCom.Execute
objCon.Close
Set objCom=Nothing
Set objCon=Nothing
9) Insert multiple sets of Data (using Excel sheet) into a database table using Database Command Object
Dim objCon,objCom,strEmpName,intEmpNo,intEmpSal,intRowcount,i
Set objCon=Createobject("ADODB.connection")
objCon.open"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\QTPInside.mdb;"
Set objCom=Createobject("ADODB.Command")
objCom.ActiveConnection=objCon
Datatable.AddSheet("input")
Datatable.ImportSheet "C:\QTPInside.xls",1,"input"
intRowcount=Datatable.GetSheet("input").GetRowCount
Msgbox intRowcount
For i=1 to intRowcount step 1
DataTable.SetCurrentRow(i)
strEmpName= DataTable.Value(1,"input")
intEmpNo= DataTable.Value(2,"input")
intEmpSal= DataTable.Value(3,"input")
objCom.CommandText="insert into Emp values( '"&strEmpName&" ',"&intEmpNo&","&intEmpSal&")"
objCom.Execute
Next
objCon.Close
Set objCom=Nothing
Set objCon=Nothing
Labels: Database Operations
We have two types Errors in VB Script; they are VBScript Run-time Errors and VBScript Syntax Errors
13.1 VBScript Run-time Errors
VBScript run-time errors are errors that result when our VBScript script attempts to perform an action that the system cannot execute. VBScript run-time errors occur while our script is being executed; when variable expressions are being evaluated, and memory is being dynamic allocated.
13.2 VBScript Syntax Errors
VBScript syntax errors are errors that result when the structure of one of our VBScript statements violates one or more of the grammatical rules of the VBScript scripting language. VBScript syntax errors occur during the program compilation stage, before the program has begun to be executed.
Labels: Errors
21.1 Case-sensitivity:
By default, VBScript is not case sensitive and does not differentiate between upper-case and lower-case spelling of words, for example, in variables, object and method names, or constants.
For example, the two statements below are identical in VBScript:
Browser("Mercury").Page("Find a Flight:").WebList("toDay").Select "31"
browser("mercury").page("find a flight:").weblist("today").select "31"
21.2 Text strings:
When we enter a value as a text string, we must add quotation marks before and after the string. For example, in the above segment of script, the names of the Web site, Web page, and edit box are all text strings surrounded by quotation marks.
Note that the value 31 is also surrounded by quotation marks, because it is a text string that represents a number and not a numeric value.
In the following example, only the property name (first argument) is a text string and is in quotation marks. The second argument (the value of the property) is a variable and therefore does not have quotation marks. The third argument (specifying the timeout) is a numeric value, which also does not need quotation marks.
Browser("Mercury").Page("Find a Flight:").WaitProperty("items count", Total_Items, 2000)
21.3 Variables:
We can specify variables to store strings, integers, arrays and objects. Using variables helps to make our script more readable and flexible
21.4 Parentheses:
To achieve the desired result and to avoid errors, it is important that we use parentheses () correctly in our statements.
21.5 Indentation:
We can indent or outdent our script to reflect the logical structure and nesting of the statements.
21.6 Comments:
We can add comments to our statements using an apostrophe ('), either at the beginning of a separate line, or at the end of a statement. It is recommended that we add comments wherever possible, to make our scripts easier to understand and maintain.
21.7 Spaces:
We can add extra blank spaces to our script to improve clarity. These spaces are ignored by VBScript.
1) Asc Function 'Returns ANSI character code corresponding to first letter in a stringDim valval="hyderabad"val=Asc(val)msgbox val 2) Chr Function 'Returns the character associated with the specified ANSI character code.Dim valval=65val=Chr(val)msgbox val 'Output: A 3) Date FunctionIt returns current system DateDim myDatemyDate=Datemsgbox myDate 4) Abs FunctionIt returns obsolute value of the given number.Dim numnum=157.56num=Abs(num)msgbox num 'Output: 157.56 num=-157.56num=Abs(num)msgbox num 'Output: 157.56 Note: It provide positive value 5) Array FunctionWe can enter list of values using this functionEx:Dim var'List of stringsvar=Array("Hyderabad","Chennai", "Nellore")msgbox var(0) 'output: Hyderabadmsgbox var(1) 'output: Chennaimsgbox var(2) 'output: Nellore 'List of numeric valuesvar=Array(100,200, 300)msgbox var(0) 'output: 100msgbox var(1) 'output: 200msgbox var(2) 'output: 300 'List of mixed valuesvar=Array(100,"India", #01-05-2010#)msgbox var(0) 'output: 100msgbox var(1) 'output: Indiamsgbox var(2) 'output: 01/05/2010 6) IsArray Function It checks weather the given variable is an Array or notDim var1, var2,x'List of stringsvar1=Array("Hyderabad","Chennai", "Nellore") x=isArray(var1) 'It returns True/False like Resultmsgbox x x=isArray(var2) msgbox x 7) IsDate It checks weather the given value is Date type data or notExamples:Dim myDate,xmyDate=100x=IsDate(myDate)msgbox x 'Output: False myDate="India"x=IsDate(myDate)msgbox x 'Output: False myDate=#10/05/2010#x=IsDate(myDate)msgbox x 'Output: True myDate=#10-05-2010#x=IsDate(myDate)msgbox x 'Output: True myDate=#10-05-10#x=IsDate(myDate)msgbox x 'Output: True myDate=10-05-2010x=IsDate(myDate)msgbox x 'Output: False 8) DateDiff Function It provides difference between two dates, based on interval (day/month)Dim Date1, Date2,xDate1=#10-10-2008#Date2=#10-09-2010# x=DateDiff("d", date1,date2) 'd for daymsgbox x' It subtracts date1 from date2 x=DateDiff("m", date1,date2)' m for monthmsgbox x' It subtracts date1 from date2 x=DateDiff("y", date1,date2) 'it considers days onlymsgbox x' It subtracts date1 from date2Note: through this function, we can day or month wise diffrence only. 9) IsNumeric It checks weather the given value is numeric or not and It provides True/False like ResultExample:Dim val,xval="100"x=Isnumeric(val)msgbox x 'Output: Trueval=100x=Isnumeric(val)msgbox x 'Output: True x=Isnumeric(500)msgbox x 'Output: True x=Isnumeric("India")msgbox x 'Output: False 10) Len Function It finds legngh of the StringExample:Dim val,xval="Hyderabad"x=Len(val)msgbox x 'Output: 9 val=100x=Len(val)msgbox x 'Output: 3 val="Hydera100"x=Len(val)msgbox x 'Output: 9 val="hy$@*de"x=Len(val)msgbox x 'Output: 7 val="100"x=Len(val)msgbox x 'Output: 3 val=#10-10-2010#x=Len(val)msgbox x 'Output: 10 x=Len("Krishna")msgbox x 'Output: 7 x=Len(Krishna)msgbox x 'Output: 0 x=Len()msgbox x 'Output: Error 11) Left Function Returns a specified number of charectors of a given string from left sideSyntax:variable=Left(string,Lengh)Example:Dim val,xval="Hyderabad"x=Left(val,3)msgbox x ' Output: Hyd val="9247837478"x=Left(val,1)msgbox x ' Output: 9 val="H92yderabad"x=Left(val,3)msgbox x ' Output: H92 x=Left(9247837478,5)msgbox x ' Output: 92478 val=#10-10-10#x=Left(val,3)msgbox x ' Output: 10/ 12) Right FunctionReturns a specified number of charectors of a given string from Right sideExample:Dim val,xval="Hyderabad"x=Right(val,3)msgbox x ' Output: bad val="9247837478"x=Right(val,1)msgbox x ' Output: 8 val="H92yderabad"x=Right(val,3)msgbox x ' Output: bad x=Right(9247837478,5)msgbox x ' Output: 37478 val=#10-10-10#x=Right(val,5)msgbox x ' Output: /2010 13) Mid function Returns a specified number of characters of a given string Example:Dim val,xval="Hyderabad"x=Mid(Val,5,3)msgbox x ' Output: rab val="Hyderabad"x=Mid(Val,5)msgbox x ' Output: rabad val="9247837478"x=Mid(val,6,5)msgbox x ' Output: 37478val="H92yderabad"x=Mid(val,1)msgbox x ' Output: H92yderabadx=Mid(9247837478,5)msgbox x ' Output: 837478 val=#10-10-10#x=Mid(val,5)msgbox x ' Output: 0/2010 14) StrReverse retuns reverse value of a stringExample:Dim val,xval="Hyderabad"x=StrReverse(val)msgbox x 'Output dabaredyH val="001"x=StrReverse(val)msgbox x 'Output: 100 val=1002x=StrReverse(val)msgbox x 'Output: 2001 val=#10-10-10#x=StrReverse(val)msgbox x 'Output: 0102/01/01 x=StrReverse("Hyderabad")msgbox x 'Output: dabaredyH x=StrReverse(100)msgbox x 'Output: 001 15) StrComp Function It compares two string (Binary and textual)Ifa) Both are equal, returns 0(zero) b) String 1 greater than string 2, returns 1(one) b) String 2 greater than string 1, returns -1Example:Dim str1,str2,xstr1="India"str2="India"x=StrComp(str1,str2,1)msgbox x 'Output 0 str1="india"str2="INDIA"x=StrComp(str1,str2,1)msgbox x 'Output 0 str1="India"str2="Indian"x=StrComp(str1,str2,1)msgbox x 'Output -1 str1="Indian"str2="Ndia"x=StrComp(str1,str2,1)msgbox x 'Output -1 str1="Indian"str2="India"x=StrComp(str1,str2,1)msgbox x 'Output 1 str1=100str2=100x=StrComp(str1,str2,1)msgbox x 'Output 0 str1=100str2=101x=StrComp(str1,str2,1)msgbox x 'Output -1 16) Lcase functionCoverts Upper case values into Lower caseDim val,xval="HYDERABAD"x=Lcase(val)msgbox x 'Output hyderabad val="Hyderabad"x=Lcase(val)msgbox x 'Output hyderabad val="HederabaD"x=Lcase(val)msgbox x 'Output hyderabad val="hyderabad"x=Lcase(val)msgbox x 'Output hyderabad x=Lcase("HYDERABAD")msgbox x 'Output hyderabad 17) Ucase functionCoverts Lower case values into Upper caseExample: Dim val,xval="HYDERABAD"x=Ucase(val)msgbox x 'Output HYDERABAD val="Hyderabad"x=Ucase(val)msgbox x 'Output HYDERABAD val="HederabaD"x=Ucase(val)msgbox x 'Output HYDERABAD val="hyderabad"x=Ucase(val)msgbox x 'Output HYDERABAD x=Ucase("HYDERABAD")msgbox x 'Output HYDERABAD 18) Round FunctionReturns the round value of a given valueif value decimal point above .5 it returns, next nearest value, below.5 returns before integer value.Example:Dim num,xnum=14.49x=Round(num)msgbox x 'Output: 14 num=14.59x=Round(num)msgbox x 'Output: 15 num="14.49"x=Round(num)msgbox x 'Output: 14 num="Hyd"x=Round(num)msgbox x 'Output: Error 19) Trim FunctionReturns a copy of string without leading spacesDim valval=" VB Script"x=Trim(val)msgbox xx=Len(x)msgbox x val=" 100"x=Trim(val)msgbox xx=Len(x)msgbox x val=" 2#$%^"x=Trim(val)msgbox xx=Len(x)msgbox x val=" VB Script "x=Trim(val)msgbox xx=Len(x)msgbox x 20) Ltrim FunctionIt removes spaces from left side of a stringDim valval=" VB Script"x=LTrim(val)msgbox xx=Len(x)msgbox x val="100 "x=LTrim(val)msgbox xx=Len(x)msgbox x val=" 2#$%^ "x=LTrim(val)msgbox xx=Len(x)msgbox x val=" VB Script "x=LTrim(val)msgbox xx=Len(x)msgbox x val= 100 x=LTrim(val)msgbox xx=Len(x)msgbox x 21) RtrimIt removes spaces from right side of a stringDim valval="VB Script "x=RTrim(val)msgbox xx=Len(x)msgbox x val=" 100 "x=RTrim(val)msgbox xx=Len(x)msgbox x val=" 2#$%^ "x=RTrim(val)msgbox xx=Len(x)msgbox x val=" VB Script "x=RTrim(val)msgbox xx=Len(x)msgbox x val= 100 x=RTrim(val)msgbox xx=Len(x)msgbox x 22) Split functionReturns a zero-based, one-dimensional array containing a specified number of substringsExample:Dim a,b,xb="VB Script is a Powerful scripting Language"a=Split(b," ")x=IsArray(b)msgbox x 'Output: Falsex=IsArray(a)msgbox x 'Output: Truemsgbox a(6) 'Output: Languageb="VB,Script,is,a,Powerful,scripting,Language"a=Split(b,",")msgbox a(5) 'Output: Scriptingb="VB Script is a Powerful scripting Language"a=Split(b)msgbox a(5) 'Output: Scriptingb="VB@Script@is@a@Powerful@scripting@Language"a=Split(b,"@")msgbox a(5) 'Output: Scriptingb="VBScriptisaPowerfulscriptingLanguage"a=Split(b)msgbox a(5) 'Output: Error
Labels: VB Script Built in Functions
