7 steps to writing a robust QTP-Script

Preface: Based on our previous executing experience, theexecute/maintain QTP automation effort cost more effort than we had imaged. Inthe coming next release, why not write robust QTP script through the lessons welearned? Here are 7 steps we summarized:  

1.      EnhanceObject Identification by using Test Object(TO) special properties:

Don’t use “Ordinal identifier” as the majorproperty, but use special properties:”title”, “text”, “name”, etc.

a.      If the value for these special properties toofixed, use regular expression.

e.g.in the following image, the left TO almost use “Index” to identify the object, butthe right TO is good example to use special properties with regular expression.


b.     Using Dynamic property-value (by SetTOPropertyMethod).

E.g.If you want click the link whose text value equals the “Item Number” just generatedpreviously.

Dim ItemNumber : ItemNumber = Browser("xx").Page("xx").WebElement(“xx”).GetROProperty(“text”)

Browser("xx").Page("xx").Link("xx").SetTOProperty "text", Trim(ItemNumber)

Browser("xx").Page("xx").Link("xx").Click

 

c.      Using Description Programming.

Hereis example to show you how to find the web-button with special property.

Set oDesc = Description.Create()

oDesc("micclass").Value = "WebElement"

oDesc("innerhtml").Value = "Detail.*"

oDesc("html tag").Value = "DIV"

Set Lists= Browser("xx").Page("xxx").WebTable("xxx").ChildObjects(oDesc)

 

2.      Encapsulationyour code into Function or procedure:

We always benefit encapsulation of our block-code:concise code, logical business steps, easy to debug, reusability, etc. Now organizeyour code into procedures as black-box abstractions:

a.      If your block-code keep related actions, encapsulated;

Function HandleTCodeSM37()

    ………………you code for handle SAP t-code SM37

End Function

b.     If your block-code need executed more than twotimes, encapsulated;

c.      If your block-code is more than 100-150 lines,encapsulated;

Function HandleTCodeSM37

      Call HandleTCodeSM37_CheckStatus

      Call HandleTCodeSM37_ProcessIDoc

End Function

 

Sub HandleTCodeSM37_CheckStatus

    ………………you code for more than 150 lines

End Sub

Sub HandleTCodeSM37_ProcessIDoc

    ………………you code for more than 150 lines

End Sub

 

d.     If your business flow has branches, encapsulatedthe previous bock-code (before branch) into function;

e.     There are various cases to encapsulate, if yourcode need recursion, or loop, conditional statement etc, feel free toencapsulate as well.

 

3.      Writingfunction libraries (qfl/vbs/txt files in QTP) error-free:

a.      We prefer to write the function libraries inscript-editor like “Notepad ++”, but forget to check syntax before your savethese files to ALM/SVN. Use QTP check-syntax tool: drag these files into QTP,and press “Ctrl+F7” to check syntax.

b.     Make sure these files being loaded during theQTP executing; otherwise it easily occurs “Mistype” error.

c.      For routines coding, report the input-validity, checkpointsresult. These tries let execution of this function under monitoring.

Sub HandleSM37(itemNumber)

If IsEmpty(itemNumber)

   Reporter.ReportEvent  micFail, "Function HandleSM37: check input", "The itemNumber  " & itemNumber &  "is not valid, pls check the value"

End If

‘………………actions and checkpoints

If IsCheckPointValid Then

    Reporter.ReportEvent  micPass, "Function HandleSM37: Opertion A", "Opertaion A successed"

Else

    Reporter.ReportEvent  micFail, "Function HandleSM37: Opertion A", "Opertaion A Not successed"

End If

End Sub

 

4.      ThePower of Variable Names, TO (Test Object) names, routines name:

Notetwo characteristics of variables names. First, they’re easy to decipher. Youcan simply read them. But second, some of the names are long—too long to be practical;names should be as specific as possible.

                Common bad names to avoid:

a.       TO(Test Object) names: you cannot name all the SAP-OR  root object with “Session”;

b.      NameLoop variable not as simple as i, j, k;

Thefollowing table only show some rules to name variables, for more information, referenceCode complete.

 

-- A good mnemonic name generally speaks to the problem rather than the solution

-- Optimum Name Length: the effort required to debug a program was minimized when variables had names that averaged 10 to 16 characters

-- Longer names are better for rarely used variables or global variables and shorter names are better for local variables or loop variables

-- Variables that contain computed values, if you modify a name with a qualifier like Total, Sum, Average, Max, Min, Record, String, or Pointer, put the modifier at the end of the name

-- It’s better to think of flags as status variables. And you could use named constants and enumerated types to set up the values for status variables

-- The name temp doesn’t tell you anything about what the variable does

-- Use an enumerated type, members of the type all belong to the same group by using a group prefix, such as Color_, Planet_, or Month_

-- When naming constants, name the abstract entity the constant represents rather than the number the constant refers to

-- Avoid numerals in names: avoid file1 and file2

-- Avoid misspelled words in names

 

5.      Avoidhardcode:

Keep away from this bad programming habit; otherwise yourregression testing execution will not be a simple journey, but an awful processto debug when months later after creation of the scripts.

Whenever you use absolute value during coding, be alert.

Bad examples

Replacement by avoiding hardcode

ObjectOne.Click

Wait 3

ObjectTwo.Click

ObjectOne.Click

ObjectTwo.WaitProperty("visible", "True")

ObjectTwo.Click

Object.Select 2

FindRowIndex = ObjectOne.FindRowByCellContent (<ItemName>, <ItemValue>)

Object.Select FindRowIndex

ExpectedResult = DataTable(“ItemExp”, 1)

RunTimeResult = Object.GetROProperty(“text”)

If RunTimeResult = ExpectedResult Then

ExpectedResult = DataTable(“ItemExp”, 1)

RunTimeResult = Object.GetROProperty(“text”)

If StrComp(Trim(RunTimeResult),Trim(ExpectedResult) = 0 Then

‘select all available rows

For i = 1 to 3

TableObject.Select i

Next

‘select all available rows

Dim intRowCount : intRowCount = TableObject.GetROProperty(“row count”)

For intRowIndex = 1 to Cint(intRowCount)

 TableObject.Select intRowIndex

Next

 

6.      Goodhabit of VBScript programming:

The following table only show some examples, for moreinformation, reference VBScript Programmer's Reference.

-- Declare all variables: use an “Option Explicit” as first statement

-- Well commented: always write comments before actual code

-- Destroy the object after finish using: use “Set MyObj = Nothing” statement

-- Pay special attention to variables scope, public or private

-- Do not nest the control of flow (if, case, for, loop) too complex

 

 

7.      Some tipsand experience:

a.       Makesure all path of resources (libraries, datasheet, OR) are correct, and loadingare valid during execution.

b.      Whenusing description programming, by default QTP treat property as regularexpressions. Sometimes add backslash(\) before special character.

set MyDesc = Description.Create()

MyDesc("PropName").RegularExpression = False

MyDesc("PropName").Value = “1.2”

Object(MyDesc).Click

Object(“PropName: = 1\.2”).Click

c.       Make.net object visible before operation

swfObject.Object.Focus

swfObject.Click

d.      Trickysolutions.

Facingconfusing problems, we can always try tricky solutions. For example, when youneed drag scroll bar to display the item in a SAP table, you can use filter todecrease the item numbers and display the item directly.

Anotherexample is double-clicking a SAP-Edit object.

‘Double click SAPEdit object using sending key method

SAPEditObj. SetFocus

SAPGuiWindowObj. SendKey F2

 

Sources:

Tarun Lalwani,QuickTest Professional Unplugged.

Adrian Kingsley-Hughes, KathieKingsley-Hughes, Daniel Read Wrox, VBScript Programmer'sReference.

Steve McConnell, Code completes.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值