QTP 8.2 Tips and Tricks

Embed Size (px)

Citation preview

  • 8/14/2019 QTP 8.2 Tips and Tricks

    1/5

    QTP 8.2 Tips & Tricks

    Well this post is about tips on various topics. I will try to cover as many as i have in mind and would try to

    summarize the tips that people have given on the forum

    Data Table

    Two Types of data tables

    Global data sheet: Accessible to all the actionsLocal 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 the spreadsheet 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 existsend 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.worksheet.addxlWorkSheet.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

  • 8/14/2019 QTP 8.2 Tips and Tricks

    2/5

  • 8/14/2019 QTP 8.2 Tips and Tricks

    3/5

    to 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 knowthat error won't occur in your

    QTP 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. "On

    error 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 actions

    depending 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 executefile

    function 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 resources

    tab.

    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. But

    when we use Executefile function to load a library file, then the function are available in the action thatcalled executefile. By associated a library to

    a test we share variables across action (global variables basically), using association also makes it possibleto execute code as soon as the script

    runs because while loading the script on startup QTP executes all the code on the global scope. We can useexecutefile in a library file associated

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

    Add-ins

    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

  • 8/14/2019 QTP 8.2 Tips and Tricks

    4/5

    off the Smart Identification for the

    object 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 nofunction like SetROProperty to

    change 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 repository

    associated 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 can

    have 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 Actiononly. If the functionality is not about any

    automation script i.e. a function like getting a string between to specific characters, now this is somethingnot 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 beput into an function using DP. Decision of

    using 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 of

    checkpoints 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 thoseattributes value to a column in data

    table.

    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"

    elseMsgBox "Check Point failed"

  • 8/14/2019 QTP 8.2 Tips and Tricks

    5/5

    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 thenMsgBox "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 use

    Environment.value("Param1") then QTP will create a new internal environment variable if it does notexists already. So to be sure that variable exist

    in the environment try using Environment("Param1").value.

    How to connect to a database?

    code:

    Const adOpenStatic = 3

    Const adLockOptimistic = 3Const adUseClient = 3Set objConnection = CreateObject("ADODB.Connection")

    Set objRecordset = CreateObject("ADODB.Recordset")

    objConnection.Open "DRIVER={Microsoft ODBC for Oracle};UID=;PWD="

    objRecordset.CursorLocation = adUseClient

    objRecordset.CursorType = adopenstaticobjRecordset.LockType = adlockoptimistic

    ObjRecordset.Source="select field1,field2 from testTable"

    ObjRecordset.ActiveConnection=ObjConnection

    ObjRecordset.Open 'This will execute your QueryIf ObjRecordset.recordcount>0 then

    Field1 = ObjRecordset("Field1").Value

    Field2 = ObjRecordset("Field2").ValueEnd if