Quick test professional

QTP Tips QTP codes QTP Faqs and more

QTP Tips and Faqs

This document gives answer to some FAQ and also provides tips on certain aspects of QTP. 

Data Table Two Types of data tables

  • Global data sheet: Accessible to all the actions
  • Local data sheet: Accessible to the associated action only


Usage:

  • DataTable(“Column Name”,dtGlobalSheet) for Global data sheet
  • DataTable(“Column Name”,dtLocalSheet) for Local data sheet


If we change any thing in the Data Table at Run-Time the data is changed only in the run-time data table. The run-time data table is accessible only
through then test result. The run-time data table can also be exported using DataTable.Export or DataTable.ExportSheet

How can i save the changes to my DataTable in the test itself?

 

  • Well QTP does not allow anything for saving the run time changes to the actual data sheet. The only work around is to share thespreadsheet and then access it using the Excel COM Api’s.


How can i check if a parameter exists in DataTable or not?

  • The best way would be to use the below code:

code:


on error resume next val=DataTable(“ParamName”,dtGlobalSheet) if err.number<> 0 then    ‘Parameter does not exist else    ‘Parameter exists end if



How can i make some rows colored in the data table?

  • Well you can’t do it normally but you can use Excel COM API’s do the same. Below code will explain some expects of Excel COM APIs

code:


Set xlApp=Createobject(“Excel.Application”) set xlWorkBook=xlApp.workbooks.add set xlWorkSheet=xlWorkbook.worksheets.add xlWorkSheet.Range(“A1:B10”).interior.colorindex = 34 ‘Change the color of the cells xlWorkSheet.Range(“A1:A10″).value=”text” ‘Will set values of all 10 rows to “text” xlWorkSheet.Cells(1,1).value=”Text” ‘Will set the value of first row and first col rowsCount=xlWorkSheet.Evaluate(“COUNTA(A:A)”) ‘Will count the # of rows which have non blank value in the column A colsCount=xlWorkSheet.Evaluate(“COUNTA(1:1)”) ‘Will count the # of non blank columns in 1st row xlWorkbook.SaveAs “C:\Test.xls” xlWorkBook.Close Set xlWorkSheet=Nothing Set xlWorkBook=Nothing set xlApp=Nothing



SMART IdentificationSmart Identification is nothing but an algorithm used by QTP when it is not able to recognize one of the object. A very generic example as per the

QTP manual would be, A photograph of a 8 year old girl and boy and QTP records identification properties of that girl when she was 8, now when

both are 10 years old then QTP would not be able to recognize the girl. But there is something that is still the same, that is there is only one girl in

the photograph. So it kind of PI (Programmed intelligence) not AI.

When should i use SMART Identification?

 

  • Something that people don’t think about too much. But the thing is that you should disable SI while creating your test cases. So that youare able to recognize the objects that are dynamic or inconsistent in their properties. When the script has been created, the SI should be enabled,so that the script does not fail in case of small changes. But the developer of the script should always check for the test results to verify if the SIfeature was used to identify a object or not. Sometimes SI needs to be disabled for particular objects in the OR, this is advisable when you use

    SetTOProperty to change any of the TO properties of an object and especially ordinal identifiers like index, location and creationtime.

     


Descriptive Programming

  • Descriptive programming is nothing but a technique using which operations can be performed on the AUT object which are not present in the OR.


Recovery ScenariosWhat is a Recovery Scenario?

  • Recovery scenario gives you an option to take some action for recovering from a fatal error in the test. The error could range in fromoccasional to typical errors. Occasional error would be like “Out of paper” popup error while printing something and typical errors would be like”object is disabled” or “object not found”. A test case have more then one scenario associated with it and also have the priority or order in which itshould be checked.

     


What does a Recovery Scenario consists of?

  • Trigger: Trigger is nothing but the cause for initiating the recovery scenario. It could be any popup window, any test error, particular stateof an object or any application error.
  • Action: Action defines what needs to be done if scenario has been triggered. It can consist of a mouse/keyboard event, close application, call arecovery function defined in library file or restart windows. You can have a series of all the specified actions.
  • Post-recovery operation: Basically defined what need to be done after the recovery action has been taken. It could be to repeat the step, moveto next step etc….


When to use a Recovery Scenario and when to us on error resume next?

  • Recovery scenarios are used when you cannot predict at what step the error can occur or when you know that error won’t occur in yourQTP script but could occur in the world outside QTP, again the example would be “out of paper”, as this error is caused by printer device driver. “Onerror resume next” should be used when you know if an error is expected and dont want to raise it, you may want to have different actionsdepending upon the error that occurred. Use err.number & err.description to get more details about the error.

     

Library Files or VBScript Files
How do we associate a library file with a test ?

  • Library files are files containing normal VBScript code. The file can contain function, sub procedure, classes etc…. You can also use executefilefunction to include a file at run-time also. To associate a library file with your script go to Test->Settings… and add your library file to resourcestab. 


When to associate a library file with a test and when to use execute file?

  • When we associate a library file with the test, then all the functions within that library are available to all the actions present in the test. Butwhen we use Executefile function to load a library file, then the function are available in the action that called executefile. By associated a library toa test we share variables across action (global variables basically), using association also makes it possible to execute code as soon as the scriptruns because while loading the script on startup QTP executes all the code on the global scope. We can use executefile in a library file associated

    with the test to load dynamic files and they will be available to all the actions in the test.

     

Test and Run-time Object
What is the difference between Test Objects and Run Time Objects ?

  • Test objects are basic and generic objects that QTP recognize. Run time object means the actual object to which a test object maps.

Can i change properties of a test object

  • Yes. You can use SetTOProperty to change the test object properties. It is recommended that you switch off the Smart Identification for theobject on which you use SetTOProperty function.

Can i change properties of a run time object?

  • No (but Yes also). You can use GetROProperty(“outerText”) to get the outerText of a object but there is no function like SetROProperty tochange this property. But you can use WebElement().object.outerText=”Something” to change the property.


Action & Functions
What is the difference between an Action and a function?

  • Action is a thing specific to QTP while functions are a generic thing which is a feature of VB Scripting. Action can have a object repositoryassociated with it while a function can’t. A function is just lines of code with some/none parameters and a single return value while an action canhave more than one output parameters. 


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 anyautomation 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 onpure VB Script, so this should be done in a function and not an action. Code specific to QTP can also be put into an function using DP. Decision ofusing function/action depends on what any one would be comfortable using in a given situation.

     

Checkpoint & Output value
What is checkpoint?

  • Checkpoint is basically a point in the test which validates for truthfulness of a specific things in the AUT. There are different types ofcheckpoints depending on the type of data that needs to be tested in the AUT. It can be text, image/bitmap, attributes, XML etc….

What’s the difference between a checkpoint and output value?

  • Checkpoint only checks for the specific attribute of an object in AUT while Output value can output those attributes value to a column in datatable.

How can i check if a checkpoint passes or not?code:


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


My test fails due to checkpoint failing, Can i validate a checkpoint without my test failing due to checpoint failure?code:


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” else    MsgBox “Check Point failed” end if


 Environment
How can i import environment from a file on disk

  • Environment.LoadFromFile “C:\Env.xml”

How can i check if a environment variable exist or not?

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


How to connect to a database?
code:


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=<UID>;PWD=<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

Author: Tarun Lalvani

Source : Mercury forum’s KB article

Advertisements

February 14, 2008 - Posted by | QTP Tips and Faqs | , , , , , , , ,

5 Comments »

  1. I’ve been testing a web page, i’ve recieved an Error massage when the page loaded to add the Cv file from the Windows desktop!

    Run Error:
    Cannot find the “SysListView32” object’s parent “” (class Dialog)

    Browser(“Page 100 – ScreeningWizard”).Dialog(“Välj fil”).WinListView(“SysListView32”).Select “CV”
    Browser(“Nova 100 – ScreeningWizard”).Dialog(“Välj fil”).WinButton(“Open”).Click

    is there any solution to get rid of this Error?

    Comment by Sherry | May 26, 2008 | Reply

  2. Hi,

    I found the details mentioned in this article is very useful.
    But I have found that the total content is same as the article of Tarun Lalwani (http://knowledgeinbox.com/qtp/qtp-faq-tips-tricks/).

    I am confused who is the actual Author of this article. If you are trying to share what you found on internet, please give the credit to real author.

    Comment by Vidya Sagar | November 26, 2008 | Reply

  3. Vidya Sagar,

    Thanks for letting me know the author.I corrrected my mistake. I was not intentionally done this. The fact is that 40% of this blog were collected from the mercury forum’s KB articles , which was closed now.I thought these are very useful articles for the others since only few prople have them, so I was uploaded these posts. but I was not aware of authors for few of the authors.

    Comment by quicktestprofessional | November 27, 2008 | Reply

  4. Hi,

    I’ve seen your posts on this site.. I have a requirement where I need to get all the objects of an application to an excel sheet and use that excel sheet as the OR (Object Repository) and there should not be any OR other than that..

    I’ve succeded in getting all the objects to an excel sheet during runtime.. But not getting any solution as how to write a script basing the objects available in the excel sheet..

    Kindly help me out as it is a very high priority issue..

    Thanks,
    Vij

    Comment by vij | July 6, 2009 | Reply

  5. Hi Mohan,

    Please can you write a micro which will fatch all the QTO object(.Tsr) boject in Excel file. do u know how to do this, if u can do this then please help me.

    Thanks
    Vishal

    Comment by Vishal Singh | June 8, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: