How to set the unique id for ui elements in Xamarin Forms (XAML)

I need to automate mobile application developed by Xamarin Tool using appium Server(Java+Selenium).How to set the property/unique id for UI elements that can be used by all platforms(iOS, Android & WP).App is built in Xamarin.Forms and XAML used for UI.

<Entry Text= "{Binding Username}" Placeholder="Username" HorizontalOptions="Center" />

Need help on this

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Why not just use the x:Name attribute?

  • Thanks Gerald for your quick response!!.
    We have set the value for x:Name in Xaml. But we are unable to get the value of x:Name in android uiautomator.How x:Name attribute will be used in android uiautomator? Do you have any idea on how the x:Name is represented in android?

  • Thanks Craig.I have tried with StyleId and followed the code what you given, but still we are not able get the StyleId in our environment(uiautomator) we got the null value for Content desc. Any idea on this?

  • AlfeuLucasAlfeuLucas USMember

    Using AutomationId property in XAML worked fine in my Android app.

  • RickPiovesanRickPiovesan CAUniversity ✭✭

    UITest / Accessibility conflict?
    1. XF has "automationid" field. Xam doc says this loads Android "label" field - from here

            Referring to the AutomationId in UITests
            When writing UITests, the AutomationId value is exposed differently on each platform:
    
            iOS uses the id field
            Android uses the label field
    
    1. Is above doc obsolete? In XF UITest sample code - from here, the MainActivity.cs file has comment:

              // NOTE: this is no longer required - Xamarin.Forms 2.2 now AUTOMATICALLY
              // assigns the "AutomationId" to the "ContentDescription"
              //global::Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) => {
              //  if (!string.IsNullOrWhiteSpace (e.View.AutomationId)) {
              //      e.NativeView.ContentDescription = e.View.AutomationId;
              //  }
              //};
      
    2. But accessibility in Android (Talkback) uses ContentDescription field and assumes content to be language-specific context textual information - i.e., the app may/should alter content based on language.

    3. But altering contents of ContentDescription would break UI tests when test executed under different language setup.

    Is above correct? Is there a conflict in XF with UITest and accessibility? If so, is it possible to revert automationId to go back to loading the android "label" field and not to use ContentDescription field?

Sign In or Register to comment.