February 15, 2012

Using Callbacks on the Regexp.Replace function

On our testprojects, I use regular expressions most of the time for the matching or testing of strings on our Application Under Test. But sometimes I have to use the Replace method of the RegExp object. This day, I discovered an advanced usage of the RegExp.Replace() method. Let me show you with an example:

Dim replacement
Dim regEx : Set regEx = New RegExp ' Create a regular expression.
Dim myString : myString = "Cake, and grief COUNSELING, will be Available at the CoNcLuSiOn of the test."

regEx.Pattern = "\b\w+\b" ' \b = inside word boundary, \w = word character,
                          ' so this pattern matches each single word
regEx.Global = True       ' Set the scope to Global

' normally we replace like this:
replacement = "word"
MsgBox regEx.Replace(mystring, replacement)

' resulting in: "word, word word word, word word word word word word word word word."

This looks familiar I think. But it is not very sophisticated. Wouldn't it be nice if we can replace each match with a custom replacement. With the use of a Function Reference, we can!

 ' We can actually perform a custom action on replace instead of doing a fixed replace
' We do this with making a reference to a function with getref
Set replacement = getRef("Capitelize")

' We need to create a function of course with the same name and three parameters
' 1. singleMatch : the string that matched to the pattern
' 2. position : the position the singlematch string was found
' 3. fullString : the full string (same as 'myString' in this case) without _any_ replacements
Function Capitelize(singleMatch, position, fullString)

    ' Capitelize returns the string singleMatch changed with the first character
    ' in uppercase and all others in lowercase
    Capitelize = ucase(left(singleMatch,1)) & lCase(mid(singleMatch,2))
End Function

MsgBox regEx.Replace(myString, replacement)
' This results in: "Cake, And Grief Counseling, Will Be Available At The Conclusion Of The Test."

Nice huh? Now we can match for a date and replace it on the fly by the correct format. We can replace straight quote characters for curly quotes with the correct orientation with much more ease, or only do a replace if it is within a certain character range in the string. "All new possibilities arise!"

January 27, 2012

Exist or be Created!

Just encountered a collegue having difficulties ensuring if a folder exists. And if a folder does not exists then it has to be created in its full path, not only the lowest level folder. The code was bloated with if/then’s, instr(), instrrev() and left()/right() functions. Even the lowest level folder name was hardcoded.

To help him out, we could go for the fancy recursive solution to show off our 1337 programming skillz, but I decided to just do it with a simple loop. Enjoy.

Public Function EnsureFolder(fPath)
    dim fso, fols, fol, pathBuild
    set fso=CreateObject("Scripting.FileSystemObject")

    fols = split(fpath, "\")

    For each fol in fols
        If fol = "" Then exit for
        pathBuild = pathBuild & fol & "\"
        If Not fso.FolderExists(pathBuild) Then
            Call fso.CreateFolder(pathBuild)
        End If
    Next

    EnsureFolder = pathBuild
End Function

December 13, 2011

QTP and Method Chaining disclosed

With method chaining, you can chain object methods to an object in order to change it. It is the default paradigm of JQuery. After using it a while with JQuery, I figured out it is an extremely usefull method to use with QTP.

If you use QTP long enough, you start to create your own function. But because VBScript doesn't allow optional parameters in homebrew functions, existing functions become clumsy with all types of parameters you don't use most of the time.
I remember using functions like findTextInTable(oTbl, txtToFind, startRow, endRow, startColumn, endColumn, byRef foundRow, byRef foundColumn) where each parameter could also be an array or contain signal characters. This results in a function call like this:
findTextInTable(myTable, array("account xyz", "!customer \d+"), 4, "", "", 5, foundRow, "")
So far for self-explanatory code.

When you use object chaining, you use only the parameters you need AND your code will become self explanatory. The code above will become:
xTable.addFindText("account xyz").addRegexFindText("customer \d+").startRow(4).endColumn(5).getFindRow(foundRow)

Of course this need some programming skills to build this correctly into a class, so let's start with an easy example: A string wrapper. When we create the class for the stringwrapper, we use functions for the methods. Functions can return primitives or objects, and with each object call, we return the object itself. You can also use "Get Property", but that will restrict you in the amount of parameters you can pass with your method to be exactly the same as the Let/Set Property.

Class cls_xString

    Private value_

    Public Function SetValue(v)
        value_ = v
        Set SetValue = me
    End Function

    Public Default Function getValue()
        getValue = value_
    End Function

    Public Function ToUpper()
        value_ = uCase(value_)
        Set ToUpper = me
    End Function

    Public Function Append(v)
         value_ = value_ & v
        Set Append = me
    End Function

    Public Function Prepend(v)
        value_ = v & value_
        Set Prepend = me
    End Function

    Public Function Surround(v)
        value_ = v & value_ & v
        Set Surround = me
    End Function

End Class

We use another trick with the getValue; it is the default return value if the string class is called without a Set. To make the class usable globally, we need an initiator function that returns an object of class xString:

Public Function [new xString](initialValue)
    Set [new xString] = new cls_xString
    call [new xString].SetValue(initialValue)
End Function

And now we can create some testcode:
Dim myString
Set myString = [new xString]("W00t, Method Chaining is really Working!")
msgbox myString.ToUpper.Prepend("(").Append(")").Surround("'")

This will result in an editbox with text '(W00T, METHOD CHAINING IS REALLY WORKING!)'
The method are parsed from left to right; first the string is transformed to uppercase, then prepended by (, appended by ) and finally surrounded by '.

Other usage:
- Narrowing down a collection and get a single object
Set linkCollection = [new linkCollection](oPage)    ' Get all links from a page
Set myLink = linkCollection.withRegexText(".*Help.*").Index(3)

Or do directly an action on that object:
linkCollection.withRegexText(".*Help.*").Index(3).Click

- Narrowing down collections and do actions on them:
Set editCollection = [new editboxCollection](oPage)    ' Get all editboxes from a page
' Set all editboxes that are visible, enabled and empty with a value
editCollection.withProperty("visible:=true").withProperty("enabled:=true").withValue("").setValue("Foo Bar!")

- Do a custom action on an object
Private Function DisplayToString(object)
    MsgBox object.ToString()
End Function
Set editCollection = [new editboxCollection](oPage)    ' Get all editboxes from a page
' Show the toString value of all editboxes that are not visible on a page
editCollection.withProperty("visible:=false").each(getRef("DisplayToString"))

Method chaining is very versatile, with the trap of being used to much. Actually it does pretty much the same using the 'with' keyword with objects. It can make your code more clear, or just more obfuscated. Use it wisely.

May 11, 2011

Curious QTP behaviour when using on error: Proceed to Next Step

Because we build our own framework, the on error settings in QTP are default on "Proceed to Next Step" for our unattended continuous script. So when a test is scheduled in the middle of the night, the test won't stall on errors like a function with an argument too few or many, an array that is out of bounds or an accidentally undeclared variable (please build an undeclared variable checker HP, the Patterson boys could do it years ago, you can do it too!).

It works fine most of the time, although debugging is done with an "attended run" script and all possible error detection on: Popup Messagebox and Option Explicit everywhere.

The curious behaviour happened in an Select Case. Consider this code:

Option Explicit
Dim myCondition : myCondition = 2
Select Case myCondition
    Case 1 MsgBox "Wooh! Condition 1"
    Case 2 MsgBox "Displaying undeclared variable: " & undeclaredVariable
    Case 3 MsgBox "Meh, Condition 3"
End Select 

When you run this code in attended mode, you'll get a nice error nagging about the undeclared variable.
However, when you run this in unattended mode, you'll get a messagebox with the text: "Meh, condition 3"

QTP is doing what it says: Continue with the next step. But the step is not fulfilling the Select condition and program technical a major sin!
Because I ran this accidentally in unattended mode, it took me some while to get a finger behind the error. In the end, I learned to always debug in attended mode.

May 10, 2011

Stuttering Firefox 4; Solved!

Haha! I solved my stuttering, stalling and staggering FireFox 4. An annoying problem that let FF halt for a second while scrolling, typing and selecting. 
The solution: just rename or delete the sessionstore.js file (don't worry, FF will create a new one for you) and restart FireFox. Firefox works as a sunshine right now.

For OSX users, it is located here: /Users/{username}/Library/Application Support/Firefox/Profiles/{randomkey}.default/sessionstore.js

May 5, 2011

Sneller zoeken op Marktplaats

Het zoeken naar spulletjes op marktplaats kan soms wat tijdrovend zijn. Zo ben ik al een tijdje op zoek naar een bankierslamp die ook wel een notarislamp genoemd wordt. Als eerste zoek je dus op "bankierslamp" en krijg je 10 hits. Na die een beetje doorgelopen te hebben zoek je vervolgens op "notarislamp" met 16 hits. Naast dat er 4 dubbele in de lijst staan, wil je ook nog even terug naar de zoekactie op bankierslamp terwijl je niet zo slim was geweest de notarislamp te zoeken in een aparte tab. Inefficiënte ellende alom.

Booleaanse logica
Vandaar dat ik wat ben gaan experimenteren met de marktplaats zoek functie (je kan ze ook deels ontdekken via de geavanceerde search, maar dat zit weer 1 klik verder, dus wie komt daar nu?). En ontdekte het volgende:
Marktplaats zoekt standaard met AND. Dit kan je overrulen door OR. Ik had dus moeten zoeken op "bankierslamp or notarislamp": 22 hits.

Parenthesiwat?
Dit kan je ook combineren met een AND criteria, bijvoorbeeld bij het zoeken op skeelers oftewel inline skates. Om deze zoekterm in te voeren zoek ik op "skeelers or inline skates". De AND criteria tussen inline en skates heb ik hier niet ingetikt, want dat marktplaats standaard. Helaas zoekt marktplaats dan op alle termen waar skeelers of inline in voorkomt, en daarnaast in elk geval skates.
Oplossing: Haakjes! Zoeken op "skeelers or (inline skates)" zorgt ervoor dat marktplaats begrijpt wat je wilt.

Uitgesloten
Een andere mogelijkheid is uitsluiten met NOT. Zo zocht ik een sleutel om een klok mee op te winden. De zoekterm "sleutel klok" levert resultaten op, maar helaas nog heel veel in combinatie met sloten. Om deze uit te sluiten zoek je op "sleutel klok not slot", et voila.

Je kan dit natuurlijk net zo ingewikkeld maken als je wilt, mijn huidige zoekterm voor de gewenste lamp ziet er nu als volgt uit:
"((notaris or bankiers) and lamp) or notarislamp or bankierslamp", waarbij ik dus ook de anglicismen "notaris lamp" en "bankiers lamp" vind: 31 hits.

Sterretje en spelling

Op marktplaats komt lui van alle allooi, ook degene die het niet zo nauw nemen met onze geliefde spelling. Skeelers wil je ook wel eens vinden onder de naam skealers en inline skates als inlijn skeets, skeates, skeats of skaats. Je kan hier een mooie OR term mee bouwen, sneller is het gebruik van het sterretje.
Op marktplaats heb ik een wildcard ontdekt en gelijk een hele krachtige: de spin (*) die matcht op alles behalve de spatie.
Om niets te missen van het buitenspeelgebeuren zoek je op "sk*lers or (inl*n* sk*t*s)".

Slim
Gelukkig is marktplaats zelf ook slim. Het heeft een library van veelgebruikte termen. Zoek je dus op skeelers, dan krijg je ook automatisch alle zoektermen voor alle andere vormen van inline skating terug. Helaas zoek je op marktplaats vaak niet altijd op een veelgebruikte term en dan is het wel superhandig als je met een beetje slimme zoekterm snel resultaten kan vinden.

Mocht je nog meer handige zoektips hebben, dan hoor ik ze graag in de comments!




December 1, 2010

Using the Levenshtein Distance to get the best match from a list

A while ago, I created a document in which I explained how to use the Levenshtein Distance to get the best match from a list of options. Well, the article has its own introduction so if you are interested; start reading!

November 19, 2010

QTP 10 first impressions: Meh

This week, QTP 10 was installed at our workspace. We upgraded from 9.1 to 10, so we could also experience all new features of 9.5.
First of all, this is written from my own perspective. I don't use the object repository, the default report, multiple actions or the native QTP connection. I know, these things are improved, but I cannot write about them.

The good things
OK, the function viewer is great. Unfortunately, it does not display classes and properties, while functions and subs inside classes are displayed as Publics. What was the problem with displaying classes as expandable trees and with properties and methods underneath you would think.
Also functions starting with a square bracket [ are not rendered in the function viewer.

The Todo list could become quite handy, but after years working without it, I can... do without it.

The bad things
200 Megs. That is the amount of RAM QTP consumes if I start it, even without any scripts loaded. Just to be fair, I think it is more a problem with the installation and that only 2 of the 30(!!) patches are loaded by default, but I cannot change it and it bothers me. So I categorized it as a bad thing.
This brings me to the next point: Hangs. It hangs a lot and it makes my PC slow, especially Internet Explorer and Windows Explorer. Again, I think it is a problem with the installation and I have to look it up and/or ask support to our godlike and always kind administrators (without them I cannot do anything, so playing nice is important. Maybe they read this.). I think that is a bad thing.

The "Public Default Property Let Foobar()
End Property
End Property
End Property
End Property" syndrom.
WHY???

Variable typing is messed up:
Print "123" + "321" results in "123321". Are we switching to Javascript or what? I thought VBScript was proud of their dynamic var typing (as if that is a good thing). Don't mess with my brain by changing the working of the + sign. Make variables dynamic or make operators dynamic, not both!

Variable conversion is different:
I noticed a classical rounding error: subtracting two numbers can end up with a very small decimal part. Like (not a real example):
4.32 + -0.32 results in 4.00000000000014
Annoying when you want to compare two numbers during automated testing.

The bad things that remain
No autocomplete inside libraries.
No jump-to-function from libraries to other libraries.
The find is still buggy, with annoying "forward or backward" radiobutton.
'Sometimes a duplicate code line overwrites other code' bug.
No declaration check on variables other than during runtime.
Set myCompositeClass = [new componentA](new componentB) results in a syntax error while it is theoretical correct.

Conclusion
I think my installation is not correct, so I had a bad week and am a bit prejudiced about my new QTP. The function viewer certainly speeds up my work, unfortunately it is implemented poorly. The problems I have with my memory and performance is probably due to the installation, on my run PCs I do not have these problems, only on my development machine (but why couldn't it be correct the first time?). The changed variable conversion and operator functionality is really not a good thing. Besides that it influences our existing tests, it is not an improvement. The remaining annoyances stays, and from a user perspective this is merely a QTP 9.6 release and not a 10 major version. I think it is time HP/Mercury will get a real competitor and we get a real choice what automated test software we can use. They are now functioning like our dutch railway system: "Use our system or don't; we don't bother".

November 18, 2010

QTP and how to create an autodestruct class

Somewhere in our application under test, we have a webpage that only continues loading if the mouse is slightly moved. The page is loading while QTP is syncing the page. During the syncing, I cannot send mouse move commands from QTP to the application.

I don’t know why the application has this feature and “they” don’t want to fix it, so… work around time.


I created a low profile mousemove application in C that only does one thing: After 5 seconds it moves the mouse cursor one pixel to the right. And after 5 seconds it moves the cursor one pixel to the left. Repeat.


Actually it is an cut down version of out nolock.exe application, this application sniffs the mouse cursor movements. If it detects inactivity for 10 minutes, it moves the mouse cursor slightly in the same manner as discussed above. This program has a shortcut in the Windows Startup folder and prevent the screen from locking when automated tests are running.


But back to the autodestruct class. We want to have a “single line of code” to implement this in any function where we expect the screen to hang. This class starts the mouse moving as soon as it is created and stops the mouse moving when it is destroyed.


Private Const MOUSE_MOVE_APPL_NAME = “mouseMover.exe”

Private Const BINARIES_FOLDER = “C:\QTP_Framework\tooling\bin\”


Class cls_AutoDestructMouseMover

Private Sub class_initialize()

‘ Put the cursor somewhere to prevent it is on the extreme right position

call extern.SetCursorPos(100, 150)

‘ Run the external mouse move application

systemutil.Run BINARIES_FOLDER & _

MOUSE_MOVE_APPL_NAME, "", BINARIES_FOLDER

End Sub


Private sub class_terminate()

‘ Kill the mouse move application

systemUtil.CloseProcessByName MOUSE_MOVE_APPL_NAME

End Sub

End Class


And make a public accessor for the class:


Public function [new AutoDestructMouseMover]

Set [new AutoDestructMouseMover] = new cls_AutoDestructMouseMover

End Function


Now, we can implement the class whenever it is needed:


Public Function Example

‘ Create the object, make it move!

Dim autoDestructMouseMover : Set autoDestructMouseMover = [new AutoDestructMouseMover]

Call TimeConsumingFunction


‘ Destroy the object, let it stop!

Set autoDestructMouseMover = Nothing

End Function


But wait, we called it an “autodestruct” class, what about the autodestruct? Well, The last line of code is not necessary. When the Example function ends, it automatically destroys all variables and objectpointers with local scope. We can simply reduce the implementation of the autodestructor to one line of code (I know, I am cheating with the semicolon):


Public Function Example

‘ Create the object, make it move!

Dim autoDestructMouseMover : Set autoDestructMouseMover = [new AutoDestructMouseMover]

Call TimeConsumingFunction

End Function


And the autoDestructMouseMover object is automatically destroyed with the function clean up.


Nice. But are there more appliances for an autodestruct object? Oh, yes.

What do you think an autoDestructStackTracer?

Class cls_AutoDestructStackTracer

Private procedureName_

Public Sub Init(pName)

procedureName_ = pName

Print now & “ ADST Start “ & procedureName_

End Sub


Private sub class_terminate()

Print now & “ ADST Exit “ & procedureName_

End Sub

End Class


And make a public accessor for the class:


Public function [new AutoDestructStackTracer](pName)

Set [new AutoDestructStackTracer] = new cls_AutoDestructStackTracer

[new AutoDestructStackTracer].Init pName

End Function


And let’s test it:


Public Function ExampleParent

Dim autoDestructStackTracer : Set autoDestructStackTracer = [new AutoDestructStackTracer](“ExampleParent”)

Print “I’m with ExampleParent now!”

Call ExampleChild

End Function


Public Function ExampleChild

Dim autoDestructStackTracer : Set autoDestructStackTracer = [new AutoDestructStackTracer](“ExampleChild”)

Print “I’m with ExampleChild now!”

End Function


Call ExampleParent

Output:

18-11-2010 9:06:06 ADST Start ExampleParent

I’m with ExampleParent now!

18-11-2010 9:06:06 ADST Start ExampleChild

I’m with ExampleChild now!

18-11-2010 9:06:06 ADST Exit ExampleChild

18-11-2010 9:06:06 ADST Exit ExampleParent


And consider uses of an [new AutoDestructFunctionTimer](pName) to measure the performance of your different functions. Or a [new ScreenShotOnFunctionExit] (and more generalized: [new ExecuteOnFunctionExit](“call PerformScreenshot”)) when you have lots of functions with multiple exit points.

October 29, 2009

Book review Quicktest Professional Unplugged

Introduction
After unpacking the book, it smiled to me in a pretty color setting, although, it reminded me on my times working at ABN AMRO. The cover shows the well known picture of the QTP splash window. The book is quite large, approximately 20x30cm and contains around 430 pages. The pages are all printed in black and white (well, the white was not printed, it was more the property of the paper itself). It is build up into chapters, and the book does have a table of contents and an index.


Content
The first thing that you’ll notice is lots of sample code. This comes in two ways: Code that supports the text and code that belongs to the challenges at the end of each chapter. The book is mainly written in a the first person plural form, so you will never be addressed personally, making the book somewhat formal. Nevertheless, it is an easy read. The layout is open and organized. Each chapter handles one subject and is providing you an introduction to this subject justifying why it is included in the book. Then it hops directly to the matter. At the end of each chapter is room for your own notes.

Most of the subjects are not new, and can be found over the web, the QTP help file and documentation provided by Mercury/HP, but it is very helpful to have it packed in one complete book and only the –no need to search anymore- property makes it worthwhile owning this book alone.
When I have to cover a new subject during my daily work, I peek into the applicable chapter which brings me on track in no time. The text and the code sticks very to the matter, meaning it is all about solving low level problems you’ll encounter by coding your automated tests.

It does not describe test methodologies or how to build a test framework. Well, actually there is a chapter “Designing Frameworks”, but it is more a guideline and best practices enumeration. Also you’ll not find examples dealing the problems of a virtual donut shop or remote control.
Tarun Lalwani writes: “This book is targeted at automation engineers who want to exploit the power that QTP offers…” and I think that is a very good description of the book. The sample code is of high level, sometimes too extensive. Programming experience is necessary to extract the full potential of it.

Conclusion

QuickTest Professional Unplugged is a very comprehensive book written by experience. It is full of handy sample code and neat tricks. Each chapter will provide you with one or more “I didn’t know that!” experiences. The book is created for a small audience, but for this particular audience it is certainly a must have.

April 26, 2009

How to use classes in QTP revisited

Classes in QTP are a bit tricky if you want to use them for the first time. You have to know and understand the principle that a class got local scope for the QTP Function Library (.qfl file) it is in, and that if you want to use it in another library, you’ll have to create a constructor (I have shown this before, but I’ll do it again):


' Constructor:
Public Function [new CustomClass]()
     Set [new CustomClass] = new cls_CustomClass
End Function

' Class definition:
Class cls_CustomClass
     Private Sub Class_Initialize()
         Print "Custom Class: I am created!"
     End Sub
End Class

' using the object in another library:
Dim myClass
Set myClass = [new CustomClass]

This seems like a drawback, but it creates a great possibility. Normally, it is not possible to pass initialisation parameters into a new class, because Class_Initialize does not accept parameters. But using the constructor function, we can!

Option Explicit

' Make a private variable that we can pass into the class
Private PARAMETER_ARRAY

' Create a function that returns the requested class. Accepts
' a parameter array. Note: Do not use paramArray, it is reserved!
Public Function [new customClass](ByVal parameterArray)

     PARAMETER_ARRAY = parameterArray

     ' Return a clsCustomClass object
     Set [new CustomClass] = new cls_CustomClass

End Function

' Create the custom class. Classes are always declared Private in QTP
Class cls_CustomClass

     ' The sub Class_Initialize is used to initialize the object with the
     ' parameters passed into the constructor
     Private Sub Class_Initialize()
         ' Do something with the parameters
         Print join(PARAMETER_ARRAY, vbNewLine)
     End Sub

End Class

' Test code
Dim myClass
Set myClass = [new customClass](array("first parameter", "second parameter", "etc."))

Notice what we just did: We created a Private variable, that can only be used by CustomClass classes. So with the use of Private variables with local scope to the library where you put the class in, you create a static variable for the class!
A static variable in this context is a variable that keeps its value and can be used over multiple objects of the same class:

' Declaring the static variables
Private INSTANCE_COUNTER : INSTANCE_COUNTER = 0

' Constructor:
Public Function [new CustomClass]()
     Set [new CustomClass] = new cls_CustomClass
End Function

' Class definition:
Class cls_CustomClass
     Private Sub Class_Initialize()
         Print "I am created and I have " & INSTANCE_COUNTER & " sister(s)."
         INSTANCE_COUNTER = INSTANCE_COUNTER + 1
     End Sub

     Private Sub Class_Terminate()
         INSTANCE_COUNTER = INSTANCE_COUNTER – 1
     End Sub
End Class

' using the object in another library:
Dim a, b, c
Set a = [new CustomClass]
Set b = [new CustomClass]
Set c = [new CustomClass]

Results in:
I am created and I have 0 sister(s).
I am created and I have 1 sister(s).
I am created and I have 2 sister(s).

By using the static variable as a reference for the object itself, we can create a singleton object. A singleton is an object whereof only one instance at a time can exist. In object oriented languages, singletons are normally used for large objects, or objects that will go bad if multiple instances exists like deadlocks or instability.

Option explicit

Private SINGLETON : Set SINGLETON = Nothing

Public function [new Singleton]()
     If SINGLETON Is Nothing Then
         set SINGLETON = new cls_Singleton
     End If

     Set [new Singleton] = SINGLETON
End Function

Class cls_Singleton

     Private Sub Class_Initialize
         Print "I am unique!"
     End Sub

End Class

Dim st1, st2, st3
Set st1 = [new Singleton]
Set st2 = [new Singleton]
Set st3 = [new Singleton]

Results in only one:
I am unique!

There is a drawback: Once a singleton object is created this way, it is not possible to destroy it without the use of some code violating the object oriented principle.

April 24, 2009

QTP variable name conventions

When you start with a new test automation project, your code is conveniently arranged and you still have a clear overview over the locations and naming of variables and functions. But later on it will become a disaster if you don’t manage it a little bit.
That is why I wrote a name convention article. It is not an official how-you-should-do-it document, it is just the way I do things and written from experience.

' Public and Private constants in capitals
Public Const MOUSEEVENTTF _MOVE = 1
Private Const APPLICATION_MAIN_WINDOW = "name:=My Application"

' Functions, Subs and local variables in lowerCamelCase
Public Function myCustomFunction(thisVariable, thatVariable)

     ' Variables with a known type can be declared with the type abbreviation in front of it
     Dim arrStaticArry(5), objFile, intCounter, blnReadOnly

     ' Variables with a unknown type are declared without a type abbreviation, This is also applicable for variables used in self commentary code.
     Dim customContainer, fileWasFound

     ' Consts with local scope are declared with the same format as variables
     Const cannotBeChanged = True

End Function

' Classes in UpperCamelCase. In QTP I use the cls_ tag in front of it for reasons explained later.
Class cls_EventListener

     ' Public variables are in UpperCamelCase too
     Public BufferLength

     ' Private variables with class scope are lowerCamelCase with an underscore behind it
     Private updateCounter_, eventNumber_

     ' Properties, Subs and Functions (public and private) are all in UpperCamelCase
     Public Property Get EventNumber()
         EventNumber = eventNumber_
     End Property
End Class

In QTP, a class gets a local scope. To make it global, you have to add a function returning that class. As a side effect, this gives you the opportunity to initialize the class. Of course you have to add an init method to your class if you do it this way.

Public Function [new EventListener](initializationParameters)
     Set [new EventListener] = new cls_EventListener
     [new EventListener].Init(initializationParameters)
End Function

Now, you can set a new class with the following code in another library:
Set EventListener = [new EventListener]("codebase:=Unicode")

Side note: The square brackets around a variable lets you enter every character for a variable, including spaces, special characters etc. If you find that inconvenient, you can use an underscore: new_EventListener to mimic normal VB functionality.
I use the square brackets when I want to express importance for example:

' Call the main routine of this script:
[___ !MAIN! ___]

' Or to enjoy my co workers (and to see if they ever peer review my code):
Dim [ O\-<>-/O ], [ ¿Que? ]

April 8, 2009

A few ways to use arrays

When you first encounter Arrays in QTP it is not very easy to understand quickly. VBScript makes use of a few types of arrays: Static, Dynamic, Assigned to a normal variable and Dictionary objects (the dictionary object is not discussed in this article.). Static and Dynamic arrays can be one dimensional or multidimensional.

First the simple static array. Static because it can only contain a fixed amount of items; Simple because we only put strings in it referred by the indexnumber (or subscript) of the array:


' Simple static array
Dim weekdays(6)

weekdays(0) = "Mon"
weekdays(1) = "Tue"
weekdays(2) = "Wed"
weekdays(3) = "Thu"
weekdays(4) = "Fri"
weekdays(5) = "Sat"
weekdays(6) = "Sun"

When you don't have plans to use the subscript, you can also create the array directly. And yes, it works the same with index numbers as with the weekdays array, but it is good programming practice to use the former method if you want to call the array items by subscript number.

' Simple assigned array
Dim workdays
workdays = array("Mon", "Wed", "Thu", "Fri")

As you can see, the variable does not have to be declared as an array, so theoretically every variable can be set as an array just as every variable can be set as an object. Although, with arrays you don't need the set statement.

Sometimes it is more convenient to dynamically increase and decrease the array size. Then you have to create a dynamic array. This is an array declared the same way as in the simple static array, but without the number of elements, just empty parenthesis: dim myArray()
To set the amount of array items, you have to use ReDim like ReDim myArray(n) where n is the amount of items you want to use +1 (The first subscript is always 0). Other then in a static array declaration (like Dim myArray(5)), the number of subscripts in a ReDim statement can be a variable or constant.
Whenever you use ReDim, the array is reïnitialized, except when you use the Preserve command, indicating you want to reuse the already set values:

' Simple dynamic assigment of an array
Dim myPets()
ReDim myPets(2)
myPets(0) = "Dog"
myPets(1) = "Cat"
myPets(2) = "Hippopotamus"

ReDim Preserve myPets(3)
myPets(3) = "Rabbit"

msgbox "My pets: " & join(myPets, ", ")

The join(array[, separation character(s)]) function lets you easily merge an array to a string.
Another trick is to use join to make an html table easily:
newTableRow = "" & join(arrElements, "") & ""

Multidimensional arrays
A multidimensional array is used to store data or objects in a matrix. You can create virtually create as much dimensions if you want (not really unlimited of course, but keep in mind this good rule with programming: If you have to ask what the limit is for some kind of instance, probably there is something wrong with your design)


' Multidimensional static array, the next code is pure and alone for demonstration purposes
' it is not optimised, maybe even not correct and there are better ways to achieve this
Dim bcCalendar(2100, 12, 31)
Dim dayCounter, maxDay, cYear, cMonth, cDay
dayCounter = 6

For cYear = 0 to 2100
    For cMonth = 1 to 12
        Select Case cMonth
            Case 1,3,5,7,8,10,12    maxDay = 31
            Case 4,6,9,11           maxDay = 30
            Case 2                  maxDay = 28 + abs((cYear mod 400 = 0) or ((cYear mod 4 = 0) and not (cYear mod 100 = 0)))
        End Select

        For cDay = 1 to maxDay
            bcCalendar(cYear, cMonth, cDay) = weekday((dayCounter mod 7)+1)
            dayCounter = dayCounter + 1
        Next
    Next
Next

msgbox "Charles Darwin was born on a " & WeekdayName(bcCalendar(1809, 2, 12))



Passing arrays
Arrays are passed the same way as variables are:

Default: By Reference
With ByRef in the function declaration: By Reference
With ByVal in the function declaration: By Value
With parenthesis around the argument in the function call: always By Value (ByRef in the function declaration is omitted)

Arrays filled with objects
Arrays do not only have to contain variables, they can also contain objects, which is great to make a collection of QTP gui objects, dictionaries, but also class objects to create child classes under a parent.
Arrays can even contain other arrays. This is useful if you want to create a fully dynamic multidimensional array. (Normally, in a multidimensional array, only the last item is expandable with a redim preserve statement.)

December 10, 2008

Performance increase in table lookup functions

Using object properties instead of QTP standard functions will improve the performance of QTP tests significantly. In our case, we often want to lookup the location of a certain value in a WebTable. QTP provides several functions to read out data from a table, but is slow when used iterating the table (like two for loops, one iterating the rows, the embedded second one iterating the columns per row).

Example of a conservative way to do this:

Public Function LocateTextInTable(byRef tbl, textToFind, byRef row, byRef col)

    For row = 1 to tbl.RowCount
        For col = 1 to tbl.ColCount
            If tbl.GetCellData(row, col) = textToFind then
                LocateTextInTable = True
                Exit function
            End if
        Next
    Next

    row = -1 : col = -1
    LocateTextInTable = False
End Function

The crux is this: .GetCellData is not a very fast method and with a table, consisting of 30 rows and 10 columns, this method is iterated up to 300 times in the most worse case scenario (= text not found).

A faster way to retrieve the data is through the Document Object Model (DOM). This allows you to use the more native properties of an object with the sacrifice of some ease of use.

A table consists of row elements and each row element contains one or more cells. We can iterate them just the same way as we did with the function above:

Public Function LocateTextInTableDOM(byRef tbl, textToFind, byRef row, byRef col)

    Dim objRow, objCell

    row = 1 : col = 1

    For each objRow in tbl.object.Rows
        For each objCol in objRow.Cells
            If objCol.Value = textToFind then
                LocateTextInTableDOM = True
                Exit function
            End if
            col = col + 1
        Next
        row = row + 1
    Next

    row = -1 : col = -1
    LocateTextInTableDOM = False
End Function

From our experience, this will increase the performance of the function with a factor 10.
But be aware, there is one big assumption: This function assumes that the row objects (and cell objects) are perfectly iterated from the first row to the last row and in this exact order. Although a For…Each construct cannot guarantee this behaviour, we never encountered an incorrect location.

November 4, 2008

How to make use of Function Pointers in a Keyword Driven Approach


A commonly used automated test methodology is working with user defined keywords. The tester defines the keyword with parameters next to it and the automated testtool processes this keyword and executes the corresponding function.

There are two ways I know of to make this work in QTP, both with advantages and drawbacks.
A "select case" construction gives you flexibility, but with every keyword you have to add a case manually.

Select Case KeyWord
   Case "login" call login()
   Case "enter user" call enterUser()
   Case Else msgbox "Invalid Keyword: '" & KeyWord & "'"
End Select

Using an eval or execute can lead to type mismatch errors. You can circumvent this by putting in an "On error resume next", but this will lead to no error handling at all by the VBScript engine. Any undefined variable normally results in a "Variable is undefined" error, which can be a great help. You are losing that with an "on error resume next" statement.
Also, a execute or eval does not return a value or object, unless you are capturing that with a variable in the string of the execute itself.

On error resume next
Execute ("TestResult = " & KeyWord & "()") ' -> Any error within the processing of the KeyWord will not be trapped.
On error goto 0

In this post, I will show you another way to dynamically assign keywords to functions in a fast way, with trapping of invalid keywords on a soft way (programmatically and not by the VBScript engine), and keeping the normal error behaviour. This can be done by creating a function pointer to the function and verifying if the function pointer is valid. If so, it can be executed as if it is a normal function:

Dim fp

On error resume next
Set fp = GetRef(KeyWord)
On error goto 0

If IsObject(fp) then
   TestResult = fp
Else
   MsgBox "Keyword '" & KeyWord & "' could NOT be mapped to a valid function."
End if


A function pointer can be used just as a normal function. A drawback of VBScript is the use of fixed arguments, but this can be solved as found elsewhere on teh internets: Passing an array or a dictionary object to the function.

Dim aryArguments
Call GetArguments(aryArguments) 'A function to set all arguments into an array
TestResult = fp(aryArguments) 'Pass the arguments to the function pointed to by the function pointer


And a quick example:

Public Function SumSquares(byRef aryArg)
  Dim i, b
  For i = lbound(anyArg) to ubound (aryArg)
     b = b + aryArg(i)^2
  Next i
  SumSquares = b
End Function

Public Function SqrtSquares(byRef aryArg)
  Dim i, b
  For i = lbound(anyArg) to ubound (aryArg)
     b = b + aryArg(i)^2
  Next i
  If b >= 0 Then SqrtSquares = Sqr(b)
End Function

Public Function HandleKeyword(byval keyWord, byRef aryArg)
  Dim fp

  On error resume next
  Set fp = GetRef(keyWord)
  On error goto 0

  If IsObject(fp) then
     HandleKeyword = fp(aryArg)
  Else
     MsgBox "Keyword '" & KeyWord & "' could NOT be mapped to a valid function."
   HandleKeyword = "INVALID KEYWORD"
  End if
End Function

Dim Arguments
Arguments = array(3, 4, 5)

MsgBox HandleKeyword("SumSquares", Arguments) ' -> shows "50"
MsgBox HandleKeyword("SqrtSquares", Arguments) ' -> shows ~ "7.07107"
MsgBox HandleKeyword("SumFactoerials", Arguments) ' -> shows "INVALID KEYWORD"

October 14, 2008

Parenthesis DO matter

One thing I never knew: Parenthesis matter in QTP (and VBScript). And they surely make a difference. I discovered this during debugging a function call. There was different behaviour between these two function calls:
foo(bar)
call foo(bar)

After some research over the internet and in QTP self, I came to the following conclusions: Passing an argument to a function surrounded by parenthesis means: "Protect me" or in other words: treat me as byVal even if it is defined in the function as byRef.

Example:
sub samplesub (a, b) ' a and b are by default handled as byRef
    a = a + b
end sub

And this is happening when we call samplesub:
x = 1
y = 2
z = 4
samplesub x, y
samplesub (z), y
msgbox x ' displays "3"
msgbox z ' displays "4" because z was passed as if it was passed byVal

The same applies when you call a function:

function samplefunc(c)
    c = c^2-1
    samplefunc = (c mod 2 = 1)
end function

q = 8
samplefunc q
msgbox q ' returns 63

' When you accidentally forgot to call:
p = 9
samplefunc(p)
msgbox p ' returns 9, because p is returned byVal

' With call:
r = 10
call samplefunc(r)
msgbox r ' returns 99, because r is returned byRef

' With call and argument protected:
s = 11
call samplefunc( (s) )
msgbox s ' returns 11, s is returned byVal

' And a last example of a function call with multiple argument with combined protection:
call multifunc( (IamProtected), IamUnprotected )

Rules in short:
A sub/function call with an argument in protected mode overrides a byRef in the function.
A sub/function call with an argument in unprotected mode is returned byRef by default unless it is overridden in the function by a byVal.
An literal or const is always returned byVal.

Syntax proposal:
OK, it is ugly, but if you use parenthesis because they are part of the call, you should use them with spaces between the first and last argument and no space between the function:

call f( a, b )

If you want to use arguments in protected mode, you should use no spaces between the parenthesis and the arguments, but do use them between the function/sub and the parenthesis belonging to the function/sub call:

f (a), (b)
or
call f( (a), (b) )