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

22 comments:

  1. Im trying to use calabash and android for testing. So it is just a started and installed calabash. The problem comes when I try to execute the first test when I run:

    calabash-android run myAPP.apk

    I get a response:

    `raise_if_android_home_not_set': Please set the ANDROID_HOME environment variable (RuntimeError)

    How do I setup ANDROID_HOME? Where do I setup ANDROID_HOME? From your experience, do you recommend calabash? Thanks

    ReplyDelete
  2. Which operating system are you using?

    ReplyDelete
  3. I'm using windows 7 and
    set ANDROID_HOME="C:\Program Files\Android\android-sdk"

    but when I try running the command I get now

    No test server found for this combination of app and calabash version. Recreating test server. D:/workspace/calabash/android/vendor/bundle/ruby/1.9.1/gems/calabash-android-0.3‌​.8/bin/calabash-android-build.rb:9:in calabash_build': No Android SDK found in "C:/Program Files/Android/android-sdk"/p latforms/ (RuntimeError).

    Any clue how I could overcome this?

    ReplyDelete
  4. Before the calabash-android run command: From the terminal just run:
    export ANDROID_HOME=/Applications/adt-bundle-mac-x86_64/sdk

    Were you replace the path with the path of your sdk

    OR
    ===

    Edit your ~/.profile file and add in the line:
    export ANDROID_HOME=PATH_TO_WHERE_SDK_INSTALLED
    Where PATH_TO_WHERE_SDK_INSTALLED is replaced by the actual path to android sdk.

    ReplyDelete
  5. Hi when i try to export my project from eclipse its showing like "action cannot be done java.lang.nullpointer exception"

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  6. Dear All,

    i am facing the samlier type of problem can any one help me what is worng with me i am new to calabrash.
    my error text as while i am runing app

    No test server found for this combination of app and calabash version. Recreatin
    g test server.
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/lib/calabash-and
    roid/helpers.rb:168:in `block (2 levels) in fingerprint_from_apk': No RSA file f
    ound in META-INF. Cannot proceed. (RuntimeError)
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/lib
    /calabash-android/helpers.rb:161:in `chdir'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/lib
    /calabash-android/helpers.rb:161:in `block in fingerprint_from_apk'
    from C:/Ruby193/lib/ruby/1.9.1/tmpdir.rb:83:in `mktmpdir'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/lib
    /calabash-android/helpers.rb:160:in `fingerprint_from_apk'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/bin
    /calabash-android-build.rb:3:in `calabash_build'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/lib
    /calabash-android/helpers.rb:62:in `build_test_server_if_needed'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/bin
    /calabash-android-run.rb:16:in `calabash_run'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.7.pre9/bin
    /calabash-android:76:in `'
    from C:/Ruby193/bin/calabash-android:23:in `load'
    from C:/Ruby193/bin/calabash-android:23:in `'

    ReplyDelete
  7. This is because the .apk is NOT signed. Refer Step 5 above and get your apk signed

    ReplyDelete
  8. Dear All,

    I am trying to get date picker field value but I getting error like that

    And I set the date to "20-02-1985" on DatePicker with index "6"
    Step unsuccessful: DatePicker with index 5 is not available! (RuntimeError)
    /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
    features/new_registration.feature:36:in `And I set the date to "20-02-1985" on DatePicker with index "6"'


    can you please help me to get the datepicker value in calabash android.

    ReplyDelete
  9. Can anyone explain me this?
    YOG
    To test and debug your application, the build tools sign your application with a special debug key that is created by the Android SDK build tools.

    Does this talk about this location?
    D:\adt-bundle-windows-x86-20130729\adt-bundle-windows-x86-20130729\sdk\build-tools

    if YES, then it means build tool only sign the Android Application,then what the use of JARSIGNER? Please help me..

    ReplyDelete
  10. Hi Amit, It's a great article. I have been able to reach somewhere using the steps given in here. I need help to set up automation completely. At step 5, I got the first error and I ran

    C:\Documents and Settings\Terry\.android>jarsigner -keystore debug.keystor
    torepass android -keypass android AndroidDemoProject.apk androiddebugkey

    And it worked and I copied the file & put it in the right directory. Now I am getting this error:

    D:\AndroidDempProject>calabash-android run AndroidDemoProject.apk
    No test server found for this combination of app and calabash version. Recreatin
    g test server.
    NoMethodError: undefined method `gsub' for main:Object
    (root) at -e:1
    RuntimeError: Could not replace package name in manifest
    calabash_build at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/bin/calabash-android-build.rb:37
    chdir at org/jruby/RubyDir.java:465
    calabash_build at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/bin/calabash-android-build.rb:32
    mktmpdir at D:/automation/jruby/lib/ruby/1.9/tmpdir.rb:97
    calabash_build at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/bin/calabash-android-build.rb:31
    build_test_server_if_needed at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/lib/calabash-android/helpers.rb:70
    calabash_run at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/bin/calabash-android-run.rb:16
    (root) at D:/automation/jruby/lib/ruby/gems/shared/gems/c
    alabash-android-0.4.14/bin/calabash-android:71
    load at org/jruby/RubyKernel.java:1046
    (root) at D:\automation\jruby\bin\calabash-android:23

    Any help is much appreciated.

    Thanks

    ReplyDelete
  11. Hi,

    From your blog, I found the APK need to re-signed before starting the test.
    I would like to suppose Calabash framework cannot test against native / pre-installed Apps, may I right?

    Thx & B.R

    ReplyDelete
  12. I don't found file debug.keystore to create signed jar.

    ReplyDelete
  13. Hi, thank for your article, but I have some problem with test. Can you help me? My log:

    No test server found for this combination of app and calabash version. Recreatin
    g test server.
    Done signing the test server. Moved it to test_servers/f0d89e902e0ddadaaa84ea31c
    0deb0b9_0.4.21.apk
    *** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansic
    on/) to get coloured output on Windows
    Feature: Demo feature

    Scenario: I can start my app # features\my_fir
    st.feature:3
    More than one device connected. Specify device serial using ADB_DEVICE_ARG (Ru
    ntimeError)
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:413:in `default_serial'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:194:in `initialize'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:42:in `new'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:42:in `default_device'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:72:in `uninstall_apps'
    D:/AndroidDemoProject/features/support/app_installation_hooks.rb:22:in `Before
    '
    No connected devices (RuntimeError)
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:412:in `default_serial'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:194:in `initialize'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:42:in `new'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:42:in `default_device'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-andro
    id/operations.rb:93:in `start_test_server_in_background'
    D:/AndroidDemoProject/features/support/app_life_cycle_hooks.rb:5:in `Before'
    And I wait for "Hello World, AndroidDemoProject" to appear # calabash-androi
    d-0.4.21/lib/calabash-android/steps/progress_steps.rb:27
    And take picture # calabash-androi
    d-0.4.21/lib/calabash-android/steps/screenshot_steps.rb:1
    No connected devices (RuntimeError)
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-a
    ndroid/operations.rb:412:in `default_serial'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-a
    ndroid/operations.rb:194:in `initialize'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-a
    ndroid/operations.rb:42:in `new'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-a
    ndroid/operations.rb:42:in `default_device'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.4.21/lib/calabash-a
    ndroid/operations.rb:101:in `screenshot_embed'
    D:/AndroidDemoProject/features/support/app_life_cycle_hooks.rb:10:in `Afte
    r'

    Failing Scenarios:
    cucumber features\my_first.feature:3 # Scenario: I can start my app

    1 scenario (1 failed)
    2 steps (2 skipped)
    0m4.705s

    ReplyDelete
  14. Hi, Your blog is too good.
    I have issue in re-signing apk file with below command but getting error with Jdk version - 7 and OS - XP

    C:\Documents and Settings\user\.android>jarsigner -keystore debug.keystore -stor
    epass android -keypass android -sigalg MD5withRSA -digestalg SHA1 LearnArabic_de
    bug.apk
    Error: Could not find or load main class sun.security.tools.JarSigner

    ReplyDelete
  15. HI Amith,
    Your blog is too good, can you help me
    i got error No test server so i have followed the step 5 resign jar, but i am getting error the "system cannot find
    the file specified" . I have cross check the path multiple time even though i am getting same error

    ReplyDelete
  16. Hi Amith,

    Your blog is very helpful for me.. I have installed everything, but if i try to run apk, i got below error.. Can u help me.
    Error : The first parameter must be the path to the apk file.

    ReplyDelete
  17. Getting the same error as Sathya, "The first parameter must be the path to the apk file". Any solution to this?

    ReplyDelete
  18. Hey guys, this also took me a good while to figure out, from I did and pushed through with the following solution:

    I got Ruby Version 1.9 and the DeveloperKit (32bit version) from http://rubyinstaller.org/downloads/
    This happens due to a Problem with the latest Ruby, so it needs the 1.9 version due to conflicts

    once you have that installed gem install cucumber version 1.3.20 I had to Uninstall cucumber 2.0.2 cause it was creating conflicts of some sort you can do this by first saying

    gem uninstall cucumber
    select what version you would like to uninstall

    and from there calabash-android run DemoAPK

    give that a shot and lemme know :)

    ReplyDelete
    Replies
    1. and also add your developer kit to your Environment PATHs

      Delete
  19. oh wow! your suggestions on that unsigned apk error worked out really well for me. Thanks a ton !!

    ReplyDelete
  20. Hello
    i am facing this error
    The first parameter must be the path to the apk file.

    ReplyDelete