Search code examples
xamarin.formsuitest

UITest on Xamarin Forms writing StyleId as Label rather than ID


I'm trying to set up a UI Test on a Xamarin Forms Projects

I followed the guide here and added StyleId to my username control

<Entry x:Name="username_name" 
        StyleId="username_styleid"
        Text="{Binding Username, Mode=TwoWay}"
        IsEnabled="{Binding IsBusy, Converter={StaticResource ReverseBoolConverter}}"
        Style="{StaticResource TextboxLight}"
        Placeholder="{Binding UsernameLabel}" />

I added code into the Android MainActivity

Forms.ViewInitialized += (sender, e) => {
    if (!string.IsNullOrWhiteSpace(e.View.StyleId))
    {
        e.NativeView.ContentDescription = e.View.StyleId;
    }
};

When I run the test and use REPL, I can see that the StyleId has been output as a label property rather than an id property

enter image description here

Has anyone managed to get this to work?


Solution

  • That is working as designed. On iOS you will see the value show up as id; on Android it will show up as label.

    app.Query(c=>c.Marked("username_styleid")) will work across platforms. The shorter form of app.Query("username_styleid") does the same thing.

    Marked will find the element(s) with the specified value in the id field on iOS; the label field on Android and as text on either. So, a key to making this easy and useful in writing your tests is to make the StyleID (or AutomationId) different than text that appears in your app.