Search code examples
c#xamarin.uitest

Xamarin UITest: Is there a way to App-Query with case insensitive strings?


I have a list of elements, and I want to check if the list contains the person name I'm looking for.

The simplest way to do that is :

app.Query(e => e.All().Id("text_name").Property("Text").Contains("Dupont") );

with "text_name" the Id of TextView inside each list item.

Except that sometimes the name written in the list might be "dupont" or "DUPONT" and in this case the query won't be able to find it.

I looked at the UITest AppQuery API and apparently there's no way to query with case insensitive strings.


Solution

  • So, to get around this limitation, what I might suggest is doing this :

    String[] results = app.Query(e => e.All().Id("text_name").Invoke("getText"))
                         .Cast<String>().ToArray();
    Assert.IsTrue( ArrayContainsCaseInsensitive(results, "Dupont") );
    

    This will return an array of text fields of all elements matching the specified Id.

    Then, with the following method, you check (in a case insensitive way) if this array contains what you're looking for :

    public static bool ArrayContainsCaseInsensitive(String[] array, string wanted)
        {
            foreach (String s in array)
                if (s.ToUpper().Contains(wanted.ToUpper()))
                    return true;
    
            return false;
        }