I want to have java script clicking a link on the page..I found something on the net that suggests adding a function like this:
function fireEvent(obj,evt){
Try this if you are still getting error (Use of Jquery + prototype)
function fireEvent(element,event){
if (document.createEventObject){
// dispatch for IE
try {
var evt = document.createEventObject();
jQuery(element).change();
return element.fireEvent('on'+event,evt);
} catch(e) { }
}
else{
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}
I think you still need to call document.createEventObject -- you only checked that it's there. Untested code follows, but based on the docs it should work.
function fireEvent(obj,evt){
var fireOnThis = obj;
if( document.createEvent ) {
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( evt, true, false );
fireOnThis.dispatchEvent( evObj );
} else if( document.createEventObject ) {
var evObj = document.createEventObject();
fireOnThis.fireEvent( 'on' + evt, evObj );
}
}
If you want to simulate clicks only for links, you can use this:
function clickLink(id){
location.href=document.getElementById(id).href;
}
We found a simpler way to simulate right click (tested in IE8). Use a double click or two single clicks, then right-click using Shift-F10. I don't know exactly why this works, but it does. In the sample code below, we use the Selenium method to click twice, then use the IE Driver to find the WebElement and send the key sequence Shift-F10, which emulates the right-mouse button click. We use this to testing GWT based web apps. One place this did not work as well was in a tree control where the context menu was set to appear at the mouse's coordinates. Often, the mouse coordinates were negative, so right-clicking the menu item did not cause the child menu options to get displayed. To handle this case, we added a bit of code to the control so that if the mouse coordinates were negative, the context menu appears at a 0,0.
selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
new InternetExplorerDriver().findElement(By.xpath("//td[@role='menuitem' and contains(text(), 'Add')]")).sendKeys(Keys.SHIFT, Keys.F10);
This didn't work for me at first and then I saw the code is missing a parameter for the IE portion. Here's an update that should work:
function fireEvent(obj, evt) {
var fireOnThis = obj;
if (document.createEvent) {
// alert("FF");
var evtObj = document.createEvent('MouseEvents');
evtObj.initEvent(evt, true, false);
fireOnThis.dispatchEvent(evtObj);
}
else if (document.createEventObject) {
// alert("IE");
var evtObj = document.createEventObject();
fireOnThis.fireEvent('on'+evt, evtObj);
}
}