HP QTP 10: Web-app testing - SomeObj.FireEvent("OnCLick") works, SomeObj.Object.FireEvent("OnCLick") doesn't
I have rich web app btuil with ExtJS. It has multi-select list control (created with JS+CSS). I want to click on some item in that list using HP QuickTest Pro 10 with Internet Explorer 6. I added that item into Object repository and found that following code works - selects some item:
Browser("blah").Page("blah").WebElement("SomeElem").Click
next code also works:
Browser("blah").Page("blah").WebElement("SomeElem").FireEvent("onMouseDown")
Browser("blah").Page("blah").WebElement("SomeElem").FireEvent("onMouseUp")
Browser("blah").Page("blah").WebElement("SomeElem").FireEvent("onClick")
But I want to select several items using shift+click method. I don't know to do that :( So I have a few questions:
- How can perform click with mouse on several web elements with Shift key pressed?
I tried to do that using CreateEventObject + shiftKey set to true, but the method (perform fireEvent on DOM object, not object from Object repository) doesn't work:
Browser("blah").Page("blah").WebElement("SomeElem").Object.FireEvent("onClick")
What the difference between WebElement("Element").FireEvent("OnClick") and WebElement("Element").Object.FireEvent("OnClick") ?
Plsease, help someone, because I fought with that problem a lot, but had no r开发者_开发知识库esult.
Thanks!
There are several differences between WebElement(...).FireEvent
and WebElement.Object.fireEvent
the first runs a QTP function which performs all sorts of work and the second directly goes to the DOM element. Here is a list of a few things that QTP does which aren't done in the native DOM method (there may be more).
- If the element isn't visible QTP makes it visible (this includes activating the tab and scrolling into view)
- QTP support device replay in which case it will move the cursor and simulate a real click (this gives you a better simulation of how real people interact with the application)
- QTP writes to the report that it performed a
FireEvent
step
As for your problem, in general you can use the native DOM method in order to pass an event object but when I tried it I couldn't get it to work from QTP
set doc = Browser("B").Page("P").WebElement("W").Object.ownerDocument
set ev = doc.createEventObject()
ev.shiftKey = True
Browser("B").Page("P").WebElement("W").Object.FireEvent "onclick", ev
However something equivalent does work when run from the browser.
Starting in QTP11 there's support for running JavaScript files from the script using Page("P").RunScript
until that you can simulate this ability manually. The following snippet works for me (I use the object from the repository to get the IE specific uniqueID
of the element so I don't have to identify it again).
' Use QTP's object identification instead of reproducing the logic in JavaScript
id = Browser("B").Page("P").WebElement("W").Object.uniqueID
' Construct JavaScript script
script = "var e = document.createEventObject(); e.shiftKey = true; " & _
"document.getElementById('" &id & "').fireEvent('onclick', e);"
' Run it on the browser
Browser("B").Page("P").Object.parentWindow.eval script
If all else fails you can try writing a small web-extensibility project and support this functionality.
精彩评论