While I'd like to be able to do it in Internet Explorer or Firefox as well, the main usage will be HtmlUnit. It's nice that the HtmlUnit HtmlElement has a rightClick()
method, but unfortunately it's protected
and so not accessible from the WebDriver wrapped HtmlUnitWebElement.
I wrote a hack to make it accessible, and so now I can call rightClick(), although it only works if it's running with HtmlUnit - not IE or FF.
// Needs to be in this package to get access to the element
package org.openqa.selenium.htmlunit;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
public class OpenHtmlUnitWebElement extends HtmlUnitWebElement {
// Provide a constructor, even though we don't really need it.
public OpenHtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) {
super(parent, element);
}
// this is the method we really want.
public static HtmlElement using(HtmlUnitWebElement huwe) {
return huwe.element;
}
}
Now my (groovy) test looks like this:
import static org.openqa.selenium.htmlunit.OpenHtmlUnitWebElement.using
...
def itemWithContextMenu = driver.findElement(By.id('theId'))
if (itemWithContextMenu instanceOf HtmlUnitWebElement) {
using(itemWithContextMenu).rightClick()
def contextMenu = driver.findElement(By.id('jqContextMenu'))
assert ...
}