Class UIRobot


  • public class UIRobot
    extends java.lang.Object
    This class permits the control of the User Interface, playing back events recorded by the user. The robot is comprised of some dialogs that are invoked using a special key defined by the application. The special key running in Java SE (eclipse, netbeans) is control+1. The code below defines the Find special key to be used at the device (you can choose any special key you want):
     Vm.interceptSpecialKeys(new int[]{SpecialKeys.FIND});
     Settings.deviceRobotSpecialKey = SpecialKeys.FIND;
     
    You should set it at the application's constructor. When this key is pressed, it opens a window with three options: record, playback and cancel. Clicking on the "record" button opens another window asking for the robot's name. Type the name and press "Start record" to start recording. Do the events smoothly and slowly. When done, press the special key again. Clicking in the playback button opens a screen with a list of recorded robots. After selecting the robots, press one of these buttons:
    • Play selected: plays the robots in the same order you selected (the order is recorded as you select).
    • Play all: plays all robots in the list, in the order they appear.
    • Play random: randomizes the selected robots order and reproduce that. A new window is opened asking you to enter the number of times that exact sequence will be run.
    • Dump contents: dumps the contents of the selected robots, so you can see the events that are played back.
    • Delete selected: delete the selected robots.
    Robots are saved in a file with ".robot" extension at the application's path. Since the robot saves absolute pen events at a specific time, they are not portable among different device resolutions and/or devices with different processors. You can start a specific robot passing its name as commandline to the application. For example, in:
    • Android: adb shell am start -a android.intent.action.MAIN -n totalcross.app.uigadgets/.UIGadgets -e cmdline test1.robot
    • Windows 32: UIGadgets.exe /cmdline test1.robot
    • J2SE: /cmdline flick.robot tc.samples.ui.gadgets.UIGadgets
    Note that no other commandline parameters must be passed to the application; the UIRobot expects that the only parameter will be the robot's name. In JavaSE (desktop), you can skip the robot start by launching the application and pressing the SHIFT key while the "Starting ..." MessageBox is displayed. This is useful if you want to record the robot again whithout having to remove it from the commandline. The shift key also aborts the robot during its execution. When the robot finishes, it takes a screenshot of the application. When it plays back, it compares the screen with the saved one and sends one of these two UIRobotEvent to the MainWindow: ROBOT_SUCCEED (if comparison succeeds) or ROBOT_FAILED (if comparison fails). You can create a log file with the results by putting this in the onEvent of your MainWindow class:
      if (event.type == UIRobotEvent.ROBOT_FAILED || event.type == UIRobotEvent.ROBOT_SUCCEED)
      {
         try
         {
            File f = new File(Settings.appPath+"/robot.log",File.CREATE_EMPTY);
            String s = event.type == UIRobotEvent.ROBOT_FAILED ? "FAILED" : "SUCCEED";
            f.writeBytes("Robot "+UIRobot.robotFileName+" "+s+". Running time: "+UIRobot.totalTime);
            f.close();
         }
         catch (Exception e) {e.printStackTrace();}
      }
     
    See Also:
    SpecialKeys, UIRobotEvent
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean abort
      Set to true to abort the run of the current UIRobot.
      static int IDLE  
      static int PLAYBACK  
      static int RECORDING  
      static java.lang.String robotFileName
      The filename of the running robot.
      static int status  
      static int totalTime
      The amount of time since the robot started.
    • Constructor Summary

      Constructors 
      Constructor Description
      UIRobot()
      Constructs a new UIRobot and opens the user interface.
      UIRobot​(java.lang.String robotFileName)
      Constructs a new UIRobot and starts the playback of the given file.
    • Field Detail

      • status

        public static int status
      • abort

        public static boolean abort
        Set to true to abort the run of the current UIRobot. Useful if you set a breakpoint at a code and want to abort the run. You can also abort the UIRobot by pressing the shift key during execution and at the starting message box.
      • robotFileName

        public static java.lang.String robotFileName
        The filename of the running robot.
      • totalTime

        public static int totalTime
        The amount of time since the robot started.
    • Constructor Detail

      • UIRobot

        public UIRobot()
                throws java.lang.Exception
        Constructs a new UIRobot and opens the user interface.
        Throws:
        java.lang.Exception
      • UIRobot

        public UIRobot​(java.lang.String robotFileName)
                throws java.lang.Exception
        Constructs a new UIRobot and starts the playback of the given file.
        Throws:
        java.lang.Exception
    • Method Detail

      • onEvent

        public void onEvent​(int type,
                            int key,
                            int x,
                            int y,
                            int modifiers)
      • stop

        public void stop()
                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • removeUIRobotListener

        public void removeUIRobotListener​(UIRobotListener listener)
        Removes a listener for UIRobot events.
        See Also:
        UIRobotListener