Xamarin.UITest is a testing framework that allows Automated UI Tests written in C# using the NUnit testing framework. It is a framework which is useful to automate iOS and Android applications. It integrates tightly with Xamarin.iOS and Xamarin.Android projects but it can also be used with iOS and Android projects that are written natively in Objective-C/Swift and Java. Xamarin.UITest is the Automation Library that allows the NUnit tests to execute on .apk and .ipa file. This automation framework interacts with the application such as a user interacts with the application. This framework feeds each control like "Labels, Textbox, Images, Button" etc., and perform events like "Tap, Swipe Gesture, enter values in the textbox," etc.
Xamarin.UITest is written as a method that is referred to as a test. The class which contains the test is known as a test fixture. The test fixture contains either a single test or a logical grouping of tests and is responsible for any setup to make the test run and any cleanup that needs to be performed when the test finishes. Each test should follow the 3 main pillars of the automation as below.
- Arrange - Automation is the process of some action based on conditions and initialization of the components.
- Act - Act is a process of interaction with the application as like human insert value into the textbox, tap on buttons and all.
- Assert - Assert is the process of cross-examination of the result of Act performed with the help of Arrange. It is a process of the match Act's result with a pre-define expected result.
What is Xamarin.UITest API?
Xamarin Unit test interacts with the mobile application through an interface Xamarin.UITest.IApp. This interface defines methods which are responsible for the collaboration of the application and user interface. This is mainly useful for two platforms like iOS and Android.
- Xamarin.UITest.iOS.iOSApp This class is used to perform automation tests on the iOS application.
- Xamarin.UITest.Android.AndroidApp This class is used to perform automation on the Android application.
iOS and Android Apps are not instantiated directly. Both types of applications are instantiated with the help of ConfigureApp class. This class ensures that the iOS or Android app is instantiated properly.
Mostly IApp interface is used for each test case performed in the automation. This interface is to prevent spilling one test into another. It is useful to maintain the purity of the test case. Generally, the NUnit test initializes an instance of IApp in the below two places.
- In the SetUp method Typically, a test fixture is a logical grouping of related tests, each of them running independently of the other. In this scenario the IApp should be initialized in the SetUp method, ensuring that a new IApp is available for each test.
- In the TestFixtureSetup method, in some situations, a single test may require its own test fixture. In this case, it may make more sense to initialize the IApp object once in the TestFixtureSetup method.
AppInitializer class contains the initialization of the app according to the platform.
- public class AppInitializer {
- public static IApp StartApp(Platform platform) {
- if (platform == Platform.Android) {
- IApp iApp = ConfigureApp
- .Android
- .ApkFile("/Users/Documents/ABC/ABC.Android/bin/Release/ABC.apk")
-
- .Debug()
- .EnableLocalScreenshots()
- .StartApp(Xamarin.UITest.Configuration.AppDataMode.Clear);
- return iApp;
- } else {
- IApp app = ConfigureApp
- .iOS
- .StartApp();
- return app;
- }
- }
- }
Mostly, the IApp interface is used for each test case performed in the automation. This interface is to prevent spilling one test into another. It is useful to maintain the purity of the test case. Generally, the NUnit test initializes an instance of IApp in below two places.
- TextFixture is a grouping of same test cases. Each Text fixture runs independently. In this case, IApp should be initialized in the Setup method of each test fixture. So, each test fixture has its own instance that maintains the purity of the test fixture.
- TestFixtureSetup Some scenario single test may require its own test fixture. In that case, we should initialize the IApp object into the TestFixtureSetup method.
Test Class describes all the test cases as methods.
- [TestFixture(Platform.Android)]
- [TestFixture(Platform.iOS)]
- public class Tests
- {
- protected IApp app;
- protected Platform platform;
- Pages.LoginPage loginPageObj { get; set; }
- Pages.GuideViewPage guideViewPageObj { get; set; }
- Pages.HamburgerMenuPage hamrgerMenuPageObj { get; set; }
- public Tests(Platform platform)
- {
- this.platform = platform;
- }
-
- [SetUp]
- public void BeforeEachTest()
- {
- if (this.app == null)
- {
- this.app = AppInitializer.StartApp(platform);
- loginPageObj = new LoginPage(this.app, this.platform);
- hamrgerMenuPageObj = new HamburgerMenuPage(this.app, this.platform);
- }
- else
- hamrgerMenuPageObj.DoLogout(this.platform);
-
- loginPageObj.DoLogin();
- }
-
- [TearDown]
- public void AfterEachTest()
- {
- hamrgerMenuPageObj.DoLogout(this.platform);
- }
BeforeEachTest method is decorated with [Setup] Attribute, which allows basic setup before executing testcase.
AfterEachTest method is decorated with [TearDown] Attribute, which executes after each test case so basically we use it for logging out of the app after each test case.
Now, let's move to write a test case.
- #region Test Cases
- [Test]
- public void A_VerifyLoginPageTestCase()
- {
- Boolean result = true;
- var UserName = loginPageObj.ValidateTextOnLoginScreen("placeholder", "username");
- Assert.That(UserName, Is.EqualTo(result));
-
- var Password = loginPageObj.ValidateTextOnLoginScreen("placeholder", "password");
- Assert.That(Password, Is.EqualTo(result));
-
- var LogInButton = loginPageObj.ValidateTextOnLoginScreen("id", "submitButton");
- Assert.That(LogInButton, Is.EqualTo(result));
-
- }
LoginPage class example,
- class LoginPage : BasePage
- {
- Platform platform;
- public LoginPage(IApp app, Platform platform) : base(app, "LoginPage")
- {
- this.platform = platform;
- }
-
- public void DoLogin(String username = "Username", String password = "Password", Boolean valid = true)
- {
- Query LoginHeader = x => x.Id("Login");
- AppInitializer.StartApp(platform);
- BasePageTests.WaitForWebElement(this.app, this.app.Query(x => x.XPath("//input[@name='email']")), 30);
- BasePageTests.WaitForWebElement(this.app, this.app.Query(x => x.XPath("//input[@name='password']")), 10);
- BasePageTests.WaitForWebElement(this.app, this.app.Query(x => x.XPath("//button[@type='submit']")), 10);
- this.FocusOnEmailField(username);
- this.FocusOnPasswordField(password);
- this.ClickOnSubmitButton();
-
- if (valid == false)
- {
- BasePageTests.WaitForWebElement(this.app, this.app.Query(x => x.XPath("//h3")));
- }
- else
- this.app.WaitForElement(x => x.Marked("Dashboard"), "Dashboard Page is not displaying in 60 seconds", new TimeSpan(0,0,60,0); );
- }
- }
Here, I am using 1 abstract class for basic settings
- abstract class BasePage {
- #
- region Constructors
- protected BasePage(IApp app, string pageTitle) {
- this.app = app;
- PageTitle = pageTitle;
- }#
- endregion
-
- # region Properties
- public string PageTitle {
- get;
- }
- protected IApp app {
- get;
- }#
- endregion
-
- # region Methods
- public virtual void WaitForPageToLoad() => app.WaitForElement(x => x.Marked(PageTitle));#
- endregion
- }
1 static class for maintaining static funtion of the automation
- public class BasePageTests {
- public static Boolean IsElementPresent(IApp app, Query locatorQuery, int waitInSeconds) {
- try {
- app.WaitForElement(locatorQuery, "Failed to find the " + locatorQuery + " in " + waitInSeconds + " seconds.", new TimeSpan(0, 0, 60, 0));
- int count = app.Query(locatorQuery).Length;
- return (count == 1);
- } catch (Exception ex) {
- Console.Write(ex.StackTrace);
- }
- return false;
- }
- }
To get the mobile screen controls we have REPL (Read Eval Print Loop) method which opens the console with application details.
In the console window, type tree. This will open a screen in a tree view as per the image.