What are the various methods to rightclick on a webelement in QTP?

Srittam picture Srittam · Dec 11, 2012 · Viewed 16.9k times · Source

I had to right click on a webelement. I tried the following methods:

For code simplicity assume webElem refers to a valid Browser().Page().WebElement().

1)

Set obj=createobject("mercury.devicereplay")

getX = webElem.GetROProperty("abs_x")
getY = webElem.GetROProperty("abs_y")


'obj.MouseClick getX, getY,RIGHT_MOUSE_BUTTON

THE ABOVE CODE DID NOT WORK

2)

webElem.RightClick

THIS CODE DID NOT WORK EITHER

3) FOLLOWING CODE WORKED:

Setting.WebPackage("ReplayType") = 2

webElem.RightClick

Setting.WebPackage("ReplayType") = 1

My questions are:

  1. Why did I have to change the device replay type to make rightclick work? Where as, click method works fine.

  2. Why, through Mercury.DeviceReplay object, the MouseClick method did not work?

  3. How can I do this through FireEvent method? (please explain FireEvent method in detail, FYI: I am new to QTP)

  4. What are the other methods to do this?

Could anyone please explain whay some methods work and some doesnt.

Thanks, Srittam

Answer

Motti picture Motti · Dec 11, 2012

By default QTP runs most steps on web elements using event replay this means that it sends DOM events to the underlying DOM element. When a person interacts with the web page the browser generates many DOM events (e.g. mousemove mousedown contextmenu) and the web application may depend on any of these events in order to trigger the required behaviour. QTP does not know which events each web application will use and thus reproduces the subset that QTP's developers thought are interesting (for example for Click QTP replays mousedown, mouseup and click. For RightClick there is also a contextmenu event somewhere (I don't remember the order) and there is no click.

The probable reason that what you tried in #2 failed is because there are additional events that the application expects which weren't created by QTP.

When you specify ReplayType=2 (as in case #3) you tell QTP's web package to use device replay this means that it physically moves the mouse to the requested location and performs the operation. This means that the browser thinks a real person is moving the mouse and will reproduce exactly the same events as it would when a human being is performing the operation.

You should have gotton exactly the same results with case #1 (explicitly using DeviceReplay). I can think of two reasons why it still failed.

  1. In your code sample you have the line 'obj.MouseClick getX, getY,RIGHT_MOUSE_BUTTON commented out, this is probably a copy/paste error but if this is also what you tried to run it would explain why nothing happened.
  2. A more likely explanation is that you're using abs_x and abs_y to replay the click, this is the top left corner of the WebElement and perhaps this doesn't reach the correct DOM element. The way that RightClick in device replay mode differs from your implementation is that it clicks in the middle of the WebElement (by default) try getting the width and height of the element and doing: obj.MouseClick getX+(width/2), getY+(height/2), RIGHT_MOUSE_BUTTON

There is another way to perform a right click and that would be:

webElem.Click micNoCoordinates, micNoCoordinates, micRightBtn

But I would recomend against it since webElem.RightClick should work at least as well if not better.