Monday, 19 August 2013

QTP frequently asked question

1) How to Stop the execution of the test.
Syntax
Services.Abort 2)How to add folders
Sub AddNewFolder(path, folderName) Dim fso, f, fc, nf
Set fso = CreateObject(”Scripting.FileSystemObject”)
Set f = fso.GetFolder(path)
Set fc = f.SubFolders
If folderName “” Then
Set nf =
fc.Add(folderName)
Else
Set nf = fc.Add(”New Folder”)
End If End Sub
An error occurs if the folderName already exists.
3 )How to get the ASCII valuse for a character
Remarks
In the following example, Asc returns the ANSI character code of the first letter of each string:
Dim MyNumber MyNumber = Asc(”A”)
‘ Returns 65.
MyNumber = Asc(”a”)
‘ Returns 97.
MyNumber = Asc(”Apple”) ‘ Returns 65.Note The AscB function is used with byte data contained in a string. Instead of returning the character code for the first character, AscB returns the first byte. AscW is provided for 32-bit platforms that use Unicode characters. It returns the Unicode (wide) character code, thereby avoiding the conversion from Unicode to ANSI.
4) How to get Character fron ASCII value
Remarks
Numbers from 0 to 31 are the same as standard, nonprintable ASCII codes. For example, Chr(10) returns a linefeed character.
The following example uses the Chr function to return the character associated with the specified character code:
Dim MyChar
MyChar = Chr(65) ‘ Returns A.
MyChar = Chr(97) ‘ Returns a.
MyChar = Chr(62) ‘ Returns >. MyChar = Chr(37) ‘ Returns %.Note The ChrB function is used with byte data contained in a string. Instead of returning a character, which may be one or two bytes, ChrB always returns a single byte. ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.
5)Accessing User-Defined Properties of Web Objects
You can use the attribute/ notation to access native properties of Web objects and use these properties to identify such objects with programmatic descriptions. For example, suppose a Web page has the same company logo image in two places on the page:   You could identify the image that you want to click using a programmatic description by including the user-defined property LogoID in the description as follows: Browser(“Mercury Tours”).Page(“Find Flights”).Image(“src:=logo.gif”,”attribute/LogoID:=123″).Click 68, 12 Note: The attribute/ notation is not supported in Netscape 4.x.
6)How to Run Your Automation Program
There are several applications available for running automation programs. You can also run automation programs from command line using Microsoft’s Windows Script Host. For example, you could use the following command line to run your automation program: WScript.exe /E:VBSCRIPT myScript.vbs 7) What is the other way of making coments instead of astrik(‘)
Ans: “Hello” : Rem Comment after a statement separated by a colon.
8)How to run commands in command prompt from qtpAns:You can run standard DOS commands in your QuickTest test using the VBScript Windows Scripting Host Shell object (WSCript.shell). For example, you can open a DOS command window, change the path to C:\, and execute the DIR command using the following statements: Dim oShell Set oShell = CreateObject (“WSCript.shell”) oShell.run “cmd /K CD C:\ & Dir” Set oShell = Nothing 9) what is optional stepAns:
Description
Causes a step to be bypassed if an object in that step is not found.
Syntax
OptionalStep.StatementToMakeOptional
Example
The following example uses the OptionalStep object to make the Paris selection from the Depart WebList an optional step. OptionalStep.Browser(“Mercury Tours”).Page(“Find Flights”).WebList(“depart”).Select “Paris” 10)What is the alternative way to getROpropertyAns) object.QueryValue(property to get)11)How to get the number of childs in a tree a) Object.children.lengthex: Browser(“xxx”).Page(“xxx”).Image(“xxx”).Object.children.length11)How to know that the table has no recordsa) rs.RecordCount 12)What are the types of parameters available in QTPa) 3 types 1) Action Parameters(Input ,output)These parameters are restricted to that action only.i.e we can not use them for another action.Note on’t think that the output action parameter can pass values between action.The only purpouse with output action parameter is to assign the values at run time rather than design time from the external source(application)Advantage: These parameters are bound to the action even this action is called from another test.How to Declare: Using Action property tabSyntax: Input parameter —variable= parameter(“name”) Output parameter—Parameter(“name”)=value 2) Test Parameters(Input ,output)These parameters are not restricted to that action i.e we can use them for any nother action.Note These parameters are not found and unable to use them in any action is called from another test.Advantage: test parameters can pass values between action.How to Declare: Using Test Setting-à parameters tabSyntax: Input parameters— variable= TestArgs (“name”)Output parameters– TestArgs (“name”)=value3)Local ParametersThese are also same as test parameters but we need to Declare and use them from code not from IDE.Advantage:These parameters still can used even the actions are called from any testSyntax:Declarationà LocalParameter(“name”)=valueUse: variable= LocalParameter(“name”)13)what are the different ways to delay the execution steps in QTPAns)3 ways1) object.waitProperty ”propertyName”,”value”,”time in milli sec”This is called conditional wait This will pauses the execution as long as the specified value of the property exist in the AUT or specified time out which ever is earlier2) Wait(seconds)This is called unconditional wait.i.e it will wait for the specified time3) Services.ThinkTime 10Same as wait14) How to minimize QTP while runninga) Set qtApp = CreateObject(“QuickTest.Application”)
qtApp.WindowState = “Minimized”
Set qtApp = Nothing

OpenFileDialog In QTP with help of DotNetFactory

OpenFileDialog In QTP with help of DotNetFactory
Set fd = DotNetFactory.CreateInstance(“System.Windows.Forms.OpenFileDialog”, “System.Windows.Forms”)
fd.InitialDirectory=”c:\\”
fd.Filter=”txt files (*.txt)|*.txt |All files (*.*) |*.*”
fd.RestoreDirectory=true
fd.FilterIndex=2
fd.ShowDialog()
msgbox fd.FileName

Prompt Dialog For Password Entry In QTP with help of DotNetFactory

The following code popup a dialog with a edit box for a password entry when it run from the QTP
Set objForm = DotNetFactory.CreateInstance(“System.Windows.Forms.Form”, “System.Windows.Forms”)
Set objBtn1 = DotNetFactory.CreateInstance(“System.Windows.Forms.Button”, “System.Windows.Forms”)
Set objEdit1 = DotNetFactory.CreateInstance(“System.Windows.Forms.TextBox”, “System.Windows.Forms”)
x=80
y=40
Set p1 = DotNetFactory.CreateInstance(“System.Drawing.Point”,”System.Drawing”,x,y) ‘This will provide the locations(X,Y) for the controls
Set lbl= DotNetFactory.CreateInstance(“System.Windows.Forms.Label”,”System.Windows.Forms”)
lbl.Text=”Enter Password”
lbl.Location=p1
objForm.Controls.Add(lbl)
p1.Y=CInt(lbl.Height)+40
objEdit1.Location=p1
objForm.Controls.Add(lbl)
objEdit1.UseSystemPasswordChar=true’To set the password character From system
objForm.Controls.Add(objEdit1)
objBtn1.Text=”OK”
p1.Y=Cint(p1.Y)+CInt(objEdit1.Height)+20
objBtn1.Location=p1
objForm.CancelButton=objBtn1
objForm.Controls.Add(objBtn1)
objForm.StartPosition=CenterScreen
objForm.Text=”Mohan kakarla”
objForm.ShowDialog
msgbox “The Password You have Entered Is: “&objEdit1.Text

Get The Latest Created File

Get The Latest Created File
Const cdtFirst = #1/1/100#
Dim sFolder : sFolder = “.\”
Dim oFS : Set oFS = CreateObject( “Scripting.FileSystemObject” )
Dim dtLatest : dtLatest = cdtFirst
Dim sExt : sExt = “ppt” ‘ not entirely sure about this extension
Dim oLFile : Set oLFile = Nothing
Dim oFile
For Each oFile in oFS.GetFolder( sFolder ).Files
If sExt = LCase( oFS.GetExtensionName( oFile.Name ) ) Then
If dtLatest < oFile.DateLastAccessed Then ‘ maybe DateCreated/DateLastModified
dtLatest = oFile.DateLastAccessed
Set oLFile = oFile
End If
End If
Next
If oLFile Is Nothing Then
WScript.Echo “No file with extension”, sExt, “found.”
Else
WScript.Echo “found”, oLFile.Name, oLFile.DateLastAccessed
End If

Introduction of Dictonary Object

Dictionary Object stores data key, item pairs. A Dictionary object stores the items in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.
Adavntages of using it in QTP:
1. can be used as Global variable declaration. so that any test can access the values from it in the run time.
2. You can store and retrive any number of run time values in to dictonary.
3. It is one of the Parameterization techique we can use in QTP
Disadvantages:
we can not specify the values in the desingn time like Datatable , Action parameters, environment variable.
So it is useful only in Run time , not design time
Methods:
Add Method
Adds a key and item pair to a Dictionary
object. object.Add (key, item)
Arguments
object Required. Always the name of a Dictionary object.
key Required. The key associated with the item being added.item Required.
The item associated with the key being added.
Remarks
An error occurs if the key already exists.
The following example illustrates the use of the Add method.
Dim d ‘ Create a variable.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
Items Method
Returns an array containing all the items in a Dictionary object.
object.Items( )
Remarks The object is always the name of a Dictionary object.The following code illustrates use of the Items method:
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
a = d.Items ‘ Get the items.
For i = 0 To d.Count -1 ‘ Iterate the array.
s = s & a(i) & “” ‘ Create return string.
Next
Msgbox s
Exists Method
Returns true if a specified key exists in the Dictionary object, false if it does not.
object.Exists(key)
Arguments
object Required. Always the name of a Dictionary object.
key Required. Key value being searched for in the Dictionary object.
Remarks
The following example illustrates the use of the Exists method.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
If d.Exists(“c”) Then
Msgbox “Specified key exists.”
Else
Msgbox “Specified key doesn’t exist.”
End If
Keys Method
Returns an array containing all existing keys in a Dictionary object.
object.Keys( )
Remarks
The object is always the name of a Dictionary object.
The following code illustrates use of the Keys method:
Dim a, d, i ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
a = d.Keys ‘ Get the keys.
For i = 0 To d.Count -1 ‘ Iterate the array.
s = s & a(i) & “” ‘ Return results.
Next
Msgbox s
Remove Method
Removes a key, item pair from a Dictionary object.
object.Remove(key)
Arguments
object Required. Always the name of a Dictionary object.
key Required. Key associated with the key, item pair you want to remove from the Dictionary object.
Remarks
An error occurs if the specified key, item pair does not exist.
The following code illustrates use of the Remove method:
Dim a, d ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
d.Add “c”, “Cairo” …
d.Remove(“b”) ‘ Remove second pair.
RemoveAll Method
The RemoveAll method removes all key, item pairs from a Dictionary object.
object.RemoveAll( )
Dim a, d, i ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
d.Add “c”, “Cairo” …
a = d.RemoveAll ‘ Clear the dictionary.