Xamarin Test Cloud – All you have to know: part2

Xamarin Test Cloud – All you have to know: part2

testcloudart2

Short introduction

In the previous article I tried to explain what kind of UI Tests frameworks are available on the Xamarin platform. You saw also kind of tests that are possible do. In this article I would like to concentrate on the UI Tests and show how to automatize them.

In the third article I will show how to deploy prepared tests to the Xamarin Test Cloud.

What do I need to start?

1) Xamarin Test Cloud account (you can use free tiral version)

2) Xamarin Test Recorder (either for Windows or MAC)

 

Let’s start

We will concentrate on UITest framework in this article. I will try to show how to use it with NUit framework and write tests that can be deployed on the Xamarin Test Cloud.

This is sample fragment of UITest:

testcloudart3

As you can see instructions are quite understandable. Let me remind you Xamarin UITest architecture:

testcloudart4

Test which you wrote are being launched by Xamarin Test Cloud Client and through the JSON they are passed to the Test Cloud Agent HTTP Server. Automation APIs running the on your app (so for instance button click is performed).

 

What are the possible options to add UITests for applications?

In this article I will prepare all operations in the Xamarin Studio but all of them are also possible to do in the Visual Studio.

There are two possible options to add UITests:

1) UITest App for cross-platform (Xamarin Forms):

testcloudart5

2) UITest App per each platform (Android and iOS):

testcloudart6

testcloudart7

It just depends on your solution structure and on the way you decided to create your apps (Xamarin Forms or Xamarin Platform).

 

Configure tests

In this step I will show how to prepare solution, configure tests and run them. It is worth to say step by step how to do it.

First of all download sample with Android, iOS and UITests projects to start working on them:

Xamarin UITest GitHub sample

Once you open solution it should look like below:

 

testcloudart8_8_8

There are three options to connect our tests to the existing apps (identifying them):

1) Bundle (.app), ipa or .apk – we need to just paste the path to the package on the disc like below:

IApp app = ConfigureApp.iOS .AppBundle(“../../path/mybundle.app”);

IApp  app = ConfigureApp.iOS .AppBundle(“../../path/mybundle.apk”);

 

2) Installed app name – we just need to define the package name of the installed app (on the emulator/simulator or on the device):

IApp app = ConfigureApp.iOS // or Android .InstalledApp(“com.xamarin.taskypro”);

 

3) Defining project in the solution – in the Test panel you have to choose which application project you would like to test:

testcloudart9

IApp is an interface to communicate with currently tested application.

In this example we will use the second option – we will pass the package name.

 

Configure Android tests

First of all you we need to set the name of the application’s package – in this case this will be:

com.xamarin.example.creditcardvalidator

On the Android there is no special configuration to enable testing. Once you have package name set correctly you can launch the application on the emulator (yes do it before tests). In my case I am using Xamarin Android Player emulator (I advice you the same, can download it from here).

The second important thing is that INTERNET permission has to be set – this is crucial for communication with test server.

Once you all above we can launch application on the emulator – you have to always firstly run your app normally on the emulator to install it – then you can test it – if you do not install the app before tests there will be exception shown:

testcloudart10

 

Now we can start writing UITests. The flow will be like below:

a) Wait for the Activity to load

b) Enter 15 digits for the credit card number

c) ap the Validate Credit Card button

d) Confirm the Error Message

 

1) Open “AppInitializer” class. Paste below code to configure app package:

public static class AppInitializer
 {

 public static IApp StartApp(Platform platform)
  {
  if (platform == Platform.Android)
   return ConfigureApp.Android.InstalledApp("com.xamarin.example.creditcardvalidator").StartApp();
 

  if (platform == Platform.iOS)
   return ConfigureApp.iOS.InstalledApp("com.xamarin.example.creditcardvalidator").StartApp();
 
  throw new Exception("AppInitializer: Unsupported platform " + platform);
  }
 }

As you can see we have to set “Platform” parameter to indicate on which platform we want to test the app.

 

2) Open “Tests” class and paste below code (with comments):

  //Set this attribute to indicate which platforms you would like to test:
    [TestFixture(Platform.Android)]
    [TestFixture (Platform.iOS)]
    public class Tests
     {
       //Platform parameter - indicates on which platform Xamarin shoudl launch tests:
       Platform platform;
       //IApp interface is responsible for communication with the app (like clicking buttons or typing in text fields);
       IApp app;

    //This is constructor for the test with setting the platform:
    public Tests(Platform platform)
    {
      this.platform = platform;
    }

    //This is setup before test is launched - below app object is initialized to enable tests:
    [SetUp]
    public void BeforeEachTest()
    {
      app = AppInitializer.StartApp(platform);
    }

    //In this method REPL console in invoked (with REPL we are able to test our app and see the result in console,
    // we can also test our app manually and all actions will be displayed on the console:
    [Test]
    public void AppLaunches()
    {
     app.Repl();
    }
   }

3) Show “Unit Tests” and “Test Resukt” pads to see the result of our tests:

testcloudart11_1

4) On the right side of the Xamarin Studio you should see  “Tests” class for the Android and iOS (because we precised two platforms earlier):

testcloudart12

5) Right click on the “Tests(Android) and select “Run tests”:

REMEMBER that app needs to be instaled on the emulator before you start testing.

Once you run the test app should be opened on the emulator and REPL console should be visible like below:

testcloudart13

 

It is worth to stop here for a while and say few words about REPL tool – Read Evaluate Print Loop.

With this tool user can type expressions and commands to test user interface. REPL will evaluate these expressions and return with the result. It allows us to explore the user interface and create the queries and statements so that the test may interact with the application. You can reed more uner REPL docs.

Now type “tree” in the command and see the result:

testcloudart14

As you can see REPL returned all views for the current Activity. It is really helpful especially when exploring the view hierarchy.

We can test our first command to flash the action bar:

app.Flash(c=>c.Marked("action_bar_title").Text("Enter Credit Card Number"))

We can take this code and paste it in the “Tests” class – this is very useful because you can use it either in the code or in the REPL.

6) Add below method to the “Tests” class just below “AppLaunches()” method:

[Test]
public void CreditCardNumber_TooShort_DisplayErrorMessage()
{
 //Wait for action bar to load:
 app.WaitForElement(c => c.Marked("action_bar_title").Text("Enter Credit Card Number"));
 //Enter 15 digits in the "creditCardNumberTest":
 app.EnterText(c=>c.Marked("creditCardNumberText"), new string('9', 15));
 //Tap the "validateButton":
 app.Tap(c => c.Marked("validateButton"));
 //Wait for the result to display message if credit card nunmber is valid:
 app.WaitForElement(c => c.Marked("errorMessagesText").Text("Credit card number is too short."));
}

Result should look like below:

testcloudart15

In the “Test Results” pad you should see if test passed or not:

testcloudart16

 

Configure iOS tests

1) To enable tests on the iOS you should add NuGet package called Xamarin TestCloud Agent.

This package is required to configure server responsible for running tests on iOS.

testcloudart18

2) Remember to add below code to “AppInitializer” class (if you did not do it before). Your app package name should be exactly like below:

public static class AppInitializer
 {

 public static IApp StartApp(Platform platform)
  {
  if (platform == Platform.Android)
   return ConfigureApp.Android.InstalledApp("com.xamarin.example.creditcardvalidator").StartApp();
 

  if (platform == Platform.iOS)
   return ConfigureApp.iOS.InstalledApp("com.xamarin.example.creditcardvalidator").StartApp();
 
  throw new Exception("AppInitializer: Unsupported platform " + platform);
  }
 }

3) Change the “CreditCardNumber_TooShort_DisplayErrorMessage” to look like below:

[Test]
public void CreditCardNumber_TooShort_DisplayErrorMessage()
{
  app.WaitForElement(c=>c.Class("UINavigationBar").Marked("Simple Credit Card Validator"));
  app.EnterText(c=>c.Class("UITextField"), new string('9', 15));
  app.Tap(c=>c.Marked("Validate Credit Card").Class("UIButton"));

  app.WaitForElement(c => c.Marked("Credit card number is too short.").Class("UILabel"));
}

As you can see we are going to test iOS user interface now. Flow is the same like for Android:

a) Wait for the UINavigationBar to load

b) Enter 15 digits for the credit card number

c) ap the Validate Credit Card button

d) Confirm the Error Message

4) You can test you app either on physical device or on the simulator. To enable tests on the real device go to “Settings” -> “Develolper” -> enable “Enable UI Automation”:

enableuiautomation

This time we will use simulator for tests. Before we launch tests there is one more thing to do. Sometimes you can encaunter problems with running tests on simulators. To solve it please do below steps:

a) Open “Tools” tab in Xamarin Studio and select “Instruments”

b) Then choose “Automation”

c) In the Instruments app choose launched simulator and the app you would like to test:

testcloudart19

d) Click red “record” button to launch the app on the simulator

e) Once its ready you can close it and get back to the Xamarin Studio

 

5) Launch the tests from the “Tests Pad”:

testcloudart20

 

6) Now you can see that app is launched on the simulator and REPL console is opened:

testcloudart17

As you can see UITests are performed and result is displayed in the “Test Results: pad:

testcloudart21

Of course there are many ways to prepare abstraction layer for tests to not duplicate test methods but for this article I decided that the most important to start is to understand how it works in general.

If you are interested in broading your knowledge about cross-platform tests (but not only) I recommend to check “XTC101” and “Xtc102” courses available on the Xamarin University.

Sum up

In this article I showed how to use UITests with Android and iOS applications. Now you know how to use REPL tool, how to launch tests on the Android emulator and iOS simulator. In the third and last article I will show how to deploy tests on the Xamarin Test Cloud.

 

Advertisements