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

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
    • background color: The link's background color.
    • color: The link's color.
    • 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:
    • color property to get the color of the text
    • backgroundColor property to get the backgroung color (behind the content of the object)
    • fontSize property to get the font size
    • fontStyle property to get the font style
    • fontFamily property to get the font family
    • fontWeight property to get the font weight
    • 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

  3. 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




Related articles:

18 comments:

Neelambaram.Sirasanagandla said...

Hi very good solution,As in general i know you dont get this solution by lil work unless you do some research .Give us a way to find solutions when this kind of critical problems when we do face while working on qtp

Dmitry Motevich said...

2Neelambaram.Sirasanagandla:
Sorry, I cannot understand you...
What is "lil work"?
What "kind of critical problems" do you mean?

Srikanth said...

I am trying to record a web app. I am getting error: "There is no default browser registered at this machine. Please change the recording options or regiester a default browser" What does this means? Could you please help me out, What proxy setting do i need to change?

Dmitry Motevich said...

2srikanth:
I think the error "There is no default browser registered at this machine" means that there is no default browser registered at your computer :)

Start IE, open Options, tab Programs and click 'Make default' btn

Anonymous said...

Can you veriify teh width, height, top and left in the QTP? thanks!

Dmitry Motevich said...

2 Anonymous:
Yes, I can.

Anonymous said...

ok, can you tell me how?

Anonymous said...

Can you veriify the width, height, top and left for a web page in the QTP? thanks!
And can you tell me how?

Dmitry Motevich said...

Use GetROProperty("...") method of Browser() with:
abs_x, abs_y, height, width.

Anonymous said...

And can you do the same for the objects of that web page.
Can you do more specific?

Dmitry Motevich said...

I recommend to investigate properties of 'currentStyle' object. I provided it in my post.

This object contains style properties such as: backgroundPositionX, backgroundPositionY, height, width.
These are properties of objects, located on webpage.

Anonymous said...

hi, i have to test a webpage and the thing i want to do is to detect every object in the page without having to add them manually.. the idea is to create a script that test the position of every object within the page, and that works for any page.. is this possible? i tried to get the page as an object and use the funtion childobject and nothing.. any idea?

Knowledge Worker said...

Hi

Thank you for your informative blog!.

I have observed that many folks create an object and then access the properties.

For example, in your example code: You have used the following code:
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


However, if we use the following code then the same result can be achieved:
msgbox browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebElement("Welcome to Gmail").Object.currentStyle.color

Is there any reason on why you have created the object first?

Dmitry Motevich said...

@Knowledge Worker,
I like creating of objects, because script becomes more readable.
Code lines are short and understandable.

Also, imagine the following situation. You use this QTP script:
Browser("b").Page("p").Item("i").DoSmth1()
Browser("b").Page("p").Item("i").DoSmth2()
Browser("b").Page("p").Item("i").DoSmth3()

When Item("i") changes to Item("i2"), you have to changes all three lines:
Browser("b").Page("p").Item("i2").DoSmth1()
Browser("b").Page("p").Item("i2").DoSmth2()
Browser("b").Page("p").Item("i2").DoSmth3()

If you use my approach:
objItem = Browser("b").Page("p").Item("i")
objItem.DoSmth1()
objItem.DoSmth2()
objItem.DoSmth3()

you have to change one line only:
objItem = Browser("b").Page("p").Item("i2")
All others lines are the same.

That's all :)

Anonymous said...

Hi Dmitry Motevich,

I am facing some problem to get the text color of the weblist.. can you please help me in this..

How to get a text color from weblist..

thanks in advance..

Regards,
Kumar

karthik said...

Hi Dmitry Motevich,

I have been using QTP 8.2 and now I have started using QTP 9.5. The newer version of QTP does not support the CurrentStyle object. Can you tell me any alternate object to get the background color, font ....

Dmitry Motevich said...

@karthik (February 5, 2009),
QTP 9.5 supports CurrentStyle.
At least, it worked in my scripts.

Dmitry Motevich said...

Dear Readers!
Thank you very much for you comments!

Since this article was published more than one year ago, I've just disabled an adding of new comments for the article.