dinsdag 29 juli 2008

Collect performance counters while running QTP tests

Following code can be run using QTP and will collect Memory/Processor/disktime and networkinterface counters while testing.
Useful when doing application profiling on your automated tests.
(copy/Paste to editor of choice to see full code)




Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set PerfProcess = objWMIService.Get("Win32_PerfFormattedData_PerfProc_Process.Name='explorer'")
tmp = ""

set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set objMemory = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Memory").objectSet
Set objMemory2 = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
Set objMemory3 = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfDisk_LogicalDisk").objectSet
Set objMemory4 = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_Tcpip_NetworkInterface").objectSet
objRefresher.Refresh
Do
tmp = Now
For each intAvailableBytes in objMemory
if not isnull(intAvailableBytes) then
tmp = tmp & ";" & intAvailableBytes.AvailableMBytes
end if
Next
For each intProc in objMemory2
if not isnull(intProc) then
tmp = tmp & ";" & intProc.PercentProcessorTime
end if
Next
For each intIO in objMemory3
if not isnull(intIO) then
tmp = tmp & ";" & intIO.PercentDiskTime
end if
Next
For each intIO in objMemory4
if not isnull(intIO) then
tmp = tmp & ";" & intIO.BytesReceivedPersec
tmp = tmp & ";" & intIO.BytesSentPersec
tmp = tmp & ";" & intIO.BytesTotalPersec
end if
Next
MyFile.writeLine tmp
tmp = ""
objRefresher.Refresh
wscript.sleep 1000
Loop

Find the last modified file in a directory - vbscript



Function FindLastModified(strDir)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = objFSO.GetFolder(strDir)

Date = CDate("1/1/1950")

For Each oFile In oFolder.Files
If oFile.DateLastModified > Date Then
NewestFile = oFile.Name
Date = oFile.DateLastModified
End If
Next
FindLastModified = NewestFile
End Function

donderdag 7 februari 2008

Why you Indians never get to date! (and how to fix it!)


At my job we do a lot of off-shoring. As a result we have a lot of Indians running around in the office. You are very nice guys, always smiling, working like a horse being perfect employees and all. But you never seem to get a girl or be in a relationship. I know some of you are actually looking at some of our hot western girls and think 'mmmmmm ... I sure would like to get my hands on one of those'. But ... it never seems to happen!

And you know why? It's so very easy ... because ...

Like you guys reinvented and improved the whole IT business you also reinvented and IMPROVED the whole being a dork lifestyle. Seriously, just now, when everyone had a sigh of relief because our own dorks where finally catching on to the whole dressing thing and being social and all, (man, our own nerds can be seen nowadays in bars and clubs), you come along!

Most of you people look like you just stepped out of the eighties and into the office. I’m really sorry but you’ll never get a girl like that! Ask our homebred dorks, they’ll tell you all about it. So here are some pointers:

1. You can NOT wear dress pants with sports shoes. NO!. Seriously, do not do that. If you wore shoes half in between dress shoes and sports shoes girls could forgive you, but, nooooooo, you are wearing white track shoes, you know Nike air max (yes they still exist!). What? You’re all Olympic 10000 meter runners? Somehow I doubt it. Sports shoes to run, yes! Sports shoes on dress pants! NO! NO! NO! (please)

2. Talking about dress pants, they should be dark in colors, you know brown, black, dark blue. They can not be light blue or even worse, skin color. Where do you guys get those? Really, tell me, where can you actually buy those? The ladies do not like them (understatement). So when you get your next paycheck, go to a shop and buy new pants. But, pleeeaaassseeee, do not pick them yourselves. Go to the shop and ask the lady or gay guy at the counter to dress you. Remember, DO NOT PICK YOUR OWN CLOTHES! However stupid this may sound, you'll pick the exact same clothes you were wearing before, I've seen it happen.

3. LOSE THE MUSTACHE. I know you are probably a hot manly babe magnet with that huge hairy thing in your own country, but over here you are a cop from the eighties. You'll say 'but girls like cops and men in uniform'. Yes they do, but they don't like the eighties and you for one should not talk about uniforms (see point 1 and 2). So, shave, get rid of the whole walrus theme! I know they’re useful for doing the dishes and removing cobwebs at your home, but, just say NO!

4. Talking about hair, there's only so much hair a man can have. You guys have great hair, a lot of hair ... but ... it looks like an elephant took a dump on your head. Girls like a well groomed man! So ... call your local hairdresser (NO! not your local Indian hairdresser, choose another one) make an appointment (Do not forget to mention that you're Indian so that the man can clear his schedule!). Do not ask for something special; just ask him to fix it!

5. So now that you’re a well groomed, smart dressed, face visible sport-shoe-less man, our last step: Be social. You’re really nice people, once we get to know you, but boy, does it take a long time before anyone in the office actually noticed that you are a living breathing human being and not part of company furniture. Talk to people! Most of you look the first weeks as if you’re about to have a stroke or something. Have a chat, have a laugh, your pile of work will not run away you know, it can wait for 5 minutes!

So now you have it all! You’re one lean mean dating machine! Only thing left to do … step up to the hot blonde at the coffee machine and ask her out! And one last thing to give you some extra incentive … it’s true … western girls are really easy !!!

Happy grooming !

And no! There are no special occasions where sports shoes on dress pants are possible! None!

woensdag 6 februari 2008

Adding a live progress log to your test automation

Just an example how you can implement a live progress log shown in an IExplore window. Now you can follow your test prgress live on screen!

Proof of concept.

VBScript:

------------------

Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=400
objExplorer.Height = 200
objExplorer.Left = 0
objExplorer.Top = 0

Text = "***** Progress Log *****"

Do While (objExplorer.Busy)
Wscript.Sleep 200
Loop

objExplorer.Visible = 1
objExplorer.Document.Body.InnerHTML = Text

For i = 1 to 10
add_log_line "Logline: " & i,1
Wscript.Sleep 200
Next
add_log_line "Done!",1


Wscript.Sleep 10000
objExplorer.Quit

Public Function add_log_line(line,nl)

if nl then
Text = Text & "<BR>" & line & "<BR>"
else
Text = Text & line
end if
objExplorer.Document.Body.InnerHTML = Text

End Function


--------------------

Schedule/execute testset run in Quality Center from Windows (VBScript)

You want to schedule and execute an automated test run every day at a certain hour. You need your results saved in QC. Here's a simple solution for scheduling by using following script in the windows scheduler.

-----------------
VBScript:

Dim QCConnectionSet
QCConnection = CreateObject("TDApiOle80.TDConnection")


QCConnection.InitConnectionEx "server_address" ' <--------------- Fill in server addressQCConnection.login "user", "pwd" '<------------------- FILL in user/pwd
QCConnection.Connect "domain_name", "project_name" '<----------- FILL Domain/Project

Set TSetFact = QCConnection.TestSetFactory

Set tsTreeMgr = QCConnection.TestSetTreeManager
nPath = "Root\test" '<------------- FILL IN PATH TO TESTSET

Set tsFolder = tsTreeMgr.NodeByPath(nPath)
If tsFolder Is Nothing Then

msgbox "error"
End If

Set tsList = tsFolder.FindTestSets("name_of testset") '<------- FILL IN NAME OF TESTSET

If tsList.Count > 1 Then
MsgBox "FindTestSets found more than one test set: refine search"
ElseIf tsList.Count <>
MsgBox "FindTestSets: test set not found"
End If

Set theTestSet = tsList.Item(1)

Set Scheduler = theTestSet.StartExecution("")
Scheduler.RunAllLocally = True
Scheduler.HostTimeOut = 100000

Scheduler.runSet execStatus = Scheduler.ExecutionStatus
RunFinished = False
while RunFinished = False
execStatus.RefreshExecStatusInfo "all", True
RunFinished = execStatus.Finished
wend
QCConnection.Disconnect
QCConnection.Logout
Set QCConnection = Nothing

---------------

Very crude! but it does the job! You just need an existing testset, the script will execute it at any time you want using your windows scheduler.
I'm sorry for the absolute lack of comments, but you're smart, you can figure it out. :)

Accessing IExplore from VBScript

So you want to access a Iexplore window that's already opened and on the desktop. Here's some sample code (VBScript):

This code will give a msgbox showing the URL for every open IExplore window.

----------

Set objShell = CreateObject("Shell.Application")
Set IEWindows = objShell.Windows

Dim IEWindow()

ReDim IEWindow(IEWindows.Count)
Dim i

msgbox IEWindows.Count

For i = 1 To IEWindows.Count

Set IEWindow(i) = IEWindows.Item(i - 1)
Next

For i = 1 To UBound(IEWindow)

msgbox IEWindow(i).LocationURL
Next

----------

Now you know how to connect to a IExplore window you can access all controls inside by programming the DOM (Document object model). It's a fairly easy model, look it up, google is your friend! :)

LoadRunner - Putting a double/float into a parameter

It took me two hours to figure it out, so I might as well share with the world. To put a 'double' or 'float' value in a new defined parameter in Loadrunner use following code:

---------

float nr;
nr = 0.01;
char strNr[4];
sprintf( strNr, "%1.2f", nr );
lr_save_string( strNr, "param_nr" );
lr_output_message("We are on nr #%s", lr_eval_string("{param_nr}"));


----------

output should be: We are on nr 0.01

PS: conversions to string from other datatypes are hell in C. (just venting)