When the XBrowser tech domain is loaded, it prevents the recognition of the MenuItem controls of a context menu. Using Silk4J we can script around this limitation by unloading the XBrowser tech domain and loading only the WIN32 tech domain. The following sample code demonstrates how we can create a method that will invoke a context menu at the current mouse location and then select a MenuItem control from that menu:


public void openContextSelect(int xPos, int yPos, String sItem){

//load the win32 tech domain only
desktop.detachAll();
desktop.attach("*\\IEXPLORE.EXE", TechDomain.WIN32);
com.borland.silktest.jtf.Utils.sleep(5000);

//Find the Browser and open menu
Window wMainWin = desktop.find("//Window[@caption='*Windows Internet Explorer']");
wMainWin.openContextMenu(new Point(xPos, yPos));

//select the item
Control cFP = desktop.find("//Window[@caption='*Windows Internet Explorer']//Control[@caption='*Windows Internet Explorer']");
cFP.find("//MenuItem[@caption='*"+sItem+"*']").click();

//reload relevant tech domains
desktop.detachAll();
desktop.attach("*\\IEXPLORE.EXE", TechDomain.WIN32, TechDomain.XBROWSER, TechDomain.FLEX);

//Allow tech domains to reload
com.borland.silktest.jtf.Utils.sleep(5000);

}


To use the above method, we would first have to move the mouse over the control on which we want to invoke the context menu and then call the above method:


@Test
public voidinteractWithMenu() {

desktop.find("//BrowserApplication").setActive();

desktop.find("//BrowserApplication//BrowserWindow//FlexApplication[@caption='explorer']//FlexButton[@caption='Button with Icon']").mouseMove();

openContextSelect(MouseInfo.getPointerInfo().getLocation().

x, MouseInfo.getPointerInfo().getLocation().y, "MyMenuItem");

desktop.find("//BrowserApplication//BrowserWindow//FlexApplication[@caption='explorer']//FlexButton[@caption='Button with Icon']").click();

}


When determining the x and y position of the mouse the MouseInfo class is used, this class is provided in the java.awt package.