top of page

Randomizing UI Tests

In an effort to get more meaningful results from your UI tests, it can become obvious that the need for randomized data will facilitate this goal. The nature of UI testing is to test the functionality of UI elements, yet many of the apps we create as mobile developers require data to be inputted from the user. The latter is generally the realm of unit tests. However, keeping these two types of tests separate might not always garner results that are satisfactory. Instead, a middle-ground that incorporates both will produce meaningful and relevant data that can be used to ascertain the complete functionality of an app, especially when supplemented with their fundamental parts.

Often, UI tests can be so guided that they may miss important elements to a test, e.g. the inputting of data into a text field. In this case, having a test that runs and invariably inputs the same text into the field might not always yield a result that is desirable. Afterwards, looking over the resulting slew of screenshots and data there will inevitably be an assortment of the same entries over and over again. This may make it difficult to distinguish the veracity of certain tests. Say, for example, when testing the functionality of a list, having the same entries repeated over and over again will make it an exercise in frustration to see which entries come from the current test, or even to differentiate between individual entries. Being able to randomize the data that is inputted into these list objects solves this problem. Let us go over a way we can do this.

By creating a simple XML sheet we can use it to store our strings that will be used during these types of tests. Take, for example, a field that requests a user to input their first name. We can have a list of names in our XML file, as so:

<firstname> <given>Michael</given> <given>Chris</given> <given>Sebastian</given> <given>Charles</given> </firstname>

We create our test as normal. When we arrive at the field in question, we can then utilize this XML file to get a random name and insert it into the field. Then, we might ask, how do we get a random element from the XML? Quite simply we can build a randomizer class that does the work for us. If you are at all familiar with working with XML files within C#, this will be easy, but let’s go over it quickly. First, let’s create our new XML object and load the file:

testDocument = new XmlDocument(); testDocument.Load("TextFieldData.xml");

There are several ways to do what we want, based on the design of our XML file. For simplicity, we are just going to iterate over a single element name. So, we get a list of those elements like so:

XmlNodeList text = testDocument.GetElementsByTagName("given");

Now we would like to generate a random number. This will be the way we choose one of the entries in our element list at random. We’d also like to pass it, in this case, the number of elements in the array so that we have the correct amount and can choose anyone of them:

Random randomValue = new Random(); int value = randomValue.Next(0, text.Count);

We can also put in some console debugging, in case we want to see the contents of the elements and verify that they are returning correctly:

for (int i = 0; i < text.Count; i++) { Console.WriteLine(text[i].InnerText); }

Finally, we return a random element that can then be inputted into our text field:

return text[value].InnerText;

We first instantiate a new randomizer class; in this case, we called our class ‘DataRandomizer’

DataRandomizer text = new DataRandomizer();

Then we get a string object that pulls a random string from our randomizer. If we named our randomizing method ‘GetRandomTextForTextField’, we do as follows:

string newText = text.GetRandomTextForTextField();

Now in our test we call this method within the EnterText method for the text field and so, we have our random text. Thus,

app.EnterText(newText);

When we run the test, we will now get a random string in that field each time. With this knowledge, we can expand this further and add more data for more fields. As our fictional test iterates it will add these to the list view and, as we can observe, we now have semi-unique entries for each object within a list, instead of the monotonous, and unhelpful, single entry ad infinitum.

With some creativity, this concept can be applied to every type of UI element: switches, buttons, tables, pickers, anything that can be manipulated. For more advanced tests, we can even use asserts to verify the presence of the data we expect. If returning more meaningful data is key to a test, skilled randomization will lead you to it.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page