in mustella/src/main/java/marmotinni/DispatchMouseClickEvent.java [46:170]
protected void doStep()
{
Double x;
Double y;
if (hasLocal)
{
StringBuilder script = new StringBuilder();
insertTargetScript(script, target);
script.append("return target.element.getBoundingClientRect().left");
if (TestStep.showScripts)
System.out.println(script);
Object any = ((JavascriptExecutor)webDriver).executeScript(script.toString());
if (any instanceof Long)
x = ((Long)any).doubleValue();
else if (any instanceof Double)
x = (Double)any;
else
{
System.out.println("x is not Long or Double");
x = 0.0;
}
script = new StringBuilder();
insertTargetScript(script, target);
script.append("return target.element.getBoundingClientRect().top");
if (TestStep.showScripts)
System.out.println(script);
any = ((JavascriptExecutor)webDriver).executeScript(script.toString());
if (any instanceof Long)
y = ((Long)any).doubleValue();
else if (any instanceof Double)
y = (Double)any;
else
{
System.out.println("y is not Long or Double");
y = 0.0;
}
x += localX;
y += localY;
}
else
{
x = stageX;
y = stageY;
}
TestOutput.logResult("DispatchMouseClickEvent: x = " + x + ", y = " + y);
// find top-most element
StringBuilder script = new StringBuilder();
script.append("var all = document.all;");
script.append("var n = all.length;");
script.append("for(var i=n-1;i>=0;i--) { ");
script.append(" var e = all[i];");
script.append(" var bounds = e.getBoundingClientRect();");
script.append(" if (" + x + " >= bounds.left && " + x + " <= bounds.right && " + y + " >= bounds.top && " + y + " <= bounds.bottom) {");
script.append(" window.marmotinni_mouse_target = e;");
script.append(" return e;");
script.append(" }");
script.append("};");
script.append("return null;");
if (TestStep.showScripts)
System.out.println(script);
RemoteWebElement mouseTarget = (RemoteWebElement)((JavascriptExecutor)webDriver).executeScript(script.toString());
if (mouseTarget == null)
TestOutput.logResult("DispatchMouseClickEvent: mouseTarget = null");
else
TestOutput.logResult("DispatchMouseClickEvent: mouseTarget = " + mouseTarget.getTagName() + " " + mouseTarget.getText());
try
{
/* a way to halt the test in the debugger. Pick an element and have this code
wait for the title of some element that doesn't have a title. This should
pause the test indefinitely so you can poke around in the debugger then
from the browser console, set the title to "foo"
if (target.contains("DataGrid"))
{
WebDriverWait wait = new WebDriverWait(webDriver, 1000);
wait.until(ExpectedConditions.attributeToBe(mouseTarget, "title", "foo"));
} */
script = new StringBuilder();
script.append("var init = { bubbles: true };");
script.append("init.screenX = ");
script.append(x.toString());
script.append(";");
script.append("init.screenY = ");
script.append(y.toString());
script.append(";");
script.append("window.marmotinni_mouse_target.dispatchEvent(new MouseEvent('mousedown', init));");
if (TestStep.showScripts)
System.out.println(script);
((JavascriptExecutor)webDriver).executeScript(script.toString());
script = new StringBuilder();
script.append("var init = { bubbles: true };");
script.append("init.screenX = ");
script.append(x.toString());
script.append(";");
script.append("init.screenY = ");
script.append(y.toString());
script.append(";");
script.append("window.marmotinni_mouse_target.dispatchEvent(new MouseEvent('mouseup', init));");
if (TestStep.showScripts)
System.out.println(script);
((JavascriptExecutor)webDriver).executeScript(script.toString());
try {
Thread.sleep(1000);
mouseTarget.click();
}
catch (StaleElementReferenceException sere)
{
// eat this if mousedown/up caused the object to go away
}
/*
if (target.contains("DataGrid"))
{
WebDriverWait wait = new WebDriverWait(webDriver, 1000);
wait.until(ExpectedConditions.attributeToBe(mouseTarget, "title", "bar"));
}
*/
}
catch (Exception e1)
{
TestOutput.logResult("Exception thrown in DispatchMouseClickEvent.");
e1.printStackTrace();
testResult.doFail (e1.getMessage());
}
}