首页 > 编程知识 正文

android经典入门教程,javaandroid开发教程

时间:2023-05-03 16:23:53 阅读:162044 作者:2681

什么是Calabash?

Calabash是一个自动化测试框架,可以测试安卓和iOS的本机APP和混合APP。

那个如下。

calabash-android

calabash-ios

Calabash-android简介

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

Calabash-android体系结构图

Features ——这里的feature是cucumber的feature,用于描述用户stories。

step definitions —— calabash Android预先定义了几个通用的step。 可以根据自己的需求定义更复杂的步骤。

在Your app ——测试之前,不需要修改APP应用程序。 这里其实有问题。 稍后再说。 )

Instrumentation Test Server ——这是一个APP应用程序,在运行测试时安装在设备上。 此APP应用程序基于Android SDK的activityinstrumentationtestcase 2。 这是Calabash Android框架的一部分。 Robotium已集成到此APP应用程序中。

构建计算安卓环境

ruby环境

安卓开发环境

JAVA

安卓SDK

Ant

指定JAVA环境变量、Android SDK环境变量(ANDROID_HOME ),并将Ant添加到路径中。

安装Calabash-android

geminstallcalabash-android

sudogeminstallcalabash-Android #如果权限不足,请使用此选项。

为calabash-android创建骨架

calabash-androidgen

将生成以下目录结构:

计算树

同调

功能

|_support

||_app_installation_hooks.rb

||_app_life_cycle_hooks.rb

||_env.rb

|_step_definitions

||_calabash_steps.rb

|_my_first.feature

写测试用例

与典型的cucumber测试一样,只需将测试用例添加到feature文件中即可。 例如,测试contact manager.apk (在androidsdksample中,Appium也使用此apk )。

我们想实现,

打开此APP

单击Add Contact按钮

向hello添加Contact Name

将Contact Phone添加到13817861875

将Contact Email添加为hengwen@hotmail.com

保存

所以我们的feature应该这样:

fa ture : loginfeaturescenario : asavalidusericanlogintomyappwhenipress ' add contact '

ThenIsee'TargetAccount '

thenienter ' hello ' intoinputfieldnumber1thenienter ' 13817861875 ' intoinputfieldnumber2thenienter ' heng Wen @ hotmail.com

theniwaitfor1secondthenitogglecheckboxnumber1then isee ' hello '

其中,input field number以ContactAdder Activity的输入框为对象。 我现在这样写其实不太友好,比较好的方式是重新封装,对DSL的笔者是透明的。 例如:

WhenIenter'hello'a

s "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 可读性会强一点。

运行 feature

在运行之前,我们对 apk 还是得处理下,否则会遇到一些问题。

App did not start (RuntimeError)

因为calabash-android的client和test server需要通信,所以要在 AndroidManifest.xml 中添加权限:

ContacterManager 代码本身的问题

由于 ContacerManager 运行时候,需要你一定要有一个账户,如果没有账户 Save 的时候会出错。为了便于运行,我们要修改下。

源代码地址在 $ANDROID_HOME/samples/android-19/legacy/ContactManager,大家自己去找。

需要修改 com.example.android.contactmanager.ContactAdder 类里面的 createContactEntry 方法,我们需要对 mSelectedAccount 进行判断, 修改地方如下:

// Prepare contact creation request

//

// Note: We use RawContacts because this data must be associated with a particular account.

//       The system will aggregate this with any other data for this contact and create a

//       coresponding entry in the ContactsContract.Contacts provider for us.

ArrayList ops = new ArrayList();

if(mSelectedAccount != null ) {

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)

.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType())

.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName())

.build());

} else {

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)

.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)

.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)

.build());

}....

if (mSelectedAccount != null) {

// Ask the Contact provider to create a new contact

Log.i(TAG,"Selected account: " + mSelectedAccount.getName() + " (" +

mSelectedAccount.getType() + ")");

} else {

Log.i(TAG,"No selected account");

}

代码修改好之后,导出 apk 文件。

运行很简单:

calabash-android run 

如果遇到签名问题,请用: calabash-android resign apk。

可以看看我运行的情况:

➜  calabash  calabash-android run ContactManager.apk

Feature: Login feature

Scenario: As a valid user I can log into my app                # features/my_first.feature:33135 KB/s (556639 bytes in 0.173s)3315 KB/s (26234 bytes in 0.007s)

When I press "Add Contact"                                   # calabash-android-0.4.21/lib/calabash-android/steps/press_button_steps.rb:17

Then I see "Target Account"                                  # calabash-android-0.4.21/lib/calabash-android/steps/assert_steps.rb:5

Then I enter "hello" into input field number 1               # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5

Then I enter "13817861875" into input field number 2         # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5

Then I enter "hengwen@hotmail.com" into input field number 3 # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5

When I press "Save"                                          # calabash-android-0.4.21/lib/calabash-android/steps/press_button_steps.rb:17

Then I wait for 1 second                                     # calabash-android-0.4.21/lib/calabash-android/steps/progress_steps.rb:18

Then I toggle checkbox number 1                              # calabash-android-0.4.21/lib/calabash-android/steps/check_box_steps.rb:1

Then I see "hello"                                           # calabash-android-0.4.21/lib/calabash-android/steps/assert_steps.rb:51 scenario (1 passed)9 steps (9 passed)0m28.304s

All pass!

大家看到 gif 是 failed,是因为在模拟器上运行的。而上面全部通过的是我在海信手机上运行的。环境不一样,略有差异。

总结

本文是对 calabash-android 的一个简单介绍,做的是抛砖引玉的活。移动测试框架并非 Appium 一家,TesterHome 希望其他框架的话题也能热火起来。watch and learn!

本帖已被设为精华贴!

来源:oschina

链接:https://my.oschina.net/u/593773/blog/263091

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