How to interact with all elements that match query?

valdeterovaldetero USInsider, University ✭✭

I currently have a datadriven form with a bunch of textboxes. If I have the statement app.EnterText(x => x.TextField(), "1"), it states "Entering text '1' in first element (9 total) matching TextField() ...". I cannot pass an AppResult to EnterText to foreach it manually.

How do I enumerate through all the results of the query to interact with them?

Best Answer

  • Fabio_GottlicherFabio_Gottlicher US ✭✭
    Accepted Answer

    I have found, that interacting with the AppResult object after querying isn't always the most elegant - I wish UITest had built-in methods that take AppResult, so I ended up writing a bunch of extension methods in my project.

    If you need to tap on an AppResult, this will work:

    public static void Tap(this IApp app, AppResult result)
    {
        app.TapCoordinates(result.Rect.CenterX, result.Rect.CenterY)
    }
    

    For entering in text, this should work:

    public static void EnterText(tis IApp app, AppResult result, string text)
    {
        app.Tap(result); //You need the Tap extension method from above
        app.EnterText(text); //This will enter text into the currently focused element
    }
    

    I haven't tested the EnterText method, but it should work alright.
    hope this helps :)

Answers

  • Fabio_GottlicherFabio_Gottlicher USMember, University ✭✭
    Accepted Answer

    I have found, that interacting with the AppResult object after querying isn't always the most elegant - I wish UITest had built-in methods that take AppResult, so I ended up writing a bunch of extension methods in my project.

    If you need to tap on an AppResult, this will work:

    public static void Tap(this IApp app, AppResult result)
    {
        app.TapCoordinates(result.Rect.CenterX, result.Rect.CenterY)
    }
    

    For entering in text, this should work:

    public static void EnterText(tis IApp app, AppResult result, string text)
    {
        app.Tap(result); //You need the Tap extension method from above
        app.EnterText(text); //This will enter text into the currently focused element
    }
    

    I haven't tested the EnterText method, but it should work alright.
    hope this helps :)

  • valdeterovaldetero USInsider, University ✭✭

    This looks promising, thanks! I'll let you know after I try it out. Wrapping this in a foreach should meet my needs (or have it take an AppResult[]).

  • valdeterovaldetero USInsider, University ✭✭

    That worked great. It was exactly what I was looking for.

Sign In or Register to comment.