Xamarin.UITest is a C# test automation framework that enables testing mobile apps on Android and iOS. Tests are written locally using either physical devices or simulators/emulators and then submitted to Xamarin Test Cloud for more extensive testing across hundreds of devices and Calabash enables you to write and execute automated tests of mobile apps. It's cross-platform, supporting Android and iOS native apps. It is also open source developed and maintained by Xamarin.
First, create a new project for UI Tests inside Xamarin.Forms Project Solution.
Then Install with these NuGet Packages,
- NUnit
- NUnitTestAdapter
- Xamarin.UITest
There are two files we have in Xamarin UI test project that we need to have a deep look
1> AppInitializer.cs
- public static IApp StartApp(Platform platform) {
- if (platform == Platform.Android) {
- return ConfigureApp.Android.StartApp();
- }
- return ConfigureApp.iOS.StartApp();
- }
IApp represents the main gateway to interact an app.
This interface contains the shared functionality between androidApp and iOSApp
2>Tests.cs
-
- [TestFixture(Platform.iOS)]
- [TestFixture(Platform.Android)]
- public class Tests {
- IApp app;
- Platform platform;
- public Tests(Platform platform) {
- this.platform = platform;
- }
- [SetUp]
- public void BeforeEachTest() {
- app = AppInitializer.StartApp(platform);
- }
- [Test]
- public void AppLaunches() {
- app.Repl();
- app.Screenshot("First screen.");
- }
One of the important tools in creating UITests is the REPL (read-eval-print-loop). REPL is a console-like environment in which the developer enters expressions or commands. It will then evaluate those expressions, and display the results to the user.
->app.Repl(): This will open a console with the emulator, then you can type all the commands and see the changes happening in your UI. Its amazing experience, you can write these commands in Tests.cs file, it will run your UI without touching screen and test your UI.
Xamarin UI test works best with automation Id so you need to give automation Id in XAML for every element, then we can use that id where ever we want.
This line will test if my Button UI control is clickable or not.
Example: app.Tap(“Button_Automation ID”)
This line will enter Username into the entry text with automation Id entryText_AutomationId.
-app.EnterText(“entryText_AutomationId”,”Username”)
The REPL is helpful when creating UITests – it allows us to explore the user interface and create the queries and statements so that the test may interact with the application.