Implementing Support for the AllLights Control

In this part of the lesson, you modify the AllLightsCS class to extend UFT One support of the AllLights control, as per your plan (Planning Support for the AllLights Control).

Back to top

Open the AllLightsCS.java file. In the label_attr method, replace the code: return super.label_attr(obj); with the code: return "Lights"; to change the name of the test object. Then perform the procedures in this topic.

Implementing Support for New Identification Properties

In this section, you implement the methods that support the new identification properties you defined for the AllLights test object class.

Analyze the AllLights custom class to see the properties it supports. Determine which properties you can access from the new support class to provide the relevant identification properties to UFT One.

Notice the public methods GetcounterOn, which allows you to check how many lights are on at a given time, and isSet, which tells you the status of a particular light.

  1. Implement the oncount_attr method.

    In the oncount_attr method, replace the code return null; with
    return String.valueOf(((AllLights)obj).GetcounterOn());

    This implementation retrieves the counter from the AllLights custom class and returns it to UFT One.

  2. Implement the onlist_attr method.

    In the onlist_attr method, delete the code return null; and implement the method as follows to scan all of the lights and create a list of all the lights that are on:

    public String onlist_attr (Object obj) {
        AllLights lights = (AllLights) obj;
        StringBuffer buffer = new StringBuffer();
        for (int i=0; i<5; i++)
            for (int j=0;j<5;j++)
                if (lights.isSet(j,i)) {
                    buffer.append (" ");
                    buffer.append (i*5+j+1);
                    }
        return buffer.toString();
    }
    
  3. Implement the gameover_attr method.

    In the gameover_attr method, delete the code return null; and implement the method as follows to return Yes or No depending on whether or not all of the lights are on:

    public String gameover_attr(Object obj) {
        if (((AllLights) obj).GetcounterOn() == 25)
            return "Yes";
        return "No";
    }
    

    Select File > Save or click the Save button to save the AllLightsCS.java file.

Back to top

Implementing Support for New Test Object Methods

In this section, you implement the methods that support the new test object methods you defined for the AllLights test object class.

Analyze the AllLights custom class methods to determine what actions the class performs when a user clicks the Restart button or a light in the grid. You want to simulate these actions when UFT One runs the test object methods.

  1. Implement the Restart_replayMethod method.

    When a user clicks within the borders of the RESTART button, the AllLights custom class calls init and update(lights.getGraphics()) to initialize and redraw the application. The Restart_replayMethod method needs to simulate this behavior by calling the same methods.

    To do this, delete the code: return Retval.NOT_IMPLEMENTED; and implement the method as follows:

    public Retval Restart_replayMethod (Object obj){
        AllLights lights = (AllLights) obj;
        lights.init();
        lights.update(lights.getGraphics());
        return Retval.OK;
    }
    
  2. Implement the ClickLight_replayMethod method.

    The AllLights custom class performs the algorithm of turning lights on or off in response to a click, when it receives a mouseUp event. Therefore, when UFT One runs the ClickLight_replayMethod, and you want to simulate a click on a specific light, you can simply send the AllLights object a mouseUp event with the appropriate coordinates.

    In the method ClickLight_replayMethod, delete the code return Retval.NOT_IMPLEMENTED; and implement the method as follows:

    public Retval ClickLight_replayMethod(Object obj, String Row, String Column) {
        AllLights lights = (AllLights) obj;
        int col_num = Integer.valueOf(Column).intValue();
        int row_num = Integer.valueOf(Row).intValue();
        /* Row and column are 40 pixels wide*/
        Event event = new Event (lights, System.currentTimeMillis(), Event.MOUSE_UP, col_num *40, row_num *40, 0, 0);
        lights.mouseUp(event, col_num *40, row_num *40);
        return Retval.OK;
    }
    

    Note: To support this code, import java.awt.Event in AllLightsCS.java.

    Select File > Save or click the Save button to save the AllLightsCS.java file.

Back to top

Implementing Support for Recording

Because you planned to support recording on the AllLights control, you suppressed low-level recording on this object, and registered to listen for mouse events on this control.

The only mouse event that you want to trigger recording on the AllLights control is a mouse click. Therefore, in this section, you implement only the mouseClicked (MouseEvent arg0) event handler method and leave the other mouse event handler methods empty.

Implement the mouseClicked method as follows and save the AllLightsCS.java file:

public void mouseClicked(MouseEvent arg0) {
    AllLights lights = (AllLights) arg0.getSource();
    int x = arg0.getX();
    int y = arg0.getY();
    try{
        if (!isInRecord())
            return;
        /* If click is within the Restart button borders*/
        if ((x > 210) && (x < 270) && (y > 165) && (y < 185)) {
            MicAPI.logLine(DEBUG_ALLLIGHTSCS, "Record Restart operation");    
            MicAPI.record(lights, "Restart");
        }
    
        /* If click is within the borders of the grid, record ClickLights*/
        if ((x >= 0) && (x < 200) && (y >= 0) && (y < 200)) {
            MicAPI.logLine(DEBUG_ALLLIGHTSCS, "Record ClickLight operation");
            MicAPI.record(lights, "ClickLight", new String[] {String.valueOf(y/40), String.valueOf(x/40)});
         }
    } catch (Throwable th) { MicAPI.logStackTrace(th);}
}

Note: When the wizard created the AllLightsCS.java file, it automatically added the import com.mercury.ftjadin.custom.MicAPI, required to support this code.

In this event handler method, you call MicAPI.record in different ways. To record the Restart operation you provide only the object and the operation name. To record the ClickLight operation you provide additional arguments as well, specifying the coordinates of the clicked light.

The isInRecord method is called avoid carrying out any unnecessary operations if UFT One is not currently recording.

The MicAPI.logLine method prints the message to the log file only when the DEBUG_ALLLIGHTSCS flag is on. For more information, see Logging and Debugging the Custom Support Class.

The try ... catch block prevents unnecessary activity if this code is reached when the Java application is running while UFT One is idle. The MicAPI.logStackTrace method prints a stack trace to the same log file as other Java Add-in Extensibility log messages, enabling you to determine when this mouseClicked method was called inadvertently.

Back to top

Testing the Completed Support

In this section you test the Javaboutique toolkit support you have just completed. You do this by analyzing its effect on how UFT One views the AllLights control.

You do not have to deploy the toolkit support to UFT One again to test it because you changed only Java class files and not configuration files. You can use an open UFT One session (running with the Javaboutique toolkit support loaded), but you must close the AllLights application, and run it again, for the changes you made in the custom support to take effect.

  1. Test the new custom support in the Object Spy.

    1. Close the AllLights application and run it again.

    2. Open UFT One and load the Java Add-in and the Javaboutique toolkit support.

    3. Open a GUI test and use the Object Spy to view the AllLights properties and methods. The AllLights test object is now named Lights.

    4. Close the Object Spy.

  2. Create and run a UFT One test on the AllLights control.

    1. Add the AllLights control to the test object repository.

    2. Create a test that clicks in two locations in the grid, checks that the game is not over, and clicks Restart.

      The test you create looks something like this:

      Note: The ClickLight_replayMethod, does not check the argument values to make sure they are valid. If you provide arguments that are out of bounds (column or row higher than 4) a run-time error will occur.

    3. Run the test and see that it operates correctly (if you defined the checkpoint to check only that the game is not over—it succeeds).

  3. Record operations on the AllLights control.

    1. In UFT One, create a new GUI test and select Record > Record and Run Settings to open the Record and Run Settings dialog box. In the Java tab, select Record and run test on any open Java application. If the Web Add-in is also loaded, click the Web tab and select Record and run test on any open browser. Click OK.

    2. Click the Record button or select Record > Record. Click on different locations in the AllLights application: the grid, the RESTART button, and one of the counters.

      When you click in the grid, a ClickLight step is added to the test, with the relevant arguments. When you click the RESTART button, a Restart step is added. When you click anywhere else, no operation is recorded (because you disabled low-level mouse event recording). The recorded test looks something like this:

    3. Click the Stop button or select Record > Stop to end the recording session.

The AllLights custom control is now fully supported, according to the specifications you decided on when planning your custom support.

Back to top

Next steps: