I am working on a browser automation framework, one of the main tenets of which is to interact with the browser as closely as possible to the way a user would without requiring the browser window to have focus. In order to simulate clicking on, hovering on, and dragging and dropping of elements, I need to perform those events using native OS-level mechanisms rather than simulated JavaScript events. To that end, I’m looking for the best method for getting IE to respond properly to the simulated mouse events.
Using JavaScript or COM methods to simulate the mouse input is problematic as they may block if, for example, a JavaScript alert() or a file selection dialog is displayed.
Using the SendMessage API is unreliable. This Raymond Chen blog post talks about keyboard input, but is equally applicable to mouse input. Using SendMessage (or PostMessage) may appear to work at first, but things like hovering on elements will not work for more than a fraction of a second.
The SendInput API is designed to cleanly simulate input to the mouse and keyboard event input queues, but does so at a very low level. As such, it requires the IE window to have focus in order to receive the mouse input.
Is there a reliable way to simulate mouse input in IE using OS-level events without requiring the window to have focus? If the answer truly is, “No, there is no way to accomplish what you want,” I would appreciate a pointer to an authoritative source describing why this is so for IE.
If your testing is going to require modifier keys like ALT and CTRL then SendInput is the only way. Raymond’s article makes this quite clear.