首页 > 编程知识 正文

android电视自动化测试,app自动化测试框架

时间:2023-05-04 07:29:15 阅读:162039 作者:612

Calabash-android

一. Calabash简介

Calabash是一个开源移动UI自动化测试框架,支持安卓和IOS。

有calabash-android和calabash-ios

二. Calabash-android简介

Calabash-android是一种支持android的UI自动化测试框架,PC端使用cucumber框架,通过http和json与模拟器和实际安装的测试apk进行通信。

模式映射详细信息:

1、Features文件

feature文件使用cucumber框架(http://cukes.info/)提供的描述性语言创建测试用例文件或最终测试用例代码文件。

同样的形式如下。

Feature: WebView feature

方案:测试webview

1

2

3

4

5

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/^ ipressimagebuttonnumber (d ) $/do |

performaction (press _ image _ button _ number ),buttonNumber ) )。

结束

performAction是calabash-android的ruby类库的方法,最终以http json的形式提供了press_image_button_number和buttonNumber这两个爸爸

3、测试APK

calabash-android的源代码中存在android测试程序,用于接收PC端发送的消息。

关于在步骤2中发送的步骤,源代码中存在以下代码

Execute方法接收传递的参数,并调用robotium的API

Key方法必须对应于ruby代码performAction中的第一个参数

publicclasspressimagebuttonnumberimplementsaction {

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@Override

公共安全(字符串. args ) )。

instrumentationbackend.solo.click on imagebutton (integer.parseint (args [0] )- 1 );

return Result.successResult (;

}

@Override

公共字符串密钥() }

return ' press _ image _ button _ number ';

}

}

4、被测APK

测试apk通过测试apk与android附带的安装进行交互

三.构建calabash-android环境

1、安装Ruby

在此下载Ruby1.8.7安装

2、下载devkit文件

解压缩后进入解压缩文件,然后执行以下命令

ruby dk.rb init

ruby dk.rb review

ruby dk.rb install

如果不安装devkit,可能会显示以下错误消息

error : errorinstallingcumber :

the ' JSON ' nativegemrequiresinstalledbu

ild tools.

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

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

3、 安装cucumber

gem install cucumber

4、 安装calabash-android

gem install calabash-android

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

1

2

3

ERROR: Error installing cucumber:

ERROR: Failed to build gem native extension.

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

四、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.rb是cucumber可以使用的一些步骤语句,默认的语句见这里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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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控件name为searchtype的click一下,这里就是点击搜索按钮

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的客户端软件,然后执行下面命令获取源代码

2、 修改instrumentation-backend源代码

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

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

public class ClickLongOnText implements Action {

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@Override

public Result execute(String... args) {

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

return Result.successResult();

}

@Override

public String key() {

return "click_long_on_text";

}

}

3、 编译修改后的源代码

编译之前需要把新增的代码添加到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生成的目录文件的featuresstep_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

1

2

3

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

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

六、总结

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

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

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

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。