Steve Chadbourne
Steve Chadbourne

Reputation: 6953

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?

Upvotes: 1

Views: 1396

Answers (2)

Glenn Wilson
Glenn Wilson

Reputation: 241

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.

Upvotes: 4

Adam
Adam

Reputation: 16199

Using the StyleId will soon be obsolete with 2.2.0.

I haven't personally tested it yet but

AutomationId Support

Xamarin.Forms now has first class support for setting automation identifiers for usage with Xamarin UITest or other testing frameworks. Simply setting the AutomationID property should allow the automation framework to find and interact with your controls.

Not sure what an AutomationId translates to however, hopefully an ID.

Upvotes: 2

Related Questions