One of the things one should never say when delivering an app to a customer- “It was working fine on my device”. To avoid such embarrassment and to make sure the users see what was planned for them to see, test should be written.
I wrote a simple Buzzword Bingo App, called “Bullshit Bingo”, to try and demonstrate how easy it is to write an Acceptance Test with Xamarin.UITests.
About the app:
Bullshit Bingo is game with one simple objective – collect 5 phrases either in a row or in a column. It has 2 Activities – a
MainActivity, that holds the controls to start the game, and a
GameActivity, that shows the actual bingo card the user interacts with.
When I created my Xamarin.Android Solution, it came with two projects – BullshitBingo and BullshitBingo.UITests. In UITests there is a sample
Tests.cs file, where the magic happens.
Each UI test should follow 3 simple steps: Arrange, Act, Assert:
- Setup your test, initialize things needed at a later point
- Do some actions, interact with the app
- Make sure you got the right result.
In order for Xamarin.UITests to know what to run in the project, the classes need to be annotated as
[TestFixture] and the methods as
<code class="language-csharp">[TestFixture] public class TestWinningConditions``` For this demo, I wanted to test only the winning conditions of the app, so I had to make sure the tests were autonomous. To do that, I defined a method, to run Before Each Test to reset the app to it’s original state:
public void BeforeEachTest ()
app = ConfigureApp.Android.StartApp ();
public void VerticalPhrases()
REPL is a great tool to explore the UI and view details about all elements displayed in the current view. It helps to generate a sequence of actions to be used in the test later. [![REPL](http://pumpingco.de/content/images/2015/08/REPL.gif)](http://pumpingco.de/content/images/2015/08/REPL.gif) So mytest for the horizontal winning condition looked like this:
public void HorizontalPhrases ()
app.Tap (x => x.Class ("TextView").Index (0));
app.Tap (x => x.Class ("TextView").Index (1));
app.Tap (x => x.Class ("TextView").Index (2));
app.Tap (x => x.Class ("TextView").Index (3));
app.Screenshot ("4 in a row are selected");
app.Tap (x => x.Class ("TextView").Index (4));
app.WaitForElement (x => x.Id ("alertTitle")); var results = app.Query (x => x.Id ("alertTitle")); Assert.AreEqual ("You won, congratulations!", results .Text); app.Screenshot ("A Congratulating dialog should appear"); }```
I found out, that when it comes to switching between activities, it’s better to wait until the elements from the new activity are loaded with
app.WaitForElement()and not rely on UITest to do that for me. In 5 out of 10 runs the test tried to tap on an element that didn’t exist.
TL,DR: Test are important, use Xamarin.UITest, it’s very easy, see the Introduction to UITest for a reference to all possible user interface interactions
IApp has to offer.