Creating A Calabash Test for an APK
- PDF for offline use:
Let us know how you feel about this.
last updated: 2016-09
At this point, the Calabash gems are installed and an APK has been built for testing. It is now time to start writing the test.
Create the features Folder
The features folder is a special folder that will contain the test code. Calabash can create this folder and provide some boilerplate code to get us started.
Start up a Terminal session (or a Powershell window), and change the working directory to the project folder – the folder that Android Studio created for the project. An example of the project folder is pictured below:
In the project folder, run calabash-android as demonstrated below:
[tom@calabash-sandbox]$ calabash-android gen ----------Question---------- I'm about to create a subdirectory called features. features will contain all your calabash tests. Please hit return to confirm that's what you want. --------------------------- ----------Info---------- features subdirectory created. ---------------------------
This has created a features folder with the following structure:
Lets move on to create the first test.
Creating the First Test
To create the first test, edit the file features/my_first.feature and replace the existing contents with the following:
Feature: Credit card validation. Credit card numbers must be exactly 16 characters. Scenario: Credit card number is too long Given I wait for the "MainActivity" screen to appear When I enter "99999999999999999" into input field number 1 And I press "validateButton" Then I should see "Credit card number is too long."
Here is a quick description of this snippet:
- A Feature must have name, and may optionally provide a description. A Feature is broken down into Scenarios.
- A Scenario is a formal example of how the Feature (or a rule in the Feature) works.
- Scenarios, in turn, are broken down into Steps. This particular feature uses predefined steps. It is also possible to create your own custom steps, but that is beyond the scope of this quick start.
Save the changes to features/my_first.feature.
Running the Test in an Android Emulator
Now that the test is created, lets run it in the emulator. calabash-android may ask you to resign the APK before it can run the test. In the Terminal, type the following:
$ calabash-android resign <PATH TO YOUR APK>
It is not necessary to resign each time you want to run a test. Typically resigning is done once, after the APK is initially compiled.
Running the Test
Now that we have created the test and have resigned the APK, let's run it locally against an Android emulator. Ensure that you have an Android emulator running, and then, at the command prompt, run
$ calabash-android run <PATH TO YOUR APK> No test server found for this combination of app and calabash version. Recreating test server. Done signing the test server. Moved it to test_servers/c3cb231724374ce9291fff1548cc8c0b_0.7.3.apk Feature: Credit card validation. Credit card numbers must be exactly 16 characters. Scenario: Credit card number is too long # features/my_first.feature:4 Given I wait for the "MainActivity" screen to appear # calabash-android-0.7.3/lib/calabash-android/steps/progress_steps.rb:51 When I enter "99999999999999999" into input field number 1 # calabash-android-0.7.3/lib/calabash-android/steps/enter_text_steps.rb:9 And I press "validateButton" # calabash-android-0.7.3/lib/calabash-android/steps/press_button_steps.rb:17 Then I should see "Credit card number is too long." # calabash-android-0.7.3/lib/calabash-android/steps/assert_steps.rb:9 1 scenario (1 passed) 4 steps (4 passed) 0m23.450s
With the test passing locally, it's time to run it in Xamarin Test Cloud. To do this, we must first create a team test run in Xamarin Test Cloud.