You are on page 1of 4

QTP - How to get font size/color, background color and other

attributes of controls

Today I faced with the following task with QTP (QuickTest Professional) - how to
get some attributes (such as: font size, font color, background color and so on)
for any control on a web page?

Piece of cake! :)
Let's see ways how it can be done.

The task: get font size, font color, background color and others possible
parameters from the gmail.com start page:

The solution:
1. First of all, I tried to use GetROProperty method.
I selected "Welcome to Gmail" label with QTP object spy and added them
to Object Repository:

Well, now I know


the class of web control - WebElement.

Then, I open QuickTest Professional Help, search for "WebElement


Identification Properties" and see that there are not needed properties
(font name, font color, size, ...).

Help reading shown that it is possible to get needed properties for some
objects, for example, for Link. Please, see "Link Identification Properties"
from the QTP Help:

Property Name: Description

o background color: The link's background color.


o color: The link's color.
o font : The link's font.

So, these properties work correctly for Link. For example, the following
code:
Browser("Welcome to Gmail").Page("Welcome to
Gmail").Link("About Gmail").GetROProperty("color")
returns value #0000ff for "About Gmail" link:

In terms of RGB (red-green-blue), the value #0000ff means that blue-


color is enabled.
It looks like truth :)

This approach (GetROProperty method) has a limitation - it can be


applied for some objects. In my case (I use WebElement object) this
methods cannot be applied.

Thanks to Mr. Google :) It found an appropriate solution:

2. currentStyle object!
The main idea is to read:
WebElement("SomeName").Object.currentStyle.someProperty
For example, use:
o color property to get the color of the text
o backgroundColor property to get the backgroung color (behind
the content of the object)
o fontSize property to get the font size
o fontStyle property to get the font style
o fontFamily property to get the font family
o fontWeight property to get the font weight
o and so on

Please, read more detailed info on properties of currentStyle object:

So, I used this code in my QTP script:

Dim ctrlWebEl, objWebEl

Set ctrlWebEl = Browser("Welcome to Gmail").Page("Welcome


to Gmail").WebElement("Welcome to Gmail")
Set objWebEl = ctrlWebEl.Object

sColor = objWebEl.currentStyle.color
sBackgrColor = objWebEl.currentStyle.backgroundColor
sFontSize = objWebEl.currentStyle.fontSize
sFontStyle = objWebEl.currentStyle.fontStyle
sFontFamily = objWebEl.currentStyle.fontFamily
sFontWeight = objWebEl.currentStyle.fontWeight

Result is:

The last thing I have to do is to get a numerical value of background color.


For that I recorded another object - WebElement("corner_tl"):

Note: when recording, click at the right of "Welcome to Gmail" text.

I executed my QTP script for that WebElement and got results:

Background color is #c3d9ff


Now, I think, mission is completed :)
All attributes (font size/color/weight, backgroung color) are gathered.

Summary: The simple way to get different attributes of controls from QTP is a
usage of currentStyle object .
Enjoy QuickTest Professional :)

Dmitry Motevich

You might also like