In spite of all the advanced properties and methods of QTP, it’s the simple things which often seem impossible.
A good example would be checking the color of a certain element in our application. Usually, we leave these tests to the manual QA team, stating that QTP isn’t meant for graphic verification (As its bitmap checkpoint mechanism is shaky, at best). This QTip will present a more delicate mechanism than a bitmap check - getting the color of a given pixel in the screen.
In order to do this, we shall relay on several Win32 API function calls. You can learn more about using Win32API in the relevant Scripting QTP’s chapter.
First, we declare the relevant functions: GetDC and ReleaseDC for handling graphical contexts, and GetPixel which will return the pixel color code.
Next, we’ll hook up to the desktop main’s display (HWnd = 0), and get its graphic context.
And then we can simply extract the pixel color code, and release the graphical context.
Put together, the entire code looks like this:
Dim hDC, hWnd
Dim iX, iY
'Declare win32 and gdi32 functions
Extern.Declare MicLong, "GetPixel", "gdi32″, "",MicLong, MicLong, MicLong
Extern.Declare MicLong, "GetDC", "user32″,"", MicLong
Extern.Declare micLong,"ReleaseDC","user32″,"",micLong,micLong
'Attach to the desktop graphical context
hWnd = cLng(0)
hDC = Extern.GetDC(hWnd)
iX = 10 'Change this to your relevant X coordinate
iY = 10 'Change this to your relevant Y coordinate
'Get the pixel color code
Print Extern.GetPixel (hDC, iX,iY)
'Release the graphical context
Extern.ReleaseDC hWnd, hDC
And the end result will be a printout of the color code (of course you can store it in a variable and use it to perform whatever validation you see fit).
Posted in Win32



Yaron Assa




May 26th, 2008 at 9:47 am
Really …a useful one.
May 26th, 2008 at 9:50 am
You can thank for avraham mager raising the question
May 26th, 2008 at 11:21 am
Is there any way I could change the screen resolution and color setting at run time?
Thanks,
June 2nd, 2008 at 1:37 pm
I found this article very useful. I used it in a custom highlighting function to determine if the highlight had been erased by a screen refresh.
June 2nd, 2008 at 1:48 pm
Excellent Idea, it’s a feature I never thought possible to validate with QTP.
June 9th, 2008 at 4:25 pm
It is great!
July 1st, 2008 at 9:25 am
we are using .net application.We have to find the row highlighting(red color) in Ultra grid.Please could you suggest