Wednesday, July 10, 2013

calabash-android Input Steps

INPUT STEPS

Then /^I toggle checkbox number (\d+)$/ do |checkboxNumber|
Toggles checkout with the specified index.

Then /^I long press "([^\"]*)"$/ do |text_to_press|
Long presses the text if found.

Then /^I long press "([^\"]*)" and select item number "([^\"]*)"$/ do |text_to_press, index|
Long presses the text (if found) and select the specified number in the context menu that appear.

Then /^I long press "([^\"]*)" and select "([^\"]*)"$/ do |text_to_press, context_text|
Long presses the text (if found) and select the specified number in the context menu that appear.

Given /^I set the date to "(\d\d-\d\d-\d\d\d\d)" on DatePicker with index "([^\"]*)"$/ do |date, index|
Finds the datepicker with the specified index and changes the date.

Given /^I set the time to "(\d\d:\d\d)" on TimePicker with index "([^\"]*)"$/
Given /^I set the "([^\"]*)" time to "(\d\d:\d\d)"$/
Finds the timepicker with the specified index and changes the time.

Given /^I set the "([^\"]*)" date to "(\d\d-\d\d-\d\d\d\d)"$/ do |content_description, date|
Find the datepicker by content description and changes the date.

Then /^I enter "([^\"]*)" into input field number (\d+)$/ do |text, number|
Enters the specified text into the input field with index number.

Then /^I enter text "([^\"]*)" into field with id "([^\"]*)"$/ do |text, view_id|
Enters Text into the input field with id view_id.

Then /^I enter "([^\"]*)" as "([^\"]*)"$/ do |text, target|
Then /^I enter "([^\"]*)" into "([^\"]*)"$/ do |text, target|
Enters the specified text into the input field that has a content desciption that matches the provided target.

Then /^I clear input field number (\d+)$/ do |number|
Finds a input field by index and blanks its value.

Then /^I clear "([^\"]*)"$/ do |name|
Finds a input field with matching content description and blanks its value.

Then /^I clear input field with id "([^\"]*)"$/ do |view_id|
Finds the input field with id view_id and clears the text from it.

Then /^I select "([^\"]*)" from "([^\"]*)"$/ do |item_text, spinner_content_description|
Finds the Spinner by 'spinner_content_description' and selects the item with the matching 'item_text'

calabash-android Waiting Steps

WAITING STEPS

This is one of the major aspects of any Test Automation. Waiting for Object (Image,Text,Label,Link, Button etc) to appear before launching\Perform the next user action.

The Custom Steps to WAIT are as described below:

Then /^I wait for progress$/ do
Will wait till there is no more progress bars.

Then /^I wait for dialog to close$/
Waits for the current dialog to close.

Then /^I wait for "([^\"]*)" to appear$/
Then /^I wait to see "([^\"]*)"$/
Waits for the specified text to appear.

Then /^I wait up to (\d+) seconds for "([^\"]*)" to appear$/
Then /^I wait up to (\d+) seconds to see "([^\"]*)"$/
Waits for the specified text to appear, with a custom timeout

Then /^I wait for the "([^\"]*)" button to appear$/
Waits for a button with the specified text to appear.

Then /^I wait for the "([^\"]*)" screen to appear$/
Waits for a particular screen (Android Activity) to appear.

Then /^I wait for the view with id "([^\"]*)" to appear$/ do |text|
Waits for a view view that id to appear. Note that use the short name and not the fully quantified name. That means if your id is 'com.foo.R.id.bar_label' you would use 'I press view with id "bar_label"'.

Then /^I wait up to (\d+) seconds for the "([^\"]*)" screen to appear$/
Then /^I wait upto (\d+) seconds for the "([^\"]*)" screen to appear$/
Waits for a particular screen (Android Activity) to appear with a timeout.

Then /^I wait for 1 second$/
Then /^I wait for a second$/
Waits for one second.

Then /^I wait$/
Waits for two seconds.

Then /^I wait for (\d+) seconds$/
Waits for a specified number of seconds

calabash-android Buttons Steps

BUTTONS STEPS

Simulates that the user pressed the back button.
Then /^I go back$/

Simulates that the user pressed the menu button.
Then /^I press the menu key$/

Simulates that the user pressed the enter button on the keyboard.
Then /^I press the enter button$/ do

Sunday, June 2, 2013

calabash-android Gestures Steps

Gestures Steps


To swipe left 
Then /^I swipe left$/

To swipe right
Then /^I swipe right$/

To scroll down
Then /^I scroll down$/

To scroll up
Then /^I scroll up$/

To open the menu and press the specified text
Then /^I select "([^\"]*)" from the menu$/

To drag from one point on the screen to another.
Then /^I drag from (\d+):(\d+) to (\d+):(\d+) moving with (\d+) steps$/ 

Note: x:y co-ordinates are expressed as percentages of the screen width:height

 

calabash-android Assertion Steps

Assertion steps

To assert that specified text can be found use any of the following steps.
Then /^I see the text "([^\"]*)"$/
Then /^I see "([^\"]*)"$/
Then /^I should see "([^\"]*)"$/
Then /^I should see text containing "([^\"]*)"$/

To assert that specified text cannot be found use any of the following steps.
Then /^I should not see "([^\"]*)"$/
Then /^I don't see the text "([^\"]*)"$/
Then /^I don't see "([^\"]*)"$/ 
 
Scenario 1:
===========
Given a App
After the App Launch - You need to Verify the text "HelloWorld" Appears
 
Sample Code :
============= 
Feature: Demo feature
Scenario : Verify after App Launch - The text "HelloWorld" appears
And I should see "HelloWorld"

Pros and Cons of using calabash-android

Pros of using calabash-android:

1. It is an Open-Source Tool  - No Licensing Fees.
2. Since - The calabash-android is based on the Cucumber framework. The test cases can be easily created 
    in real simple language.
3. Support for all the basic events and movements on the mobile are present in the libraries.
4. It has a thriving forum and Google Group: “Calabash Android”.

Cons of using calabash-android:

1. It takes time to run on an emulator or device as it always installs the app first before starting the first 
    scenario.
2. If a step fails then the subsequent tests in the scenario are skipped.
3. It is still in its nascent stage. Support for many complex scenarios or events is not supported. For that 
    either you have to code your way in Ruby or wait for these supports to appear on the scene.
4. We must have the code of the app for identifying the ids of various elements.

Monday, May 27, 2013

Calabash-android for automating tests

Please refer the section - Setup for calabash-android

1) Create the sample .apk 
For learning purpose download calabash-android-demoapp 
    - Download the zip file "calabash-android-demoapp-master.zip"
    - Create a folder "AndroidDemoProject"
    - Extract the zip file to "AndroidDemoProject" folder
    - Launch Eclipse
    - Navigate File > Import > and Select - Existing Android code into workspace
    - Click Next
    - Import Project - Browse and Select \\AndroidDemoProject\calabash-android-demoapp-master
    - Click Finish
    - Select project AndroidDemoProject and right-click
    - From the menu pop-up select Android Tools > Export Unsigned Application Package

2) Save AndroidDemoProject.apk file to the folder

3) Generate a Cucumber skeleton :
  In CMD prompt, go to the  "AndroidDemoProject" folder and Run the command C:\AndroidDemoProject\calabash-android gen
It will create a Cucumber skeleton in the current folder like this:

features
|_support
| |_app_installation_hooks.rb
| |_app_life_cycle_hooks.rb
| |_env.rb
|_step_definitions
| |_calabash_steps.rb
|_my_first.feature
 


4) Open the file my_first.feature and enter the following text:

 Feature: Demo feature
 Scenario: I can start my app
    And I wait for "Hello World, AndroidDemoProject" to appear
    And take picture

5) Finally - Execute Test code:
  From CMD prompt C:\AndroidDemoProject\calabash-android run AndroidDemoProject.apk

*** In Case you get the following Error****
C:\AndroidDemoProject>calabash-android run AndroidDemoProject.apk
No test server found for this combination of app and calabash version. Recreating test server.
C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.4/lib/calabash-android/
helpers.rb:145:in `block (2 levels) in fingerprint_from_apk': No RSA file found in META-INF. Cannot proceed. (RuntimeError)

This is because the .apk should be signed

Follow the Steps below to overcome the above Error
  • Copy AndroidDemoProject.apk file to this folder - C:\Users\<UserName>\.android>
  • In CMD prompt, go to this location: C:\Users\<username>\.android
  • Type > jarsigner -keystore debug.keystore -storepass android -keypass android AndroidDemoProject.apk androiddebugkey > click enter. 
  • We are done with the signing part
  •  Copy AndroidDemoProject.apk and replace file in folder AndroidDemoProject 
Repeat Step 5 above - The Test should be Successful

*** In Case Even after signing the .apk - you are getting the following bellow error****

android.util.AndroidException: INSTRUMENTATION_FAILED: sh.calaba.demoproject.tes
t/sh.calaba.instrumentationbackend.CalabashInstrumentationTestRunner
        at com.android.commands.am.Am.runInstrument(Am.java:676)
        at com.android.commands.am.Am.run(Am.java:119)
        at com.android.commands.am.Am.main(Am.java:82)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
        at dalvik.system.NativeStart.main(Native Method)
  App did not start (RuntimeError)


You need to resign the .apk

Follow the Steps below to overcome the above Error
  • From CMD prompt C:\AndroidDemoProject\calabash-android resign AndroidDemoProject.apk
Repeat Step 5 above - The Test should be Successful

*** In Case you get the following Error****
No connected devices (RuntimeError)
Connect your "mobile device" or "simulator" where you want to test your code:
Repeat Step 5 above - The Test should be Successful

Saturday, May 18, 2013

Usefull Commands

1. To Start emulator from command prompt :
emulator @yourAvdName

2. To Run a specified feature file :
calabash-android run <your_apk_file>.apk features/<feature_1>.feature  

3. To Generate HTML Report
calabash-android run <your_apk_file>.apk --format html --out test_report.html

4. command to know the installed calabash\cucumber gem version
gem list calabash-android
gem list calabash-cucumber

JumpStart Android Testing using Calabash

This Post will help you to write your First Functional Test using calabash and cucumber.

Before we Jump Start to this - Let me ASK you two questions :

1. Do you have the .apk file of the mobile app you are planning to test ?
If  NO - please get it first and put it in some folder say C:\My_Android_App\<your_apk_file>.apk

2. Do you have install the pre-requisties tools ?
If NO - Please refer the section - Setup for calabash-android 

STEP 1:
Using command prompt Navigate to the folder where you have your .apk file

STEP 2:
Generate a Cucumber skeleton :
Run the command C:\My_Android_App\calabash-android gen
It will create a Cucumber skeleton in the current folder like this:

features
|_support
| |_app_installation_hooks.rb
| |_app_life_cycle_hooks.rb
| |_env.rb
|_step_definitions
| |_calabash_steps.rb
|_my_first.feature
 


In this skeleton you find all the predefined steps that comes with calabash. Try to take a look my_first.feature and change it to fit your app.

STEP 3:
Resign your apk with the default "debug.keystore" available at C:\Users\<your_user_name>\.android
Run the command C:\My_Android_App\calabash-android resign <your_apk_file>.apk

STEP 4:
Connect your "mobile device" or "simulator" where you want to test your code:
Run the command C:\My_Android_App\adb devices
*** The device or simulator should show in the List of devices attached***

STEP 5:
Finally - Execute Test code:
Run the command C:\My_Android_App\calabash-android run <your_apk_file>.apk

Setup for Calabash-android

The First and foremost things is to have all the prerequisites tools installed in your system. The below are the step by step explanation of how to quickly setup your development environment for functional testing of mobile apps (android) using Cucumber and Calabash-android.

Pre-Requisites:

1. You need to have Ruby installed. Verify your installation by running ruby -v in a terminal - it should print "ruby 1.8.7" (or higher).



***If you are on Windows you can get Ruby from RubyInstaller.org ***
http://rubyinstaller.org/downloads/

2. You should have the Android SDK installed and the environment variable ANDROID_HOME should be pointing to it.


Note :  Make Sure Following Environment variables should be set properly
====================================================
Set the Android path to the directory where you have the android sdk installed :
ANDROID_HOME=C:\Android-SDK\adt-bundle-windows-x86_64-20130219\sdk

STEP 1 : Install Cucumber
Navigate to Command Prompt and type "gem install cucumber"



*** In case you face any problem refer Tips Section***

STEP 2 : Install Calabash-android
Navigate to Command Prompt and type "gem install calabash-android"




***You might have to run sudo gem install calabash-android if you do not have the right permissions.***

*** In case you face any problem refer Tips Section***


That's all ....
YOU are now set to write your First Functional Test - Refer "JumpStart Android Testing using Calabash"