{"id":6454,"date":"2014-02-07T17:33:31","date_gmt":"2014-02-07T14:33:31","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=6454"},"modified":"2021-08-16T13:51:30","modified_gmt":"2021-08-16T10:51:30","slug":"ios-acceptance-testing-with-calabash-and-rspec","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/ios-acceptance-testing-with-calabash-and-rspec\/","title":{"rendered":"iOS acceptance testing with Calabash and Rspec"},"content":{"rendered":"\n<p>While investigating existing automated tools for mobile TDD, we have run into a well-documented and supported library by Xamarin. It&#8217;s called calabash, comes with iOS and Android support and has a wide scope of supported user actions, gestures and expectations.<br>The only drawback we found significant is lack of native Rspec support (cucumber only for now).<br>This post provides a simple tutorial for using calabash with Rspec to test your iOS applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p>Calabash library including \u0441ucumber step definitions is aggregated within single gem <a title=\"calabash-cucumber\" href=\"https:\/\/rubygems.org\/gems\/calabash-cucumber\" target=\"_blank\" rel=\"noopener\">calabash-cucumber<\/a>.<br>Basic tutorial for integrating your project with Calabash testing tool is well described <a href=\"https:\/\/github.com\/calabash\/calabash-ios\" target=\"_blank\" rel=\"noopener\">here<\/a>.<br>We use mixed approach for initial setup by adding <a href=\"http:\/\/cocoapods.org\/?q=on%3Aios%20calabash\" target=\"_blank\" rel=\"noreferrer noopener nofollow\" title=\"Calabash pod\">Calabash pod<\/a> and manually copying main project target in Xcode.<br>As cucumber is a primary framework coming out of the box, some additional steps are required to use it with Rspec.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up Rspec environment<\/h3>\n\n\n\n<p>First of all install rspec gem and initialize your project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gem install rspec \/\/or use Gemfile\nrspec --init\n<\/pre>\n\n\n\n<p>This will generate the following file structure:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>.\/spec folder for all your test examples.<\/li><li>.\/spec\/spec_helper.rb file for all configuration and initial setup stuff.<\/li><\/ul>\n\n\n\n<p>Using predefined cucumber setup and a bit of source investigation, we came up with the following definitions within spec_helper.rb:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">require 'sim_launcher'\nrequire 'calabash-cucumber\/operations'\n\ninclude Calabash::Cucumber::Core\ninclude Calabash::Cucumber::TestsHelpers\ninclude Calabash::Cucumber::WaitHelpers\ninclude Calabash::Cucumber::KeyboardHelpers\n\nRSpec.configure do |config|\n  config.treat_symbols_as_metadata_keys_with_true_values = true\n  config.run_all_when_everything_filtered = true\n  config.filter_run :focus\n  config.order = 'random'\n\n  config.before do\n    @calabash_launcher = Calabash::Cucumber::Launcher.new\n    unless @calabash_launcher.calabash_no_launch?\n      @calabash_launcher.relaunch\n      @calabash_launcher.calabash_notify(self)\n    end\n  end\n\n  config.before(:each) do\n    element_exists('view')\n  end\n\n  config.after do\n    unless @calabash_launcher.calabash_no_stop?\n      calabash_exit\n      @calabash_launcher.stop if @calabash_launcher.active?\n    end\n  end\nend\n<\/pre>\n\n\n\n<p>A before hook was set up to wait for any view to appear &#8211; such trick is required to avoid stupid failures because of initial simulator startup delay.<br>There are a few additional method definitions required to make this setup work:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def escape_quotes(str)\n  str.gsub(\"'\", \"\\\\\\\\'\")\nend\n  \n#### emulate cucumber reports\ndef embed(path, type, label)\n  p \"storing report: #{path} (type: #{type}, label:#{label})\"\nend\n<\/pre>\n\n\n\n<p>You can add them to your spec_helper.rb.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sugarize it<\/h3>\n\n\n\n<p>Basic ruby modules included in calabash-cucumber gem have predefined steps for<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>querying elements<\/li><li>performing actions\/gestures<\/li><li>matching expectations<\/li><\/ul>\n\n\n\n<p>We have added simple wrapper methods to make test examples readable and DRY.<br>Here are some examples that are used in our test scenarios:<\/p>\n\n\n\n<p><em><b>Step definitions<\/b><\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">module StepHelper\n  WAIT_TIMEOUT = (ENV['WAIT_TIMEOUT'] || 30).to_f\n  STEP_PAUSE = (ENV['STEP_PAUSE'] || 0.5).to_f\n\n  def wait_to_see_view(expected_mark)\n    wait_for_elements_exist([marked_view_query_string(expected_mark)], :timeout =&gt; WAIT_TIMEOUT)\n  end\n\n  #Touches\n  def touch_the_button(name)\n    touch(\"button marked:'#{name}'\")\n    sleep(STEP_PAUSE)\n  end\n\n  def touch_view(expected_mark)\n    touch(marked_view_query_string(expected_mark))\n    sleep(STEP_PAUSE)\n  end\n\n  def go_back\n    touch(\"navigationItemButtonView first\")\n    sleep(STEP_PAUSE)\n  end\n\n  #Waiting\n  def wait_for_seconds(seconds)\n    sleep seconds.to_f\n  end\n\n  #Inputs\n  def enter_text_into_textfield_by_placeholder(text_to_type, placeholder)\n    touch(\"textField placeholder:'#{placeholder}'\")\n    keyboard_enter_text(text_to_type)\n    sleep(STEP_PAUSE)\n  end\n\n  def enter_text_into_textfield_by_label(text_to_type, label)\n    touch(\"textField marked:'#{label}'\")\n    keyboard_enter_text(text_to_type)\n    sleep(STEP_PAUSE)\n  end\n\n  def marked_view_query_string(expected_mark)\n    \"view marked:'#{expected_mark}'\"\n  end\n\n  ...\n\nend\n<\/pre>\n\n\n\n<p><em><b>Custom matchers<\/b><\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RSpec::Matchers.define :show_view do |expected_mark|\n  match do\n    (element_exists( \"view marked:'#{expected_mark}'\" ) or\n     element_exists( \"view text:'#{expected_mark}'\"))\n  end\n\n  failure_message_for_should do\n    \"No visible view marked '#{expected_mark}' found\"\n  end\n\n  failure_message_for_should_not do\n    \"Found visible view marked '#{expected_mark}'\"\n  end\nend\n\nRSpec::Matchers.define :show_error_alert do |expected_error_message|\n  match do\n    error_message = query(\"view className:'UIAlertView' label className:'UILabel'\", :text).first\n    error_message == expected_error_message\n  end\n\n  failure_message_for_should do\n    \"No alert with text '#{expected_error_message}' found\"\n  end\n\n  failure_message_for_should_not do\n    \"Found visible alert with text '#{expected_error_message}'\"\n  end\nend\n\n...\n\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusions<\/h3>\n\n\n\n<p>Adding Rspec support for your acceptance testing using calabash library is really simple.<br>Approach described above helped us to build and support application test coverage for wide variety of flows and interactions.<br>Here is a short example on login flow test scenario:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">...\n\nshared_examples_for \"login error\" do\n  it do\n    should show_error_alert(alert_error_message)\n    should_not show_view('Asset list zero case')\n  end\nend\n\n...\n\ndescribe \"login error\" do\n  before do\n    navigate_to_login_screen\n    stub_api_request_with_error(\"POST\", \"\/users\/sign_in\", error_message);\n    submit_login_form\n  end\n\n  context \"wrong credentials\" do\n    let(:error_message) {'login_failed'}\n    let(:alert_error_message) {wrong_email_or_password_error_message}\n\n    it_should_behave_like \"login error\"\n  end\n\n  context \"server exception\/unavailable\" do\n    let(:error_message) {'some_error_message'}\n    let(:alert_error_message) {server_unavailable_error_message}\n\n    it_should_behave_like \"login error\"\n  end\nend\n\n...\n<\/pre>\n\n\n\n<p>Using Rspec instead of cucumber gives you a better opportunity to setup context of test examples.<br>It&#8217;s also very important to use any CI server you prefer as it takes some time for full test coverage to run.<br>Do not hesitate to add your own step definitions and matchers to keep your test examples readable, DRY and clean.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While investigating existing automated tools for mobile TDD, we have run into a well-documented and supported library by Xamarin. It&#8217;s called calabash, comes with iOS and Android support and has a wide scope of supported user actions, gestures and expectations.The only drawback we found significant is lack of native Rspec support (cucumber only for now).This&#8230;<\/p>\n","protected":false},"author":50,"featured_media":7447,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Roman Bilous"],"class_list":["post-6454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=6454"}],"version-history":[{"count":55,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6454\/revisions"}],"predecessor-version":[{"id":14115,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6454\/revisions\/14115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/7447"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=6454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=6454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=6454"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=6454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}