Forum Discussion

SimpleMe's avatar
SimpleMe
Occasional Contributor
7 years ago

How do I FindChild / FindAllChildren and Touch in iOS?

I'm new(er) to test automation and have been building an iOS project off and on for a few months. I have not done a lot of scripting, but can figure it out as needed. I'm using Python.

I'm faced with a CollectionView that has numerous CollectionViewCells. The labels within each cell will change based on the current settings. I want to be able to use something like FindChild or FindAllChildren to search for the properties of the label (ex:labelxyz) within the CollectionView. When found, I want to Touch it. From the help articles, I'm guessing I want to use FindAllChildren.

I want to try this method because I can't map the label as it floats around among several cells depending on the data.

Questions:

1) Is this the best method or approach?

2) If so, is there a way to do this with If/Then logic from the Operations menu?
3) If not, can someone please give me an example of scripting something like this in Python?

 

The CollectionView (CollectionView(3)) remains constant. The label can be found in any one of 20+ CollectionViewCells.

 

The full name example is:

Mobile.Device("iPad").Process("MyApp").Window(0).CollectionView(3).CollectionViewCell(0).Label("LabelToTouch")

4 Replies

  • baxatob's avatar
    baxatob
    Community Hero

    Hi,

     

    You can try the following code:

     

    def test():
    
        target_CollectionView = Mobile.Device("iPad").Process("MyApp").Window(0).CollectionView(3)
    
        target_Label = target_CollectionView.Find( ['ObjectType', 'ObjectText'], 
    ['Label', 'LabelToTouch'], 3) target_Label.Touch()

     

    • SimpleMe's avatar
      SimpleMe
      Occasional Contributor

      Thanks. I think I'm doing something wrong with your example. Here is how I'm trying to use it:

       

      def Status_Find_Touch():
      
       
      
      target_CollectionView = Mobile.Device("iPad").Process("MyApp").Window(0).CollectionView(3)
      
       
      
      #'Label (is ObjectType)', 'ObjectText (property of the label)' I think I have this part setup correctly
      target_Label = target_CollectionView.Find( ['Label', 'Enroute'],
      
       
      
      #'Label', 'mappedname?') This is the part I think I have wrong
      ['Label', 'MappedNameOfLabel'], 3)
      
       
      
      target_Label.Touch()

       

       

      When I run it I'm receiving an error "You are trying to call the "Touch" method or property of an object that does not exist."

      • baxatob's avatar
        baxatob
        Community Hero

        If I correctly understood, your code now looks like:

         

         

        target_Label = target_CollectionView.Find( ['Label', 'Enroute'], ['Label', 'MappedNameOfLabel'], 3)

         

        ['Label', 'Enroute'] - first array should contain the names of the properties

        ['Label', 'MappedNameOfLabel'] - and the second array - the values of those properties.

         

        If you want to find a label only by its name (using only ONE property), you can use:

         

        target_Label = target_CollectionView.Find('ObjectText', 'Enroute', 3)

        More details here: https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/common-for-all/find-method.html

  • anhvu's avatar
    anhvu
    Frequent Contributor

    Here is a sample function to find and click a control.

     

    [Control need to be search] = pProcess.FindChild( [Property of the control] , [Value of the control], [How Deep to Search]) 

     

    EX: var btnLogIn = pProcess.FindChild( "ControlText" , "Log In", 10) 

     

    full example:

     

    function ClickMenuDrawer(device, caption)
    {
    var status = false;
    var props = "mContentDescription";
    var values = caption;//"open drawer menu";
    var searchControl;
    if(Mobile.Device(device).Exists)
    {
        //Check The app existed or not
        if(Mobile.Device(device).Process(app package).Exists)
        {
            var pProcess = Mobile.Device(device).Process(app package));
             pProcess.Refresh();

            var btnClick;
            //Search the button
            searchControl = pProcess.FindChild( props , values,10)
            if(searchControl.Exists && searchControl.VisibleOnScreen)
            {
                //Click the button
                btnClick = searchControl;
                btnClick.Click();
                Mobile.Device(device).Refresh();
                status = true;
             }
            else
            {
                Log.Error("ClickMenuDrawer: FAIL! The button not found or invisible!");
            }
        }
        else
        {
        Log.Error("ClickMenuDrawer: FAIL! The App is not installed or not an instrumented app.");
        }
    }
    else
    {
    Log.Error("ClickMenuDrawer: FAIL! Device Not Found.");
    }
    return status;

    }