If you need to trigger mouse-clicks on a disabled controls element, recording such a script would be a pushover; however, when executing the script, TestComplete will output the following error message to the log: The window is disabled. The action cannot be executed.
In this recipe, we will consider emulating a mouse-click on the disabled controls elements, following an example of button A in the calculator.
Launch the Calculator Plus application in the Scientific mode (navigate to View | Scientific).
To click on the disabled controls element, it is necessary to perform the following steps:
function testClickDisabledButton() { var wCalc = Sys.Process("CalcPlus").Window("SciCalc"); wCalc.Window("Button", "A").Click(); }
function clickDisabledObject(obj) { var x = obj.ScreenLeft + obj.Width/2; var y = obj.ScreenTop + obj.Height/2; Sys.Desktop.MouseDown(VK_LBUTTON, x, y); Sys.Desktop.MouseUp(VK_LBUTTON, x, y); }
OnLogError
event.if(aqString.Find(LogParams.MessageText, "The window is disabled") > -1) { Log.Message("The window is disabled"); var objText = LogParams.AdditionalText.split(" ")[1]; var obj = eval(objText); clickDisabledObject(obj); Log.Event("Disabled object was clicked", objText); LogParams.Locked = true; }
testClickDisabledButton
function again, clicking on the A button will be reproduced, and the log would contain two messages posted by using the Log.Event
and Log.Message
methods. There would be no errors in the log.Standard TestComplete means do not allow for a mouse-click on the disabled controls element: this is why, to resolve the task, we have made use of these methods:
Sys.Desktop.MouseDown
and Sys.Desktop.MouseUp
. These two methods trigger the mouse-click and release the assigned mouse-button (in our case, the left one that is, VK_LBUTTON), in the assigned screen coordinates (x and y). To obtain the screen coordinates of the center of the object, we have used the following properties: ScreenLeft
, ScreenTop
, Width
, and Height
.
In the event handler itself, we are checking if the error message contains the following string: The window is disabled. If so, we will get an object targeted for the mouse-click, and pass it as a parameter to the
clickDisabledObject
function.
Finally, with the help of setting the property LogParams.Locked = true
, the error output to the log is blocked.
The process of obtaining the object needs to be explained in more detail. Since we cannot get the object directly from any of the properties of the LogParams
object, we have to retract it from the AdditionalText
property. First, via the split
method, we obtain the second string of the message (this string contains the complete name of the controls element), and then with the help of the eval
function, the text is further transformed to the object (the eval
function transforms the string into the executable JScript code).