Test a GUI application with functions

This exercise describes how to create and use a user-defined function, which is useful to handle tasks not supported by default for a specific object class.

Create a new test to test a function

Create a copy of your existing MyFlight test, where you can add a function library and use it to test your app.

Do the following:

  1. Start with the MyFlightApplication solution open in UFT One.

    If you've closed and opened UFT One, make sure you start it with the WPF Add-in loaded.

  2. In the Solution Explorer, right-click the MyFlight test node, and select Save As.

  3. Save the test in the same location (C:\Users\<username>\Documents\Unified Functional Testing).

    In the File name field, enter MyFlightFunction, and then click Save.

The Solution Explorer switches to display MyFlightFunction test instead, with all the same actions and steps as the original MyFlight test.

Tip: To display both of your tests at the same time, add the MyFlight test back to your solution.

In the Solution Explorer, right click the solution node, and select Add > Add Existing Test. Select the MyFlight test.

Back to top

Create a function and associate it with your test

Create a function to test whether a date in the MyFlights app is displayed in the correct format and is actually valid.

Associate it with the MyFlightsFunction test to make it available for use.

Do the following:

  1. In the toolbar, click the New button down arrow and select Function Library.

    In the New Function library dialog, do the following:

    1. Ensure that you are located in the C:\%HOMEPATH%\My Documents\Unified Functional Testing folder.
    2. Create a new sub-folder named TutorialFunctionLibraries, and open it.
    3. In the File name field, enter CheckDateFunction to name your new function library.
    4. Click Create.

    The new CheckDateFunction is opened as a new tab in UFT One.

  2. Copy and paste the following code into the CheckDateFunction file.

    This function verifies that the date format is equal to DD-<MMM string>-YYYY, and is actually a valid date for the app.

    'The following function checks whether a date string (dateStr)
    'has the characters representing DD-<MMM string>-YYYY
    Function check_data_validity( dateStr )
     Dim firstDashPos, secondDashPos
     Dim mmPart, ddPart, yyyyPart
     firstDashPos = inStr( dateStr , "-" )
     secondDashPos = inStrRev( dateStr, "-" )
     If ( (firstDashPos <> 2 and firstDashPos <> 3) or (secondDashPos <> 6 and secondDashPos <> 7)) Then
       reporter.ReportEvent micFail,"Format check", "Date string is: "&" missing at least one dash ( - )."
       check_data_validity = False
       Exit function
     End If
     if firstDashPos = 2 Then
       ddPart = mid( dateStr, 1, 1)
     else
       ddPart = mid( dateStr, 1,2 )
     End If
     mmPart = mid( dateStr, firstDashPos+1, 3 )
     yyyyPart = mid( dateStr, secondDashPos +1 , 4 )
     If  not( mmPart="Jan" or mmPart="Feb" or mmPart="Mar" or mmPart="Apr" or mmPart="May" or mmPart="Jun" or mmPart="Jul" or mmPart="Aug" or mmPart="Sep" or mmPart="Oct" or mmPart="Nov" or mmPart="Dec")  Then
       reporter.ReportEvent micFail, "Format Check", "The month: "& mmPart&"is invalid. It is not a valid month string."
       check_data_validity = False
       Exit function
     End If
     If ddPart > 31 Then
       reporter.ReportEvent micFail, "Format Check", "The date: "& ddPart&"is invalid. It exceeds 31."
       check_data_validity = False
       Exit function
     End If
     If yyyyPart < 2021 Then
       reporter.ReportEvent micFail, "Format Check", "The year: "&yyyyPart&" is invalid. (Prior to this year.)"
       check_data_validity = False
       Exit function
     End If
     check_data_validity = True
    End Function
  3. Save, and then close the function library file.
  4. Associate your new function library with the MyFlightsFunction test.

    Do the following:

    1. In the Solution Explorer, right-click the MyFlightsFunction test node, and select Add > Associate Function Library.
    2. In the Open Function Library dialog, select the CheckDateFunction.qfl file, and click Open.

    The CheckDateFunction is added to the MyFlightsFunction test in the Solution Explorer, under a separate Function Libraries sub-folder.

  5. Save your test, and continue with Find the name of the date property to check.

Back to top

Find the name of the date property to check

The function you added in the previous steps checks the date format for a calendar object.

To verify this date format in your test, you must first find and note the properties for the specific object where you want to perform this check.

Do the following:

  1. Open the MyFlights GUI sample app, and log in.

    For more details, see Flight GUI layer.

  2. In UFT One, click the down arrow near the Object Identification Center toolbar button and select the Object Spy.

    UFT One 15.0.1 or earlier: Click the Object Spy toolbar button .

  3. In the Object Spy, click the pointing hand button .

    When UFT One is minimized to display the MyFlights app, click the date field to display its properties in the Object Spy.

  4. In the Object Spy, scroll through the table in the Properties tab until you see the Date value, and note the property name.

    For example:

  5. Close the Object Spy and return to your test.
  6. In the MyFlightFunction test, open the FlightFinder action in the Editor.
  7. In the FlightFinder action, place your cursor after the WpfCalendar.SetDate step and press ENTER to add a new line.
  8. Add a step to retrieve the date property for the datePicker object. This ensures that UFT One has the details it needs to run the function properly.

    Copy and paste the following code to the new line in your action:

    currentDate = WpfWindow("Micro Focus MyFlight Sample").WpfCalendar("datePicker").GetROProperty("DisplayDate")
    orderDate = currentDate + 10
    WpfWindow ("Micro Focus MyFlight Sample").WpfCalendar("datePicker").SetDate orderDate
    departureDate = WpfWindow("Micro Focus MyFlight Sample").WpfCalendar("datePicker").GetROProperty("date")
  9. Add another step to call the checkpoint function. Copy and paste the following code to a new line after the departureDate step.

    If check_data_validity(departureDate) Then
    	reporter.ReportEvent micPass, "Date is valid", departureDate
    End If
  10. Save your test and continue with:

Back to top

View your action in the Editor

Verify that your action now has the following lines in the Editor:

WpfWindow("Micro Focus MyFlight Sample").WpfComboBox("fromCity").Select "Los Angeles"
WpfWindow("Micro Focus MyFlight Sample").WpfComboBox("toCity").Select Sydney
currentDate = WpfWindow("Micro Focus MyFlight Sample").WpfCalendar("datePicker").GetROProperty("DisplayDate")
orderDate = currentDate + 10
WpfWindow ("Micro Focus MyFlight Sample").WpfCalendar("datePicker").SetDate orderDate
departureDate = WpfWindow("Micro Focus MyFlight Sample").WpfCalendar("datePicker").GetROProperty("date")
If check_data_validity(departureDate) Then
reporter.ReportEvent micPass, "Date is valid", departureDate
End If
WpfWindow("Micro Focus MyFlight Sample").WpfComboBox("Class").Select Business
WpfWindow("Micro Focus MyFlight Sample").WpfComboBox("numOfTickets").Select "2"
WpfWindow("Micro Focus MyFlight Sample").WpfButton("FIND FLIGHTS").Click

Back to top

View your action in the Keyword View

Switch to the Keyword View, and note the new datePicker GetROProperty and Function Call steps.

Back to top

Run your function test and analyze the results

Run your test to see how UFT One performs the function check to verify the date format and value.

Do the following:

  1. Close any open windows running MyFlights.

  2. In the toolbar, click Run .

    In the Run dialog, ensure that the MyFlightFunction test and the New run results folder options are selected.

  3. Click Run.
  4. When the test run is complete and the run results open, scroll down in the Test Flow to the FlightFinder action > Date is valid node.

    Note that a green checkmark is displayed next to the step name. This informs you that the checkpoint passed, as per the function you added.

Back to top

Next steps: