AutomationId not available for use

OscarOliverOscarOliver USMember

I can not use the AutomationId property to provide ID's to elements (Button, EntryEditText, etc).

If I try to use it in the C# file like,
    var b = new Button {         Text = "Click me"         AutomationId = "MyButton"     };
it is not suggested to me and therefore I have compilation error.

Otherwise, if I use AutomationId in XAML like,
     <Button AutomationId = "MyButton" Text = "Click Me" />
Also is not suggested, but no compile error appear although when I run the application it fails.

I followed the steps in the next link:
https://developer.xamarin.com/guides/xamarin-forms/deployment,_testing,_and_metrics/uitest-and-test-cloud/

Best Answer

  • mallibonemallibone US ✭✭
    Accepted Answer

    @OscarOliver that definitely is the issue. The Automation ID support is from version 2.2 and higher (Releasenotes). Is there a reason why you do not use the latest version of Xamarin.Forms?

Answers

  • mallibonemallibone USUniversity ✭✭
    edited July 2016

    @OscarOliver did you extend the AppDelegate and MainActivity to allow this function? I wrote a blog post a short while ago about this topic.

    Here are the snippets that need to be added for the MainActivity:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    
        global::Xamarin.Forms.Forms.Init(this, bundle);
    
        Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) => {
            if (!string.IsNullOrWhiteSpace(e.View.AutomationId))
            {
                e.NativeView.ContentDescription = e.View.AutomationId;
            }
        };
    
        LoadApplication(new App());
    }
    

    And the App Delegate:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        #if ENABLE_TEST_CLOUD
        Xamarin.Calabash.Start();
        #endif
    
        Forms.Init();
    
        Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) => {
            // http://developer.xamarin.com/recipes/testcloud/set-accessibilityidentifier-ios/
            if (null != e.View.AutomationId)
            {
                e.NativeView.AccessibilityIdentifier = e.View.AutomationId;
            }
        };
    
        LoadApplication(new App());
    
        return base.FinishedLaunching(app, options);
    }
    
  • OscarOliverOscarOliver USMember
    edited July 2016

    Hi @MarkAllibone, I wrote that lines but not recognize View.AutomationId

    image

    The error message given from Visual Studio is:
    image
    'VisualElement' does not contain a definition for 'AutomationId' or is no extension method 'AutomationId' accepting a first argument of type 'VisualElement' (any directive 'using' or an assembly reference missing?)

    You know what is the reason?

    Thank you for your attention!

  • mallibonemallibone USUniversity ✭✭

    @OscarOliver what version of Xamarin.Forms/Xamarin UI Test are you using?

  • OscarOliverOscarOliver USMember

    Xamarin 4.0.4.4
    Xamarin.Android 6.0.4.0
    Xamarin.iOS 9.6.2.2

  • OscarOliverOscarOliver USMember

    @MarkAllibone
    Xamarin 4.0.4.4
    Xamarin.Android 6.0.4.0
    Xamarin.iOS 9.6.2.2

  • valdeterovaldetero USInsider, University ✭✭

    What version of Xamarin.Forms are you using? Look in your packages.config. I'm using 2.2.0.45 and it works for me. It is a property on the base object: Xamarin.Forms.Element.

  • OscarOliverOscarOliver USMember

    @valdetero I'm using an older version (package id="Xamarin.Forms" version="2.0.0.6490")
    Do you know if that version don't supports AutomationId?

  • mallibonemallibone USUniversity ✭✭
    Accepted Answer

    @OscarOliver that definitely is the issue. The Automation ID support is from version 2.2 and higher (Releasenotes). Is there a reason why you do not use the latest version of Xamarin.Forms?

  • OscarOliverOscarOliver USMember

    @MarkAllibone and @valdetero Thanks for the help.
    We have no reason, until now our development team has not had any problems with that version. I have spoken with them and we will update to the latest version.

  • ShimiShimi INMember

    @MarkAllibone I have same issue , so i updated xamarin forms to latest release , the Automation ID problem resolved but i got new problem .. in uw platform menu icon not showing ...

Sign In or Register to comment.