protected void doStep()

in mustella/src/main/java/marmotinni/DispatchMouseEvent.java [40:121]


    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;
		}
		
		// 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("         if (!e.id) e.id = Math.random().toString();");
        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());
		//System.out.println("mouseTarget: " + mouseTarget.getTagName() + " " + mouseTarget.getAttribute("id"));
		String actualId = mouseTarget.getAttribute("id");
		
		script = new StringBuilder();
		script.append("var e = document.createEvent('MouseEvent');");
		script.append("e.initMouseEvent(\"" + type.toLowerCase() + "\", true, false, window, " + delta + ", " + x + ", " + y + ", " + localX + ", " + localY + ", " + ctrlKey + ", false, " + shiftKey + ", false, " + type.equals("mouseDown") + ", " + relatedObject + ");");
		script.append("document.getElementById('" + actualId + "').dispatchEvent(e);");
		if (TestStep.showScripts)
			System.out.println(script);
        try
        {
			((JavascriptExecutor)webDriver).executeScript(script.toString());
        }
        catch (Exception e1)
        {
            TestOutput.logResult("Exception thrown in DispatchMouseEvent.");
            testResult.doFail (e1.getMessage()); 
        }
		
    }