Google+ Followers

Friday, 31 January 2014

calabash-android: common problems with solutions (screenshots attached)

1) java.lang.SecurityException: Permission Denial: starting instrumentation ComponentInfo{com.appname.test/sh.calaba.instrumentationbackend.ClearAppData} from pid=2904, uid=2904 not allowed because package com.appname.test does not have a signature matching the target com.appname.

this is seen when you are trying to run tests against app which was built on different machine but tests are run from your machine. In this case application built on different machine will have different signature (incase you haven't shared the keystore file with them) and your test server cannot communicate with the app

Solution: 
manually uninstall app from device
take the apk file and resign it - calabash-android resign app.apk
now run tests , they will work fine

2) invalid multibyte char (US-ASCII)
Add below on top of your ruby source files.
# encoding: utf-8




Friday, 24 January 2014

how calabash cross platform example loads platform specific commands

If you are looking to share test code between ios and android - this is the best example https://github.com/calabash/x-platform-example


Download the project from github using client like source tree or tower  or as zip (https://github.com/calabash/x-platform-example/archive/master.zip)


When you run the test using this command there are lots of background stuff happening 
calabash-android -p android app.apk


  • Cucumber profile

cucumber.yml file is scanned for profile mentioned above
based on configuration specified there (https://github.com/calabash/x-platform-example/blob/master/config/cucumber.yml)

features, step definitions, support,helpers and step definition files are loaded.
only directories that are shared between IOS and android ( like step definitions / features / support files) and android specific directories are loaded

yml file can be organised  little better by separating common (common_path)and android (android_common_path) files  
    <% common_path = "-r features/support -r features/step_definitions -r features/BasePages" %>
    <% android_common_path = "-r features/android/support -r features/android/helpers  -r features/android/pages/" %>
    android: RESET_BETWEEN_SCENARIOS=1 PLATFORM=android <%= common_path %> <%= android_common_path %>  --tags @test1 -v

Below files are loaded before tests are run 

features/android/support/app_installation_hooks.rb
features/android/support/hooks.rb
features/support/env.rb

    based on profile enviroment variables are set, based on enviroment variables calabash test setup will be done
    check here for RESET_BETWEEN_SCENARIOS example

    • Calabash libraries

    take a look at this file features/step_definitions/calabash_steps.rb
    based on PLATFORM variable set in profile ios or android libraries are loaded


      if ENV["PLATFORM"] == "android"
        require 'calabash-android/calabash_steps'
      elsif ENV["PLATFORM"] == "ios"
        require 'calabash-cucumber/calabash_steps'
      end


    All page classes must include calabash like shown below in order to include calabash libraries 

    MainPage < Calabash::ABase

    IOS 
    MainPage < Calabash::IBase


    some more good practices to share code

    1. create base classe for ios and android and inherit the class into ios or android as required, place common methods in base class
    2. share queries between ios and android - www.mobiletestworld.com/2014/01/how-to-share-calabash-commands-between.html
    3. create common method to share between ios and android ( helper classes to read files / read excel etc and place them in  one of the shared folders (mentioned at beginning of topic)


    calabash: how to reset app for specific scenarios using cucumber

    RESET_BETWEEN_SCENARIOS

    $ RESET_BETWEEN_SCENARIOS=1 calabash-android run <apkfile>
    if the value is set to 1 application is reset between each scenario, incase you have logged in then you will logged out as application data will be cleared, used it if its only neccessary.


    • lets looks at https://github.com/calabash/x-platform-example/blob/master/features/android/support/app_life_cycle_hooks.rb
    This is run before feature files are run

    Android

    Before do |scenario|
      scenario_tags = scenario.source_tag_names
      if scenario_tags.include?('@reinstall') || !INSTALLATION_STATE[:installed]
          uninstall_apps
          install_app(ENV['TEST_APP_PATH'])
          install_app(ENV['APP_PATH'])
          INSTALLATION_STATE[:installed] = true
      end
      start_test_server_in_background
    end


    To reset or reinstall for only specific scenarios, tag them with @reinstall only those scenarios ill be reset


    if its required to only reset app then use clear_app_data in place of uninstall , install apps command
    scenario_tags = scenario.source_tag_names
        if scenario_tags.include?('@reset')
          clear_app_data
        end



    for ios use this code 


    Before do |scenario|
      initialize_all
      @calabash_launcher = Calabash::Cucumber::Launcher.new
      scenario_tags = scenario.source_tag_names
      if scenario_tags.include?('@reset')
        @calabash_launcher.reset_app_jail
      end
      unless @calabash_launcher.calabash_no_launch?
        @calabash_launcher.relaunch
        @calabash_launcher.calabash_notify(self)
      end
    end


    Calabash IOS: common problems with solutions

    Common calabash IOS problems for IOS projects with workspace files

    Error 1
    Wordpress IOS application compilation failed
    'linker command failed with exit code 1'

    this usually happens if selected libraries are not loaded

    Solution 1:
    Its common practice to launch <app>.xcodeproj file and compile app
    but for some apps workspace file has to be launched inorder for the app to compile


    TIP 1:
    'calabash-ios setup' command must be run in folder where <app>.xcodeproj is present not from
    folder where workspace file is present





    Quit xcode :

    Error 2:  calabash-ios setup
    Checking if Xcode is running...
    Detected running Xcode. You may need to restart Xcode after setup.
    Found no *.xcodeproj files in dir ..

    Solution 2:
    quit ios while executing 'calabash-ios setup'





    Error 3:

    Unable to find *.xcodeproj in /Users/<username>/projects/meine.tui.ui-automation (RuntimeError)
     /Users/<username>/.rvm/gems/ruby-2.0.0-p247/gems/calabash-cucumber-0.9.165/lib/calabash-cucumber/launch/simulator_helper.rb:61:in `derived_data_dir_for_project'
     /Users/<username>/.rvm/gems/ruby-2.0.0-p247/gems/calabash-cucumber-0.9.165/lib/calabash-cucumber/launch/simulator_helper.rb:156:in `app_bundle_or_raise'
     /Users/<username>/.rvm/gems/ruby-2.0.0-p247/gems/calabash-cucumber-0.9.165/lib/calabash-cucumber/launcher.rb:292:in `relaunch'
     /Users/<username>/projects/meine.tui.ui-automation/features/ios/support/01_launch.rb:19:in `Before'

    Solution:

    common problem Running calabash commands from folder outside project ( when features are not present in same folder as .xcodeproj file)

     Solution 3a:
     run the tests from folder where .xcodeproj file is present

     Solution 3b:
     specify the app bundle path, its location where you compile app is present. its mostly present at
      /Users/<username>/Library/Developer/Xcode/DerivedData/appname-galigtjucxijnkexousxcnokqktm/Build/Products/Debug-iphonesimulator/appname.app

      full command to run tests will be like this

      ~ $:  APP_BUNDLE_PATH=/Users/<username>/Library/Developer/Xcode/DerivedData/appname-galigtjucxijnkexousxcnokqktm/Build/Products/Debug-iphonesimulator/appname.app cucumber

    Thursday, 23 January 2014

    Calabash android installation with screen shots ( Step by step guide)

    Install Ruby 

    Mac

    1) \curl -sSL https://get.rvm.io | bash










    2) Install specific ruby version using

    rvm install 1.9.3

    for installing latest stable ruby version

    \curl -sSL https://get.rvm.io | bash -s stable --ruby
    3) check list of rubies installed 'rvm list

    check these links for more help
    https://coderwall.com/p/tptocq
    http://rvm.io/rubies/installing


    Windows 


    Android SDK :

    Download & install SDK

    follow steps from here
    https://developer.android.com/sdk/installing/index.html

    Install android libraries

    Launch installed sdk and choose one of the latest android api to install (if you know minSdkVersion for you app install that also)

    minSdkVersion is present in androidmanifest.xml file as this this minimal SDK level required to run this particular application(http://developer.android.com/guide/topics/manifest/uses-sdk-element.html)

    pic: Find out Android Api version required




    pic: Installing required version from SDK manager



    Enviroment Variables

    MAC 

    In terminal type 'open ~/.bash_profile' (create this file if its not present already) or 'open ~/.profile' (if it exists) 
    • export ANDROID_HOME=/Users/tejasvi/Documents/proj/android/adt/sdk
    • export PATH=$PATH:$ANDROID_HOME/platform-tools
    • export PATH=$PATH:$ANDROID_HOME/tools
    • JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home

    WINDOWS

    If you have no idea to set environment variables check here
     
    Set 
    JAVA_HOME

    Android tools / Android platform-tools folder into path variable





    And set ANDROID_HOME=<path to android sdk>

     

    BOTH WINDOWS & MAC

    First connect your device
    Relaunch terminal  and run below command

    ~ $: adb devices
    * daemon not running. starting it now on port 5037 *
    * daemon started successfully *
    List of devices attached 
    479005cda7cxxx device

    windows: if devices are not listed its mostly problem with drivers ( install HTC sync for HTC devices / samsung kies for samsung devices)

    Calabash Installation

    Open terminal and 
    type these commands  on mac

    
    

    windows - do not use sudo 


    Wait until they are installed

    Then do 'gem list' , this will display all gems installed 

    To find current gem enviroment use 'gem env' command

    How to share calabash commands between IOS and Android

    This example suits well for calabash x platform example (where code is shared between IOS and android)

    set OS=ios in command line from where your cucumber tests are run (ex: OS=android calabash-android run <apkfile> )
    value of @g_query_txt  is set based on env variable

    @g_query_txt="view " if ENV['OS']=="ios"
    @g_query_txt="* " if ENV['OS']=="android"


    Add the below method wait_for_similar_text_to_appear in a class that is accessible to both IOS and android code
    or
      Create a common module (between ios & android) and then include that into ios or android classes

     
    ## Specify text to check and time to wait for
    # This will return true even if text matches part of the sentence
    @g_query_txt="view " if ENV['OS']=="ios"
    @g_query_txt="* " if ENV['OS']=="android"

    def wait_for_similar_text_to_appear(text, time_out=5)
      query_text=@g_query_txt+"{text LIKE '*#{text}'}"
      begin
        wait_poll({:until_exists => query_text, :timeout => time_out.to_i}) do
          puts text
        end
      rescue
        fail("Failed to find text"+text)
      end
    end

    Arguments:
      text: This is part of the text which we want to match
      time_out: Optional argument, default timeout value is 5 seconds

    Tuesday, 21 January 2014

    Calabash-Android Mobile Automation: Part 1 : Framework setup Links & video


    Check here for calabash android installation steps 





    Video



    Video link - https://www.youtube.com/watch?v=p9ZwdQnaXrs

    List of commands are present here -(http://pastebin.com/ch7H3y8q) and they are also copied below for quick reference

    Precondition :

    Download application to test - WebView Demo from below link

    Download android SDK -  

    Steps  : (Below steps are for reference, explanation  is present in video)
    1.  Calabash Github link - https://github.com/calabash/calabash-android
    2. Install Android SDK.  set path variables (ANDROID_HOME)
    3. Launch eclipse + create AVD (Android virtual device)
    4. Copy Applicaiton APK to folder where your tests are present
    5. Run calabash-android gen command from command line - To create skeleton for features
    Command used to run android tests :
    calabash-android run /Users/tejasvi/Documents/proj/android/calabash-sample-proj/WebViewDemo/bin/WebViewDemo.apk