What is the property in control to be set to get the value from AppResult.Text in Xamarin.UITest? RRS feed

  • Question

  • User390391 posted

    I am trying to create custom control in Xamarin.Forms which has the unique automation id for automation. So, i have set the android renderer's contentDescription property. So, i can get the AppResult.Label property to identify the control. But, my requirements is that how to get the control's text property? What property i have to set in control level with the corresponding text to get it in AppResult.Text property.



    public void WelcomeTextIsDisplayed()



    AppResult[] results = App.WaitForElement("myControl");

    Assert.IsTrue(results[0].Text == "My Control Text", results[0].Text + "\n" + results[0].Description + "\n" + results[0].Id + "\n" + results[0].Label); }

    Monday, February 10, 2020 1:10 PM

All replies

  • User390391 posted

    Can anybody look onto this and let me know whether we have any solution?

    Tuesday, February 11, 2020 5:02 AM
  • User382871 posted

    Add the AutomationId to the control in xaml, then reference the id to query the info in test code. <xx x:Name="b" AutomationId="age" Text="test..."/> Test query code: string result = app.Query("age").Text;

    Tutorial: https://docs.microsoft.com/en-us/appcenter/test-cloud/uitest/get-started-xamarin-forms#use-automationid-in-the-xamarinforms-ui

    Tuesday, February 11, 2020 9:02 AM
  • User390391 posted

    Hi Jarvan,

    Yes. We can identify the UI element via AutomationId property. But, i don't know how to return the expected value in AppResult.Text?

    As per your update, will app.Query("age").Text property return the test.... If yes, how it will return that? Will framework itself return the value which is set in *Text * property. If yes, should we expose new Text property in control level itself?

    Monday, February 17, 2020 9:31 AM
  • User382871 posted

    As per your update, will app.Query("age").Text property return the test... Yes, just use app.Query("id").Text method to get the value. If you don't specify a value to the Text property, the method will return the default value. ``` string result = app.Query(c => c.Id("id_text")).Text;

    //or var appresult = app.Query(c => c.Id("idtext")); string result = app_result.Text; ```

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/xamarin.uitest.queries.appresult?view=xamarin-uitest-sdk https://docs.microsoft.com/en-us/dotnet/api/xamarin.uitest.android.androidapp.query?view=xamarin-uitest-sdk#XamarinUITestAndroidAndroidAppQuerySystemString_

    Tuesday, February 18, 2020 3:37 AM
  • User390391 posted

    Hi Jarvan,

    Thanks for your update.

    I have prepared the simple example to explain better about my case. Here, i have derived my custom control from Grid and i introduced the Text property. When i try to view the element using Repl() method, it does not show the Text property but it shows the text properties for Label & Entry controls.


    XAML <StackLayout > <Label Text="Hello, Custom Renderer!" /> <local:MyEntry Text="In Shared Code" AutomationId="myEntry" /> <local1:CustomView Text="Sample" BackgroundColor="Red" HeightRequest="500" AutomationId="customControl" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/> </StackLayout>


    ``` public class CustomView : Grid { public CustomView() {

    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(string),string.Empty);
    public string Text
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }


    Test case ``` [Test] [Description("SampleTest")] public void WelcomeTextIsDisplayed() {
    App.Repl(); AppResult[] results2 = App.WaitForElement("customControl"); Assert.IsTrue(results2[0].Text == "Sample", results2[0].Text + "\n" + results2[0].Description + "\n" + results2[0].Id + "\n" + results2[0].Label);

    } ``` Repl result

    Can you please let me know whether i missed anything?

    Tuesday, February 18, 2020 1:48 PM
  • User390391 posted

    Do we have any solution for this?

    Wednesday, February 19, 2020 7:36 AM
  • User382871 posted

    To write unit tests around custom Xamarin.Froms controls, you can refer to this guide. https://spin.atomicobject.com/2018/10/30/unit-testing-xamarin-forms-controls/

    Thursday, February 20, 2020 6:16 AM
  • User394751 posted

    Try to use with index position like this: app.Query(c=>c.Id("NoResourceEntry-2"))[0].Text

    similarly you can use class for same: app.Query(c=>c.Class("LabelRenderer"))[0].Text

    Query for Class("LabelRenderer") gave 2 results. So in above ex. you can see it gave you 2 results but you have to use index for particular result.

    Thursday, May 21, 2020 8:31 AM