Friday, August 26, 2011

QTP - How to get the color of an object using QTP?

One quick answer for this question what I heard/read was to use GetRoProperty("Color"). But when noticed carefully, using this method always returns a value #0000ff no matter what the actual color of the object(say for example a link) is. #0000ff is an RGB value for Blue color. Most of the times the color of a link would be blue and using this would pass your test case. But what happens when you have a link in a color other than Blue?

Take a look at the following Screen Shots:


Example 1:





Example 2:



Also the same method can’t be used against web elements as you wouldn't find any color property in the list of available properties for web elements when you do an object spy on that element.





So to get the accurate results, it is always recommended to use Object.Current Style method to get the color of any object using QTP. 

How do you use this method?

See the below code:

Dim oColor

oColor= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.color

msgbox oColor

Now assume that the color of the link changes when hovered on that link, how do you get that changed color?





To do this you need to switch QTP replay mode to Mouse replay mode, fire an OnMouseOver event, get the color of the object using current style method, and then switch back the QTP reply mode Event Replay mode. Please look at the following code:

Dim oColor, oColorMouseOver

oColor=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 2

Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").FireEvent "onMouseOver"

oColorMouseOver=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 1

msgbox "Color of the object is: " & oColor & ". And the Color of the object on MoouseOver is: " & oColorMouseOver




You can also use this CurrentStyle Object method to get the FontSize, FontStyle, BackGroundColor etc., in the following way:


oFontSize= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontSize
oFontStyle= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontStyle
oBackGroundColor= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.backgroundcolor