Stage 9: Implementing support for recording on the Book control

By this point in the tutorial, your toolkit support set already enables full UFT One functionality. UFT One recognizes the Book control, can learn it and can run tests on it.

An additional, optional way to create tests in UFT One is by recording operations that a user performs on the application. As you can see in Plan support for the Web Add-in Extensibility Book sample toolkit, by default UFT One records plain Click operations on the various Web link and image objects within the Book control. It would be more helpful to record Select, GoToAuthorPage, and GoToUsedBooksPage operations on the Book control itself, in response to those same clicks.

To support customized recording on your control, you must instruct UFT One to listen to the relevant events and inform UFT One what test steps to record in response to each event.

To do this, you write two types of JavaScript functions:

  • One JavaScript function that uses the RegisterForEvent function in the _util utility object that UFT One exposes in the Web Add-in Extensibility SDK to register for listening to the correct events on the correct elements. The arguments of this function also determine what JavaScript functions UFT One calls when each event occurs.

    You specify the name and, optionally, the location of this JavaScript function in the General tab of the test object class designer. Extensibility Accelerator saves this information in the toolkit configuration file.

  • One or more JavaScript functions that handle the events by calling the Record function in the _util utility object to inform UFT One what step to add to the test.

    Note: The Record function, and other utility object functions, require a SafeArray type argument. To convert an array to a SafeArray, you can use the toSafeArray (array) function that Web Add-in Extensibility provides. This function is defined in <Extensibility Accelerator installation folder>\bin\PackagesToLoad\common.js. (This file is also located in the <UFT One installation folder>\dat\Extensibility\Web\Toolkits folder.)

For information on the syntax of the utility object functions, see the _util section in the API Reference.

To develop support for recording on the Book control:

  1. Set the advanced options for recording in the General tab of the test object class designer.

    1. Open the WebExtBook General tab (Test Object Class designer).

      If you closed the designer, open it from the Class View by double-clicking WebExtBook in the list of test object classes for your project.

    2. Click Advanced Options, and scroll down to the RecordOptions.

    3. In the Event registration function name box enter ListenToEvents.

    4. Clear the check boxes for the options that instruct UFT One to use standard Web event configuration to handle events on the control and its children.

    5. Save your changes, and open the WebExtBook.js file from the Project Explorer to see how your definitions are reflected in the file.

      The following Record element is added within the WebExtBook Control element:

      <Record>
          <EventListening use_default_event_handling_for_children = "false" 
                          use_default_event_handling = "false" 
                          type = "javascript" function = "ListenToEvents"/>
      </Record>
      

      This instructs UFT One not to use the default Web Event Configuration for handling events on the Book control and its children, but to call the JavaScript function ListenToEvents. Because you did not specify a JavaScript file, UFT One looks for the JavaScript function in the WebExtBook.js file specified at the Control level for the WebExtBook test object class.

  2. Open the WebExtBook.js file.

    In the General tab in the test object class designer, in the advanced options for recording, click Implementation Code button near the Event registration function name box.

    The WebExtBook.js file opens to the stub created for the ListenToEvents function.

  3. Add the JavaScript implementation required to support recording.

    1. Replace the stub created for the ListenToEvents function with the following code:

      function ListenToEvents( elem )
      {
          // Connect to the "Select" event: When the book name or the book 
          // icon is clicked, call OnSelectClicked.
          _util.RegisterForEvent( _elem.rows[0].cells[0].children[0], "onclick", "OnSelectClicked");
          _util.RegisterForEvent( _elem.rows[0].cells[1].children[0], "onclick" , "OnSelectClicked" );
          // Connect to the "Author" event: When an author name is clicked,
          // call OnAuthorClicked.
          for( var i = 0 ; i < _elem.rows[1].cells[0].children.length ; ++i )
          {
              if( _elem.rows[1].cells[0].children[i].tagName == "A" )
              {
                  _util.RegisterForEvent( _elem.rows[1].cells[0].children[i], "onclick", "OnAuthorClicked" );
              }
          }
          // Connect to the "UsedBooks" event: When "Used" is clicked,
          // call OnUsedBooksClicked.
          if(  _elem.rows[3].cells[0].children.length > 1 )
              _util.RegisterForEvent( _elem.rows[3].cells[0].children[1], "onclick", "OnUsedBooksClicked" );
          return true;
      }

      This function registers UFT One to listen to click events on the book's title, image, and authors, and on the Used link. When registering for an event, this function specifies what JavaScript function UFT One must call when the event occurs.

    2. Add the following event handler JavaScript functions:

      function OnSelectClicked( handlerParam , eventObj )
      {
          // Record the "Select" step
          var arr = new Array();
          _util.Record( "Select", toSafeArray(arr) , 0 );
          return true;
      }
      
      function OnAuthorClicked( handlerParam , eventObj )
      {
          // Record the "GoToAuthorPage" step
          var arr = new Array();
          arr[0] = eventObj.srcElement.innerText; 
          _util.Record( "GoToAuthorPage", toSafeArray(arr) , 0 );
          return true;
      }
      
      function OnUsedBooksClicked( handlerParam , eventObj )
      {
          // Record the "GoToUsedBooksPage" step
          var arr = new Array();
          _util.Record( "GoToUsedBooksPage", toSafeArray(arr) , 0 );
          return true;
      }
    3. These functions record Select, GoToAuthorPage, and GoToUsedBooksPage on the WebExtBook test object, as planned in Plan support for the Web Add-in Extensibility Book sample toolkit.

    Continue to Deploy the support to UFT One and test recording.