Forum Xamarin Test Cloud (Read Only)

Tapping a random item from a list

I am building the tests for creating a character in my app. We have multiple skin tones and clothing options and I need an easy way to select a random one.

The items all have the same ID and have no differentiating factors between them unless I rip the URL from it (which I don't want to do.). I was able to get random selections in Calabash with a simple solution but finding one in C# is proving a bit more difficult.

app.Tap(x=>x.Id("image"); will always tap the first item in the list.

Tagged:

Answers

  • christianIMVUchristianIMVU USMember ✭✭
    var item = app.Query(x=>x.Id("image"));
    
            Random rnd = new Random();
    
            var i = rnd.Next(item.GetLength(0));
    
            app.Tap(x => x.Equals(item[i]));
    

    Is there a way for a query to target a particular result?

  • LearnEverythingLearnEverything USMember ✭✭✭
    public static class EnumerableListExtension
        {
            public static T PickRandom<T>(this IEnumerable<T> source)
            {
                return source.PickRandom(1).Single();
            }
            public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
            {
                return source.Shuffle().Take(count);
            }
            public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
            {
                return source.OrderBy(x => Guid.NewGuid());
            }
        }
    
Sign In or Register to comment.