calabash-android源码解读

本文介绍了calabash-android的源码分析,包括Instrumentation源代码在Eclipse中的查看方式和利用这些源码进行扩展的方法。同时,详细探讨了ruby-gem中的step-definition以及如何通过`calabash-android gen`命令创建测试文件夹,并建议对DSL进行封装以提高feature的可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码地址:https://github.com/calabash/calabash-android


1. Instrumentation源代码地址,可以以project的形式导入到Eclpise中查看,也为后续对step-definition

calabash-android/ruby-gem/test-server/instrumentation-backend/

在src中可以查看到相应的instrumentation方法,利用其中方法可以对calabash-android进行扩展. 

2. ruby-gem中calabash-android/ruby-gem/lib/calabash-android/ 下可以看到对cucumber中的step的定义

(1)我们看到此文件下面的calabash_steps.rb中有如下定义

require 'calabash-android/steps/assert_steps'
  require 'calabash-android/steps/check_box_steps'
  require 'calabash-android/steps/context_menu_steps'
  require 'calabash-android/steps/date_picker_steps'
  require 'calabash-android/steps/enter_text_steps'
  require 'calabash-android/steps/location_steps'
  require 'calabash-android/steps/navigation_steps'
  require 'calabash-android/steps/press_button_steps'
  require 'calabash-android/steps/progress_steps'
  require 'calabash-android/steps/screenshot_steps'
  require 'calabash-android/steps/search_steps'
  require 'calabash-android/steps/spinner_steps'
  require 'calabash-android/steps/time_picker_steps'
(2) calabash-android / ruby-gem / lib / calabash-android / steps / 中有很多关于steps的具体定义,所以比较简单的step我们只需要引用require 'calabash-android/calabash_steps'即可。

3. 关于calabash的扩展

我们新建一个测试文件夹,cd到此文件先,然后运行“calabash-android gen”会有如此目录结构

➜  calabash  tree

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

其中calabah_steps.rb中默认引用了 require 'calabash-android/calabash_steps'此代码,如果需要扩展可以在此中定义具体的steps并且调用instrumentation-backend中定义好了的方法。



feature 简单定义:

Feature: Login feature  Scenario: As a valid user I can log into my app    When I press "Add Contact"
    Then I see "Target Account"
    Then I enter "hello" into input field number 1    Then I enter "13817861875" into input field number 2    Then I enter "hengwen@hotmail.com" into input field number 3    When I press "Save"
    Then I wait for 1 second    Then I toggle checkbox number 1    Then I see "hello"

这里 input field number 就针对了 ContactAdder Activity 中输入框。我现在这样写其实不太友好,比较好的方式是进行再次封装,对 DSL 撰写者透明。比如:

When I enter "hello" as "Contact Name"

step_definition

When (/^I enter "([^\"]*)" as "([^\"]*)"$/) do | text, target |
    index = case target
    when "Contact Name": 1
    ...
    end
    steps %{
        Then I enter #{text} into input field number #{index}
    }end

这样 feature 可读性会强一点。后面的语句依旧调用calabash-android/calabash_steps中的steps

Then I enter #{text} into input field number #{index}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值