Calabash Android 简介

来源:互联网 发布:intent 传递数据 编辑:程序博客网 时间:2024/05/17 22:32

Calabash-android

一、Calabash介绍

Calabash是一个开源的移动端UI自动化测试框架,支持androidIOS

存在calabash-androidcalabash-ios

https://github.com/calabash

二、Calabash-android介绍

Calabash-android是支持androidUI自动化测试框架,PC端使用了cucumber框架,通过httpjson与模拟器和真机上安装的测试apk通信,测试apk调用robotium的方法来进行UI自动化测试,支持webview操作。

 

 

 

 

Calabash-android调研 - kevinkong - 风之云的足迹
 

 

 

 

 

 

 

 

 

 

 

 

 

 


架构图详解:

1、 Features文件

feature文件是cucumber框架(http://cukes.info/)自带的通过描述性的语言来编写测试用例文件,也就是最终的测试用例代码文件。

 类似的格式如下:

   Feature: WebView feature

   Scenario: Test WebView

       Then I wait for 5 seconds

       Then I take a picture

       Then I press image button number 1

 

2、 测试步骤定义,Ruby类库

步骤定义也是cucumber框架中的一部分,比如

Then I press image button number 1 这句话表示测试的步骤是点击当前页面的第一个image button 按钮

每一个feature文件中编写的步骤,都需要通过ruby编写方法进行定义

下面的ruby方法就是对于上面步骤操作的定义,calabash-android自带了一些定义,如果需要自己扩展功能的话,也需要相应的扩展定义文件。

 

Then /^I press image button number (\d+)$/ do |buttonNumber|

  performAction('press_image_button_number', buttonNumber)

end

performActioncalabash-androidruby类库中的方法,最终会通过http+json的形式,把press_image_button_numberbuttonNumber这两个参数,传入到calabash http server

 

3、 测试APK

calabash-android的源代码中会存在一个android测试程序,接收PC端发送过来的消息,

对于第二步中发送的步骤,在源代码中存在以下的代码

Execute方法接收传入的参数,并且调用robotiumAPI

Key方法是需要和ruby代码中的performAction第一个参数对应

 

public class PressImageButtonNumber implements Action {

 

    @Override

    public Result execute(String... args) {

       InstrumentationBackend.solo.clickOnImageButton(Integer.parseInt(args[0]) - 1);

        return Result.successResult();

    }

 

    @Override

    public String key() {

        return "press_image_button_number";

    }

 

}

 

4、 被测试APK

测试apk通过android自带的instrumentation和测试APK进行交互

 

 

 

 

三、calabash-android环境搭建

1、 安装Ruby

在这里下载Ruby1.8.7安装

http://rubyinstaller.org/downloads/

2、 下载devkit文件

解压出来后进入解压文件,执行下面命令

ruby dk.rb init

ruby dk.rb review

ruby dk.rb install

https://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe

 

不安装devkit的话可能会出现下面的错误信息

ERROR:  Error installing cucumber:

The 'json' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit

from 'http://rubyinstaller.org/downloads' and follow the instructions

at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

 

3、 安装cucumber

gem install cucumber

4、 安装calabash-android

gem install calabash-android

如果再次安装还是出现类似下面的错误

       ERROR:  Error installing cucumber:

       ERROR: Failed to build gem native extension.

是因为之前安装的一些软件修改了cmd.exe的配置,在注册表该了一些内容,导致出现问题,解决方案参考

http://www.cnblogs.com/puresoul/archive/2011/12/01/2270890.html

四、calabash-android对于webview的支持

1、编写android被测应用

编写一个简单的webview控件的android应用程序webview.apk,里面有一个webview控件,加载的是http://m.youdao.com 有道搜索首页的内容
2
、被测应用apk使用系统自带的key文件重签名
3
calabash-android gen 命令生成目录结构

里面会自动生成运行测试程序需要的文件
生成的文件结构如下

features

|_support

| |_app_installation_hooks.rb

| |_app_life_cycle_hooks.rb

| |_env.rb

| |_hooks.rb

|_step_definitions

| |_calabash_steps.rb

|_my_first.feature

calabash_steps.rbcucumber可以使用的一些步骤语句,默认的语句见这里https://github.com/calabash/calabash-android/blob/master/ruby-gem/lib/calabash-android/canned_steps.md ,但是里面没有增加webview的,需要自己扩展,直接在calabash_steps.rb文件中加入下面的代码,添加对webview的支持,这里只是加入了几个,其它的有用到的后续再添加。
关于calabash_steps.rb文件中Then等语句的意思,可以学习cucumber框架
http://cukes.info/

 

 

生成本来就存在的,用于加载默认的步骤

require 'calabash-android/calabash_steps'

 

#用于在webview控件找到一个field,同时往里面输入内容

Then /^I enter "([^\"]*)" into input field with id "([^\"]*)"$/ do |text, css|

  performAction('set_text','css',css,text)

end

 

Then /^I press the button with id "([^\"]*)"$/ do | css|

  performAction('click_by_selector',css)

end

 

Then /^I touch the button with id "([^\"]*)"$/ do | css|

  performAction('touch','css',css)

end

 

Then /^show the html source code$/ do

  performAction('dump_body_html')

end

4、修改my_first.feature文件
自动生成的features目录下的.feature就是一些编写测试脚本的执行文件,我们需要修改默认的.feature文件
下面代码中测试的app中的webview是访问的
http://m.youdao.com/

 

Feature: WebView feature

 

  Scenario: Test WebView

    Then I wait for 10 seconds

    Then I take a picture

        #找到input控件名称为q的,输入值为test

    Then I enter "test" into input field with id "input[name='q']"

    Then I wait for 10 seconds

    Then I take a picture

        #找到input控件namesearchtypeclick一下,这里就是点击搜索按钮

    Then I press the button with id "input[name='searchtype']"

    Then I wait for 10 seconds

    Then I take a picture

5、运行测试

进入到calabash-android gen 命令生成的文件的跟目录下,运行calabash-android run webview.apk
则会自动执行my_first.feature文件,安装被测应用然后操作webview控件进行搜索

五、扩展calabash-android

Robotium中存在clickLongOnText方法,但是calabash-android中没有,这里我们自己扩展一下,扩展calabash-android的步骤如下

1、          git下载源代码

首先下载安装git的客户端软件,然后执行下面命令获取源代码

git clone https://github.com/calabash/calabash-android.git
git submodule init
git submodule update

 

2、          修改instrumentation-backend源代码

git下载源代码后,在ruby-gem\test-server目录下存在instrumentation-backend目录,这个是一个android工程,也就是calabash-android的测试程序,导入到eclipse中。

我这边在sh.calaba.instrumentationbackend.actions.text包中新增了一个类clickLongOnText.java,代码如下

public class ClickLongOnText implements Action {

 

    @Override

    public Result execute(String... args) {

        InstrumentationBackend.solo.clickLongOnText(args[0]);

        return Result.successResult();

    }

 

    @Override

    public String key() {

        return "click_long_on_text";

    }

 

}

 

3、          编译修改后的源代码

参考https://github.com/calabash/calabash-android/wiki/Building-calabash-android

编译之前需要把新增的代码添加到git库中才可以

Git add ***

Git commit –m “dddd”

Git push

 

然后进入到ruby-gem目录

Cd ruby-gem

Rake install

会自动把修改后的源代码安装到ruby库中,再次执行calabash-android命令会调用最新的代码。

 

4、          PC端的calabash_steps.rb文件中,添加扩展的步骤定义

Calabash_steps.rb文件,在之前calabash-android gen生成的目录文件的features\step_definitions目录下

这里是新增clickLongOnText操作的定义

Then /^I long press the text "([^\"]*)"$/ do | css|

  performAction('click_long_on_text',css)

end

 

5、编写features文件

Feature: WebView feature

 

  Scenario: Test WebView

         Then I wait for 20 seconds

         Then I take a picture

Then I long press the text "CocoaChina"

5、          运行features文件

Calabash-android run ***.apk

运行之前最好把test_servers目录下的apk文件删除,保证会使用最新的测试代码,重新编译生成新的测试apk文件。

 

6、          其它

如果在编译最新的instrumentation-backend工程出现问题的话,有一个简单的方式验证你扩展的功能。

直接进入到你的ruby安装目录,我这里是ruby1.8.7

Ruby187\lib\ruby\gems\1.8\gems下面会有相应的calabash-android-***文件夹,直接把你修改或者新增的代码放入下面的test-server\instrumentation-backend相应的目录下,再次运行calabash-android run ***.apk时也会加载最新新增的代码。

六、总结

本次在windows上针对calabash-android的调研,尝试了针对webview的支持,能够实现基本的webview操作,当前还存在的问题是

1、 features文件不支持中文,需要自己去扩展

2、 Drag操作直接通过robotium扩展过来在calabash上不能使用

3、 windows上运行的时候好多次出现了连接断开的问题等不稳定情况

所以现在在windows下直接使用calabash-android到生成环境有一定的风险


0 0
原创粉丝点击