Forum Discussion

kashikar_harsh's avatar
kashikar_harsh
Contributor
14 years ago

Using Automation ID to identify objects

Hi,

      I am automating an application whose control names may change at runtime. So I am planning to use Automation IDs to identify objects uniquely. Is there any way to do this using TestComplete.



Regards,

Harshad

12 Replies

  • Hello,

      I found this forum post useful while helping another customer search for objects based on their automation id. Here's a sample script that works similar to findchild().


    function foo ()


    {


      var window = Sys.Process("WpfApplication1.vshost").WPFObject("HwndSource: MainWindow", "MainWindow")


      var MyAppDomain = "WpfApplication1.vshost.exe";


      var MyAppProcess = Sys.Process("WpfApplication1.vshost");


      


       myControl = FindByAutomationID (MyAppProcess, MyAppDomain, "button1aid", window, 5 ,false)


       myControl.Click();


    }


     


    function FindByAutomationID ( objProcess, strAppDomain, strAutomationID, objSearchContainer, intSearchDepth, blCaseSensitive)


    {


      AutomationIdProperty = objProcess.AppDomain(strAppDomain).dotNET.System_Windows_Automation.AutomationProperties.AutomationIdProperty


     


      objarrChildren = objSearchContainer.FindAllChildren("Exists", "True", intSearchDepth);


      objarrChildren = objarrChildren.toArray();


      


      if (objarrChildren.length > 0)


      {


        for (ii = 0; ii < objarrChildren.length; ii++)


        {


          if (0 == aqString.Compare(strAutomationID, objarrChildren[ii].GetValue(AutomationIdProperty), blCaseSensitive))


          {


            Log.Message("Found control with automation id '" + strAutomationID + "': " + objarrChildren[ii].FullName);


            return objarrChildren[ii];        


          }


        }


      }


      


      Log.Warning("No control found with automation id: " + strAutomationID);


    }


     







    This script will successfully find a control defined by the following XAML in a WPF application:

    <Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="252,134,0,0" AutomationProperties.AutomationId="button1AID" VerticalAlignment="Top" Width="75" Background="#E7FC0019" Foreground="#FF000700" BorderBrush="#FF070000" Click="Button_Click_1" >



    Of course, you need to modify the window, MyAppProcess, and MyAppDomain variables to match your tested application.



    Thanks,

    Rick



    SmartBear Customer Care
  • Is there any way to enable this for WPF applications without affecting existing name mapping items? Changing the settings as described above seems to cause mappings to work differently.