Clicking on disabled controls without an error message

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.

Getting ready

Launch the Calculator Plus application in the Scientific mode (navigate to View | Scientific).

How to do it...

To click on the disabled controls element, it is necessary to perform the following steps:

  1. First of all, let's write up some code to trigger a mouse-click on the A button (executing this code will lead to the previous discussed error in the log):
    function testClickDisabledButton()
    {
      var wCalc = Sys.Process("CalcPlus").Window("SciCalc");
      wCalc.Window("Button", "A").Click();
    }
  2. Now, let's write up a function that will trigger a mouse-click on a disabled controls element:
    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);
    }
  3. Now, create an event handler for the OnLogError event.
  4. Interpolate the created event handler into the following code:
    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;
    }
  5. If we launch the 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.

How it works...

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).

Note

Pay attention that all the executable actions are being logged (with the help of the Log.Message and Log.Event methods). This is a good style of writing similar event handlers as well as any other generic code, since further on, when analyzing the logs, this information might come in handy.

See also

  • The process of creation of an event handler is dealt with in greater detail in the Creating event handlers recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.148