QTP: Get Link’s Mouse Over (Hover) Color

by Anshoo Arora ON November 5, 2009 · Posted In All, QTP, QTP/Web · 39 comments

This post describes a simple technique that can enable retrieval of a Link’s onMouseOver color. The only trick is to change the ReplayType setting to Mouse Events while firing an onMouseOver event on the link. Changing the event cause an actual FireEvent action, that you would encounter when you hover the mouse on the link manually.

Below, you will see that the hover event is triggered with the ReplayType set to Mouse Events:

Setting.WebPackage("ReplayType") = 2
Browser("Browser").Link("Link").FireEvent "onMouseOver"
MsgBox Browser("Browser").Link("Link").Object.currentStyle.color
Setting.WebPackage("ReplayType") = 1

The above snippet can be simplified and be reused using the function below:

Function GetHoverColor(oLink)
    Setting.WebPackage("ReplayType") = 2
        oLink.FireEvent "onMouseOver"
        GetHoverColor = oLink.Object.currentStyle.color
    Setting.WebPackage("ReplayType") = 1
End Function
 
RegisterUserFunc "Link", "GetHoverColor", "GetHoverColor"

Now, whenever the link color is to be retrieved, you can simply call the “GetHoverColor” function as you would normally use the Click event:

MsgBox Browser("Browser").Page("Page").Link("Link").GetHoverColor

I hope you find this useful.

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Leave a Comment

{ 39 comments… read them below or add one }

Sophia February 28, 2012 at 1:14 am

Hello! i found this post relevant to my problem. Actually I want to grab the tool tip text of a JavaButton when mouse is hovered over the button, in a java application. Can you guide me how to do that? I have tried following things but nothing works;
. JavaWindow(“Main Window”).JavaButton(text).FireEvent micMouseEnter, 0,0 // I am not sure of the arguments here, neither could I find the details over web.
. ToolTip1 = JavaWindow(“Main Window”).JavaButton(text).GetROProperty(“alt”) //Does not work either
. JavaWindow(“Main Window”).JavaButton(text).FireEvent “onmouseover” // this one does not work either also tell me where is this event defined? I could not find it in documentation.

I also tried adding the a property and its value to the object in object repository but in that case QTP does not identify the object as it is changed.

Reply

Anshoo Arora March 15, 2012 at 8:29 am

Sophia, sorry, never had the chance to do this. Do you have a demo app that I can test this on?

Reply

Mini February 12, 2012 at 10:01 pm

Can anyone tell me how to automate hover behaviour in a web page using QTP?

Reply

Abhilash January 24, 2012 at 2:27 am

Hi,
Anshoo Arora

can u tell me how i can use the data i receive from mouse event to be validated instead of printing it???

example if i want Reporter.ReportEvent to fail if color is black and pass if color returned is orange….

Thanks
Abhilash

Reply

Anshoo Arora February 13, 2012 at 11:10 am

Abhilash, you can do so by using a conditional (If..Then..Else) statement.

If color = "orange" Then
    Reporter.ReportEvent micFail, "Orange", "Invalid color"
Else
    Reporter.ReportEvent micPass, "Not Orange", "Valid color"
End If

Reply

ankit December 12, 2011 at 6:38 am

Hi Anshoo,
How to click on a sublink which becomes visible when mouse is put over the parent link.
Like in your website first fire a onmouseover event for Articles link(this part I am able to do) then select and click on any of the sublink.

Thanks
Ankit

Reply

ankit December 5, 2011 at 9:37 am

Hi Anshoo,
How to click on a sublink which becomes visible when mouse is put over the parent link.
Like in your portal first fire a onmouseover event for Articles link(this part I am able to do) then select and click on any of the sublink.

Thanks
Ankit

Reply

Anshoo Arora December 27, 2011 at 6:58 am

Ankit, the way I generally do this is not visible to the user but the link is clicked. If you want to make it visible, then I guess you can consider changing the replaytype:

Setting.WebPackage("ReplayType") = 2

Browser("title:=Relevant Codes.*").Link("innertext:=Articles").FireEvent "onmouseover"
Browser("title:=Relevant Codes.*").Link("innertext:=VBScript", "index:=0").Click

Setting.WebPackage("ReplayType") = 1

Reply

SAM May 17, 2012 at 7:21 am

Hi Anshoo,

First …Thanks for sharing the techniques of hovering and replayType details ..

@ Ankit Query :- The script may NOT be hovering and then selecting sublink and click on it if there are multiple links in that webpage with same names.
So when you use index to click the desired link it does NOT require to hover the mouse on the parent LINK.

So I think QTP cannot identify the sublinks drop down list which gets enabled only when Mouse is hovered over Parent Link. Please let me know if there is any workaround for this. So it does NOT treat the items in this list as Listitems but considers as independant links

Thanks,
SAM

Reply

Maruti August 10, 2011 at 3:44 pm

Dear Sir,
I am automating a web application and I need to find the color of data which is in a WebTable. When I am using Object Spy, the cell inside the table identifying as a webelement.

I used the below code retreive the colror, but while running it is not identifying the object. I used DP as well.

Set objWebEl= Browser().Page().WebTable().ChildItem(2,2,”WebElement”, 0)
Print objWebEl.Object.currentStyle.color

I tried searching in google, I didn’t get anything useful. Please advice.

Thanks,
Maruti

Reply

Anshoo Arora August 16, 2011 at 8:45 pm

Maruti, try this:

Print Browser("").WebTable("").Object.rows(0).cells(0).currentStyle.backgroundColor
'or
Print Browser("").WebTable("").Object.rows(0).cells(0).currentStyle.color

Reply

siva June 24, 2011 at 3:59 am

in my application i need to select the dropdown trough mouse, guys if any body knows send mail to me.

Reply

Anshoo Arora June 24, 2011 at 11:19 am

Siva: Why this requirement? It can be done as well using other methods..

Reply

vikramjit March 14, 2011 at 6:18 pm

Hi Anshoo I figured that problem out .Thanks
Now I am facing another problem ,, I am getting data from a row in webtable and it is returning “59 782″.Can you tell me how I can separate these numbers like n1=58 and n2=782.I mean I want to convert string coming out of the webtable to numbers .Its really urgent .
Thanks
Vikramjit Singh

Reply

Prasant March 14, 2011 at 8:06 pm

Vikramjit,

split the return value using SPLIT { Split(“58 782″, ” ” ) } method with space as a delimiter and then typecast using the CINT { n1 = Cint(arr(0)) and n2 = Cint(arr(1)) }

Reply

bhawna March 7, 2011 at 7:50 am

Hi Anshoo
I am working on automating a website using descriptive programming.I am using object.onmouseover (“link”) to highlight 7sublinks.actually i want click on that sublink through DP. How could i do this .
Please help me

Reply

Prasant March 14, 2011 at 8:23 pm

After the sub link menu displays, learn it (it may be learned as a table) or again describe this sub menu object. Then find the required item in that and try to click that object.
Hope it will work

Reply

Vikramjit Singh February 23, 2011 at 6:48 am

Hi anshoo
Object.click works and shows sublinks but does not click on the sublinks(does not open the sublink pages)
Thanks

Reply

Anshoo Arora February 28, 2011 at 7:27 am

Did changing the ReplayType help?

Reply

vikramjit February 21, 2011 at 12:08 pm

Hi I am working on automating a website using descriptive programming.I am using object.onmouseover (“link”) to highlight 3 sublinks.I am successful in that till now.But now I am facing problem to click on the sublinks and go over the relevant page as clicked on the sublink.Kindly help me to solve my problem.I appreciate your response
Thanks
Vikramjit

Reply

Anshoo Arora February 23, 2011 at 2:17 am

Vikramjit, does Object.Click work? If not, can you try changing the ReplayType setting and clicking on the link?

Reply

Robin October 21, 2010 at 12:00 am

Anshoo,
You are great in scripting. I love to read your columns. Could you please start one topic for QTP Small Automation Tips.

Thanks!
Robin

Reply

Suresh Bhandari September 20, 2010 at 9:24 pm

Hi Anshoo,
Could you pls tell us, what is the use of below line of code
“Setting.WebPackage(“ReplayType”) = 2/1″

Reply

Anshoo Arora September 29, 2010 at 3:05 pm

Suresh,

The ReplayType tells QTP the type of events it has to use when working with a Browser application.

ReplayType = 2 = Mouse events
ReplayType = 1 = Browser events

Reply

Ralf August 30, 2010 at 5:44 am

Hi Anshoo,
how to do this with FF 3.5?

QTP 10.0 sends an Runtime-Error:
Line (83): “GetHoverColor = Link.Object.currentStyle.color”.

I have installed Patch QTPWEB_00046 – Add Support For Firefox 3.5
by the way, is their an Way to use FF 3.6?

best regarts
Ralf

Reply

Anshoo Arora September 2, 2010 at 7:42 am

Ralf,

Not sure if this will work in Firefox as it does not support the concept of .Object (yet). We’re primarily dealing with COM here, but the underlying technology of Firefox is different. I agree its very similar to COM, but its not exactly that – its XPCOM.

Reply

Mahalakshmi August 18, 2010 at 6:47 am

To select from the WebList box I am changing the Setting.WebPackage(“ReplayType”)=2 selecting the required item from the list and then on trying to reset the replay type to 1, QTP crash occurs..

Reply

naveen March 11, 2010 at 12:19 am

Hi, Please Let me know how to read functions from DLLs we currently have .vbs files and we want to make them as DLLs and use the functions .Can u please let me know how to convert the.VBS files into dlls where we can hide the code from others with out loosing the flexibility to use the functions.

Reply

Anshoo Arora March 11, 2010 at 3:59 pm

Hi Naveen,

Please see this article. Any public method/property/variable can be accessed once you load the DLL through DOTNetFactory.

Reply

Raj February 18, 2010 at 6:08 pm

Great articles…. Keep it up

Reply

Anshoo Arora February 19, 2010 at 2:13 am

Thanks Raj :)

Reply

Rajesh January 28, 2010 at 2:05 pm

Its really nice. Can you please share few articles related to the below problem.

when I fire a mouseover event, I get a complete structure of a table. how to retrive the data from this. Can you Please share this info. thank u

Reply

Anshoo Arora January 29, 2010 at 1:47 pm

Hi Rajesh,

I’m not sure if I understand the scenario quite well. The table structure is retrieved when you onMouseOver it? What data are you trying to retrieve from the table? If possible, could you attach or e-mail me the HTML source of the document?

My email is anshoo.arora [at] relevantcodes.com

Reply

Selvam V November 11, 2009 at 2:40 am

Simple neat and very nice….

Reply

Anshoo Arora November 11, 2009 at 4:46 pm

Glad you liked it, Selvam :)

Reply

Rajiv Kumar Nandvani November 9, 2009 at 2:16 am

Hi Anshoo,

Today i go through from Your blog in goggle search.
realy you are doing great job.
keep it up

Reply

Anshoo Arora November 9, 2009 at 4:29 pm

Thank you, Rajiv. I hope you like reading the articles here :)

Reply

Anonymous February 18, 2010 at 11:57 pm

Thank you
Anshoo

The problem is now resolved. I learned the Table and is working good.

Reply

Anshoo Arora February 19, 2010 at 2:13 am

Great! :)

Reply

Previous post:

Next post: