Tips and tricks for AI-based testing

GUI UFT Mobile, web, and windows-based SAP GUI tests

This topic provides tips and tricks for writing AI-based tests in UFT One.

Learn how to transform your property-based test steps to AI-based ones

Run your tests with the AI Transformation Assistant enabled, to receive suggestions for transforming technology-based test steps to AI-based testing steps. Replace existing steps with the suggested AI object steps to create a more resilient and platform-agnostic test.

To enable the assistant in Tools > Options > GUI Testing > AI, select Replace run results with AI step suggestions.

When this option is selected, your test run results do not reflect the results of the test. Instead, the report suggests an AI object and operation for any step where a property-based test object can be replaced with an AI object.

Note: The AI Transformation Assistant does not provide AITable or AICalendar object suggestions.

Back to top

Send keyboard commands to AI objects

In some applications, a Type operation on an AI object requires an additional keyboard command, such as pressing Enter. You can simulate this command using Windows Scripting or Device Replay for desktop browsers and Device.EnterKeys for mobile applications.

Example #1 Type a model name in the Cell Phone combo box and send the Enter key to trigger selection

  • For desktop browsers

    • Using Windows Scripting

      Copy code
      AIUtil("combobox", "Cell Phone").Type "Pixel 2"
      Dim mySendKeys
      Set mySendKeys = CreateObject("WScript.shell")
      'Send Enter key
      mySendKeys.SendKeys("~")       

      For details, see SendKeys Method of Windows Script Host.

    • Using Device Replay

      Copy code
      AIUtil("combobox", "Cell Phone").Type "Pixel 2"
      wait 5    
      Dim myDeviceReplay
      Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")

      myDeviceReplay.PressKey 28            'Send Enter key

      ' Other example keys:
      ' myDeviceReplay.PressKey 14           'Send Backspace
      ' myDeviceReplay.PressKey 211          'Send Delete
      ' myDeviceReplay.PressKey 15           'Send Tab
      ' myDeviceReplay.PressKey 199          'Send Home

      For details, see DeviceReplay Object in the UFT One Object Model Reference for GUI Testing.

  • For mobile applications

    Using Device.EnterKeys

    Copy code
    AIUtil("combobox", "Cell Phone").Type "Pixel 2"
    wait 5    
    Device("Device").Enterkeys typeKey, "Enter"

    Tip: Add a wait step before sending keys to the application if the mobile device is slow to process keyboard input.

    For details, see EnterKeys Method of Device Object.

Example #2 Clear a text box before writing in it on desktop browsers

Using Device Replay

Copy code
Sub CleanInputField(Label)
      AIUtil(input,Label).Click
    Dim myDeviceReplay
    Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")
      for i = 1 to 25
            myDeviceReplay.PressKey 14
      next 
end sub

Back to top

Improve performance by minimizing re-inspections during a test run

Use the new AIUtil.Context.Freeze and UnFreeze methods to prevent UFT One from reinspecting the application between steps, if you know the application is not expected to change.

Back to top

Handle cases where AIUtil("search").Search does not perform a search

In some applications, the search box is shown after clicking the search icon. In these cases, AIUtil("search").Search may not be able to perform a search operation in your application.

You can perform the search using multiple steps, as demonstrated in the following examples:

Scenario #1

Text is not typed in the search box after AIUtil("search").Search

Suggested solution

Copy code
AIUtil("search").Click
'Wait a while until the search box is displayed
wait 2
AIUtil("search").Search "telekom"

Scenario #2

After clicking the search icon, the search box is not in focus and two search icons are displayed

Suggested solution

Copy code
AIUtil("search").Click
'Wait a while until the search box is displayed
wait 2
AIUtil("search",micNoText,micFromBottom,1).Search "telekom"

Scenario #3

After clicking the search icon, the search icon disappears and the search box is not in focus

Suggested solution

Copy code
AIUtil("search").Click
'Wait a while until the search box is displayed
wait 2
AIUtil("input","search").Type "telekom"

'Press Enter key
Dim myDeviceReplay
Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")
myDeviceReplay.PressKey 28

Back to top

Find an object in a scrollable panel

This example demonstrates how to use the AIUtil.Scroll method to find objects in scrollable panels of your application.

Copy code
Set ObjBrowser= Browser("creationtime:=1")
ObjBrowser.Maximize
AIUtil.SetContext ObjBrowser
'---------------------------------------------------------------------------------------
ObjBrowser.Navigate https://www.youtube.com/
ObjBrowser.Sync
'wait(2)
'---------------------------------------------------------------------------------------
print ClickAndScrollTo ( AIUtil("home"), AIUtil.FindTextBlock("Report history"))
'---------------------------------------------------------------------------------------
ObjBrowser.Navigate https://usa.banggood.com
ObjBrowser.Sync
'wait(2)
-----------------------------------------------------------------------------------------
print ClickAndScrollTo (AIUtil("down_triangle", micAnyText, micFromTop, 5), AIUtil.FindTextBlock("Home Appliances", micFromBottom, 1))
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function ClickAndScrollTo (ObjStart, ObjTarget)
    ClickAndScrollTo=false
    AIUtil.RunSettings.AutoScroll.Disable
    ObjStart.Highlight   
    ObjStart.Click
    For i = 1 To 4
        if ObjTarget.Exist(0) then
        ObjTarget.Highlight
        ObjTarget.hover
        ClickAndScrollTo=true
               exit function 
        end if
'ScrollUsingPointerPosition([in] micAIScrollDirection direction, [in] double numberOfWheelTicks);
'Move the mouse over the scroll object using AIUtil.Hover or another method, for example device replay 
        AIUtil.Scroll "down", 1
    Next
End Function

Back to top

Prevent OCR failure on misspelled text

When running tests, OCR fails to identify text displayed with a misspelling indication, such as a red line under the text.

Use one of the following solutions:

  • Disable spell checking in your browser or application.

  • Move the focus away from the field with the misspelled text. To do this, add a step to your test that clicks somewhere outside of the text field before the step that retrieves the text.

Back to top

Horizontal scrolling on an SAP GUI application

If AIUtil.Scroll and AIUtil.ScrollOnObject do not provide horizontal scrolling on an SAP GUI application, you can use AIUtil.Click and AIUtil.MultiClick to perform horizontal scrolling.

Copy code
' Click the right triangle to perform right scrolling
' If there are more than one right_triangles, you can identify the control by relative location.
Set ObjRightTriangle = AIUtil("right_triangle")
'Click the right triangle four times
ObjRightTriangle.MultiClick 4
' Click the left triangle to perform left scrolling
Set ObjLeftTriangle = AIUtil("left_triangle")
'Click the left triangle
ObjLeftTriangle.Click

Back to top

See also: