According to the Xamarin website (https://developer.xamarin.com/guides/testcloud/uitest/), Xamarin.UITest is an Automated UI Acceptance Testing framework that allows programmers to write and execute tests in C# and NUnit that validate the functionality of an application.
That means we can write test methods using C# to test the functionality of an Xamarin application. For example, we can test these and many more scenarios:
Mouse
into the search field, click on the Find button, wait for the result list to be filled, and check if there are 25 items in the listXamarin.UITest uses the Xamarin Test Cloud Agents. Xamarin Test Cloud Agent is an HTTP server that will communicate with the mobile application being tested.
Unfortunately, we can only test Android and iOS applications with the Xamarin.UITest framework. It is not possible to test Windows Phone applications at the time of writing.
Let's open Xamarin Studio and create a sample UITest project:
UITestSample
into the App Name field.
The only difference is that the UITestSample.UITests
project was created with solution.
Let's investigate the Test.cs
file inside the UITestSample.UITests
project by double-clicking on it.
We can see that the Tests
class is annotated with a [TestFixture]
attribute.
The TestFixture
attribute turns the Tests
class into a test methods container class.
When testing starts, the TestFixture
annotated classes begin to run.
First of all, a method with the [SetUp]
attribute annotation is executed. This method handles starting test logic.
Then all the methods with the [Test]
attribute annotation are executed.
We should only write the [Test]
attribute annotated methods. These methods basically do whatever the client (the application's end user) does with our application by automation:
[TestFixture] public class Tests { AndroidApp app; [SetUp] public void BeforeEachTest() { app = ConfigureApp.Android.StartApp(); } [Test] public void WelcomeTextIsDisplayed() { AppResult[] results = app.WaitForElement(c => c.Marked("Welcome to Xamarin Forms!")); app.Screenshot("Welcome screen."); Assert.IsTrue(results.Any()); } }
We can easily see that only the test is waiting for the Welcome to Xamarin Forms! label on the screen.
The TextFixture
attribute changes classes to test classes. All the methods with a Test attribute listed in the Unit Test Panel can be run to test.
We created a test method to test if the label contains the Welcome to Xamarin Forms! text.
The WaitForElement
method iteratively queries the parameter to find the matching element. If there is no element that can be found in a timely fashion, TimeoutException
will be thrown.
Some of the other methods are listed here:
ClearText
EnterText
Tap
DoubleTap
TouchAndHold
PinchToZoomIn
PinchToZoomOut
PressVolumeDown
PressVolumeUp
Screenshot
ScrollDown
ScrollTo
ScrollUp
SetOrientationPortrait
SetOrientationLandscape
SwipeRight
SwipeLeft
We may use these methods to create any test scenario we want using C#. If we use the WaitForElement
method, its first parameter is type of AppQuery
. The AppQuery
class lets us write a query and returns matched view elements.
Some of the methods we may use with AppQuery
are listed here:
All
Button
Id
Index
Child
Parent
TexField
Marked
These methods take parameters and find the matched view elements.
For example, the All()
method returns all view elements in a page.
Another example, the Marked()
method, takes a parameter and finds its value in all view elements' ID or text fields in page and returns an element if found.
Assert calls are for what we want to test.
We should open the Unit Tests panel by clicking View | Unit Testing in the Xamarin Studio IDE:
We can run individual tests by right-clicking and clicking Run on the menu, or we can run all tests by clicking the Run All button.
When tests are completed to run they should be green. If there is a red test we should check what is wrong and fix it to make the test green again:
We can easily use the following methods to traverse and interact with the application's UI surface:
Button
Id
Index
Text
TextField
Class
Marked
Tap
Parent
Query
PressEnter
EnterText
WaitForElement
Screenshot
With these methods we can easily write thousands of tests to test the application's UI and execute them within seconds.
For example, if we need to test a page with an e-mail address entry box, we may use the following code block:
app.WaitForElement(c => c.Marked("lblEmail").Text("Enter Email")); app.EnterText(c=>c.Marked("txtEmail"), "test@test"); app.Tap(c => c.Marked("btnSend")); app.WaitForElement(c => c.Marked("lblError").Text("Entered email address is invalid"));
The following list is the test flow:
Running test wait for an element appear on the screen, named lblEmail
, have Enter Email
Running test enter test@test to txtEmail
named element
Running test click on the btnSend
named element
Running test wait for the Entered email address is invalid
text appears in the lblError
named element
18.222.182.105