Thursday, March 20, 2008

FAQ - Automation - QTP (191 to 201)

191. Where to use function or action?

Well answer depends on the scenario. If you want to use the OR feature then you have to go for Action only. If the functionality is not about any automation script i.e. a function like getting a string between to specific characters, now this is something not specific to QTP and can be done on pure VB Script, so this should be done in a function and not an action. Code specific to QTP can also be put into a function using DP. Decision of using function/action depends on what any one would be comfortable using in a given situation.

192. How can I check if a checkpoint passes or not?

chk_PassFail = Browser(…).Page(…).WebEdit(…).Check (Checkpoint(”Check1″))
if chk_PassFail then
MsgBox “Check Point passed”
MsgBox “Check Point failed”
end if

193. My test fails due to checkpoint failing, Can I validate a checkpoint without my test failing due to checkpoint failure?

Reporter.Filter = rfDisableAll ‘Disables all the reporting stuff
chk_PassFail = Browser(…).Page(…).WebEdit(…).Check (Checkpoint(”Check1″))
Reporter.Filter = rfEnableAll ‘Enable all the reporting stuff
if chk_PassFail then
MsgBox “Check Point passed”
MsgBox “Check Point failed”
end if

194. How can I import environment from a file on disk?

Environment.LoadFromFile “C:\Env.xml”

195. How can I check if an environment variable exists or not?

When we use Environment(”Param1″).value then QTP expects the environment variable to be already defined. But when we use Environment.value(”Param1″) then QTP will create a new internal environment variable if it does not exists already. So to be sure that variable exist in the environment try using Environment(”Param1″).value.

196. How to connect to a database?

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject(”ADODB.Connection”)
Set objRecordset = CreateObject(”ADODB.Recordset”)
objConnection.Open “DRIVER={Microsoft ODBC for Oracle};UID=;PWD=“
objRecordset.CursorLocation = adUseClient
objRecordset.CursorType = adopenstatic
objRecordset.LockType = adlockoptimistic
ObjRecordset.Source=”select field1,field2 from testTable”
ObjRecordset.ActiveConnection=ObjConnection ObjRecordset.Open ‘This will execute your Query
If ObjRecordset.recordcount>0 then
Field1 = ObjRecordset(”Field1″).Value
Field2 = ObjRecordset(”Field2″).Value
End if

197. How to compare two Excel Sheets cell by cell?

This code will open two excel sheet and compare each sheet cell by cell, if any changes there in cells , it will highlight the cells in red color in the first sheet.

Set objExcel = CreateObject(”Excel.Application”)
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open(”C:\Documents andSettings\zakir\Desktop\zak1.xls”)
Set objWorkbook2= objExcel.Workbooks.Open(”C:\Documents andSettings\zakir\Desktop\zak2.xls”)

Set objWorksheet1= objWorkbook1.Worksheets(1)

Set objWorksheet2= objWorkbook2.Worksheets(1)

For Each cell In objWorksheet1.UsedRange
If cell.Value <> objWorksheet2.Range(cell.Address).Value Then
cell.Interior.ColorIndex = 3′Highlights in red color if any changes in cells
cell.Interior.ColorIndex = 0
End If

set objExcel=nothing

198. How to use sorting in Excel(Ascending and Descending)

Excel Sorting By Row:

Const xlAscending = 1
Const xlNo = 2
Const xlSortRows = 2

Set objExcel = CreateObject(”Excel.Application”)
objExcel.Visible = True

Set objWorkbook = objExcel.Workbooks.Open(”C:\Documents and Settings\zakir\Desktop\zak1.xls”)
Set objWorksheet = objWorkbook.Worksheets(1)

Set objRange = objExcel.ActiveCell.EntireRow
objRange.Sort objRange, xlAscending, , , , , , xlNo, , , xlSortRows
set objExcel=nothing

Excel Sorting By Column :

Const xlAscending = 1′represents the sorting type 1 for Ascending 2 for Desc
Const xlYes = 1

Set objExcel = CreateObject(”Excel.Application”)’Create the excel object
objExcel.Visible = True’Make excel visible
Set objWorkbook = _
objExcel.Workbooks.Open(”C:\Documents and Settings\zakir\Desktop\zak1.xls”)’ Open the document

Set objWorksheet = objWorkbook.Worksheets(1)’select the sheet based on the index .. 1,2 ,3 …
Set objRange = objWorksheet.UsedRange’which select the range of the cells has some data other than blank
Set objRange2 = objExcel.Range(”A1″)’ select the column to sort

objRange.Sort objRange2, xlAscending, , , , , , xlYes
set objExcel=nothing

199. Uninstall a software using VBscript in QTP

This works for uninstalling most applications. In this example I will show how AutoCAD 2005 can be uninstalled using a VBScript.

Open Regedit and look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall search for the application and find a the product code that is in GUID format looking like this: {5783F2D7-0301-0409-0002-0060B0CE6BBA} It might be something else for you. The code below will uninstall AutoCAD 2005 as well as Express Tools.

Copy the code below into Notepad and give the file the extension vbs.

on error resume next
Set WshShell = CreateObject(”WScript.Shell”)
‘ Uninstall Express Tools
WshShell.Run “msiexec /x {5783F2D7-0311-0409-0000-0060B0CE6BBA} /q”,1,true
‘ Uninstall AutoCAD 2005
WshShell.Run “msiexec /x {5783F2D7-0301-0409-0002-0060B0CE6BBA} /q”,1,true

200. How to create window application using VBscript?

using HTML Application (HTA)

An HTML Application (HTA) is a Microsoft Windows application written with HTML and Dynamic HTML. The ability to write HTAs was introduced with Microsoft Internet Explorer 5.0.

HTAs can be made from regular HTML files by simply changing the file extension to .hta. A regular HTML file is confined to the security model of the web browser - i.e. to communicating with the server, manipulating the page’s object model (usually to validate forms and / or create interesting visual effects) and reading / writing cookies. An HTA runs as a fully trusted application and therefore has more privileges than a normal HTML file - for example an HTA can create / edit / remove files and registry entries.

Because an HTA has more privileges than an HTML page, it cannot be executed via http. Rather, the HTA must be downloaded (just like an EXE file) and executed from local file system.

201. What is a Dictionary 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.

Advantages 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 retrieve any number of run time values in to dictionary.

3. It is one of the Parameterization techniques we can use in QTP


We can not specify the values in the design time like Datatable , Action parameters, environment variable.

So it is useful only in Runtime , not design time

No comments: