Ideas & Feature Requests

Share your ideas on how Test Cloud can better help you deliver higher quality apps faster.

Expose the dev friendly representation of a Xamarin UITest query via Func<AppQuery, AppQuery>.ToString()

When I do for example 

 
app.Query(x => x.Marked("Foo").Descendant("Bar").Index(0))
 
then
 
Marked("Foo").Descendant("Bar").Index(0)
 
(or something along those lines) is printed out for me in the terminal. I would like to have a UI Test API where I can access the string representation of a Func<AppQuery, AppQuery>. For example:
 
Func<AppQuery, AppQuery> myQuery = x => Marked("Foo").Descendant("Bar").Index(0);
Console.WriteLine("Your query is '" + myQuery.ToDevFriendlyString() + "'");
 
This would be especially useful for helper methods such as the following:
 
public static void AssertSingleElement(this IApp app, Func<AppQuery, AppQuery> query)
{
    Assert.AreEqual(1, app.Query(query).Length, message: $"Query '{query.ToDevFriendlyString()}' did not result in a single element.");
}
 
Now if the assertion fails, I'll know what it was looking for without having to consult the test source.
  • Nathaniel Cook
  • Sep 5 2017
Device Manufacturer
Device Model Name
Operating System Version
  • Attach files
  • Nathaniel Cook commented
    September 5, 2017 18:23

    As a workaround I came up with this based on what I found with ILSpy. Hope it helps someone:

    public static string ToCodeString(this Func<AppQuery, AppQuery> query, Platform platform)
    {
    var queryPlatform = (platform == Platform.iOS) ? QueryPlatform.iOS : QueryPlatform.Android;

    AppQuery expandedAppQuery;

    if (query == null)
    {
    expandedAppQuery = new AppQuery(queryPlatform);
    }
    else
    {
    expandedAppQuery = query(new AppQuery(queryPlatform));
    }

    var tokens = ((ITokenContainer)expandedAppQuery).Tokens;

    if (!tokens.Any())
    {
    return "*";
    }

    var values = tokens
    .Select(x => x.ToCodeString())
    .Where(x => !string.IsNullOrWhiteSpace(x));

    return string.Join(".", values);
    }