calabash-android 命令行使用

本文介绍calabash-android的命令行的使用,通过例子,熟悉calabash-android所调用的ruby api和常用控制台命令, 如query, touch, wait等。,。熟悉这些API有助于我们编写自定义feature。

第一步:下载app

本文所用的测试app为黑客头条,点此下载。app中view的元素如下图所示:

第二步:打开命令行,进入app的下载目录,输入如下命令

calabash-android console HackerNews.apk

如果一切运行正常,则会进入calabash命令行,同时可以看到提示符变为:irb(main):001:0>此时可在命令行里输入如下命令

irb(main):001:0> reinstall_apps
irb(main):002:0> start_test_server_in_background
  第一条命令用来安装被测试app和test_server, 第二天命令启动test_server,此时可以进入到程序的主界面。

每个Android应用程序都是有一系列的View组成.Button, ImageView, chekbox都是屏幕上的View。每个View都有对应的类型,程序界面可以看做是View的集合。

Query的用法

query(uiquery, *args)

此函数返会一个数组,每一个数组项包含View的信息,如id,坐标,类型。在命令行中输入

irb(main):003:0> query("ActionBarButton")

由于在ControllerBar中有两个ActionBarButton,故输出结果如下所示:

[
    [0] {
                        "id" => nil,
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.actionbar.ActionBarBut
ton",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 480,
              "height" => 96,
                   "y" => 50,
               "width" => 96,
                   "x" => 432
        },
                       "tag" => nil,
               "description" => "ActionBarButton: \"推广\""
    },
    [1] {
                        "id" => nil,
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.actionbar.ActionBarBut
ton",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 576,
              "height" => 96,
                   "y" => 50,
               "width" => 96,
                   "x" => 528
        },
                       "tag" => nil,
               "description" => "ActionBarButton: \"刷新内容\""
    }
]

由于返回的结果是个ruby array,我们可以使用array的方法,如获取size

irb(main):004:0> query("ActionBarButton").size

返回的结果是2

查询keys

irb(main):013:0> query("ImageView").first.keys
[
    [0] "id",
    [1] "enabled",
    [2] "contentDescription",
    [3] "class",
    [4] "rect",
    [5] "tag",
    [6] "description"
]

index

query("*")[0]
query("* index:0")

query中的参数可以是通配符,如输入

irb(main):006:0> query("*")

返回屏幕中的所有元素

通过类名来过滤元素

简单类名  

之前查询元素的方法是通过简单类名,且大小写不敏感。如

irb(main):010:0> query("ImageView")
[
    [0] {
                        "id" => "img_up_icon",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "android.widget.ImageView",
                      "rect" => {
            "center_y" => 97,
            "center_x" => 74,
              "height" => 67,
                   "y" => 64,
               "width" => 67,
                   "x" => 41
        },
                       "tag" => nil,
               "description" => "android.widget.ImageView{41f4c740 V.ED.... ....
.... 41,14-108,81 #7f060091 app:id/img_up_icon}"
    }
]

irb(main):010:0> query("ImageVIew")

结果一样。

 含有包作用域的类名 

若输入以下查询命令,

irb(main):011:0> query("android.widget.ImageView")

结果如下:

[
    [ 0] {
                        "id" => "icv_up_indicator",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 20,
              "height" => 50,
                   "y" => 73,
               "width" => 41,
                   "x" => 0
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{41f41cc
8 V.ED.... ........ 0,23-41,73 #7f060090 app:id/icv_up_indicator}"
    },
    [ 1] {
                        "id" => "img_up_icon",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "android.widget.ImageView",
                      "rect" => {
            "center_y" => 97,
            "center_x" => 74,
              "height" => 67,
                   "y" => 64,
               "width" => 67,
                   "x" => 41
        },
                       "tag" => nil,
               "description" => "android.widget.ImageView{41f4c740 V.ED.... ....
.... 41,14-108,81 #7f060091 app:id/img_up_icon}"
    },
    [ 2] {
                        "id" => "icv",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 480,
              "height" => 96,
                   "y" => 50,
               "width" => 96,
                   "x" => 432
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{4241f03
8 V.ED.... ........ 0,0-96,96 #7f060096 app:id/icv}"
    },
    [ 3] {
                        "id" => "icv",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 576,
              "height" => 96,
                   "y" => 50,
               "width" => 96,
                   "x" => 528
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{424aa62
8 V.ED.... ........ 0,0-96,96 #7f060096 app:id/icv}"
    },
    [ 4] {
                        "id" => "icv_overflow",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 98,
            "center_x" => 672,
              "height" => 96,
                   "y" => 50,
               "width" => 96,
                   "x" => 624
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{41f6a6b
0 V.ED..C. ........ 500,0-596,96 #7f060095 app:id/icv_overflow}"
    },
    [ 5] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 317,
            "center_x" => 63,
              "height" => 33,
                   "y" => 301,
               "width" => 26,
                   "x" => 50
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{42075eb
8 V.ED.... ........ 50,55-76,88 #7f060120 app:id/icv_comment}"
    },
    [ 6] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 454,
            "center_x" => 49,
              "height" => 33,
                   "y" => 438,
               "width" => 26,
                   "x" => 36
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{4240c4d
0 V.ED.... ........ 36,89-62,122 #7f060120 app:id/icv_comment}"
    },
    [ 7] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 557,
            "center_x" => 63,
              "height" => 33,
                   "y" => 541,
               "width" => 26,
                   "x" => 50
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{42368ad
0 V.ED.... ........ 50,55-76,88 #7f060120 app:id/icv_comment}"
    },
    [ 8] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 660,
            "center_x" => 63,
              "height" => 33,
                   "y" => 644,
               "width" => 26,
                   "x" => 50
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{41c8a72
0 V.ED.... ........ 50,55-76,88 #7f060120 app:id/icv_comment}"
    },
    [ 9] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 797,
            "center_x" => 77,
              "height" => 33,
                   "y" => 781,
               "width" => 26,
                   "x" => 64
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{423685d
8 V.ED.... ........ 64,89-90,122 #7f060120 app:id/icv_comment}"
    },
    [10] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 900,
            "center_x" => 77,
              "height" => 33,
                   "y" => 884,
               "width" => 26,
                   "x" => 64
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{424c065
8 V.ED.... ........ 64,55-90,88 #7f060120 app:id/icv_comment}"
    },
    [11] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 1037,
            "center_x" => 49,
              "height" => 33,
                   "y" => 1021,
               "width" => 26,
                   "x" => 36
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{423b43f
8 V.ED.... ........ 36,89-62,122 #7f060120 app:id/icv_comment}"
    },
    [12] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 1140,
            "center_x" => 63,
              "height" => 33,
                   "y" => 1124,
               "width" => 26,
                   "x" => 50
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{420c0dd
8 V.ED.... ........ 50,55-76,88 #7f060120 app:id/icv_comment}"
    },
    [13] {
                        "id" => "icv_comment",
                   "enabled" => true,
        "contentDescription" => nil,
                     "class" => "com.airlocksoftware.holo.image.IconView",
                      "rect" => {
            "center_y" => 1277,
            "center_x" => 63,
              "height" => 33,
                   "y" => 1261,
               "width" => 26,
                   "x" => 50
        },
                       "tag" => nil,
               "description" => "com.airlocksoftware.holo.image.IconView{423ab81
0 V.ED.... ........ 50,89-76,122 #7f060120 app:id/icv_comment}"
    }
]

此时返回的结果有14个元素

由此可看出:

1.当仅给出简单类名时,仅查询该类名所对应的对象

2.当查询含有包名的类名时,会给出该类的对象和该类的子类的对象。

过滤操作

可以通过id,text或class来对结果进行过滤。

指定id:

irb(main):018:0> query("android.widget.ImageView id:'icv_comment'")

主要id后面的单引号

指定text:

query("* text:'Login'")

该操作会返回屏幕中含有"Login"文本的View

其它属性

query("* width:200")
query("* enabled:false")

注意,此处无单引号

可以同时指定多个属性,如:

query("* enabled:true height:50")

查询原理

calabash过滤属性的语法如下:

prop:value

prop是属性名,value是一个字符串,布尔值或整数。

上面的例子,我们使用的属性有id,text,enabled和width.除此之外,还有更多的可以用。

每一个View对象都有一系列方法,如View有isEnabled方法。当calabash准备去解析query中传入的属性时,它会以如下的顺序调用View的方法:prop(),getProp()和isProp().

故对于enabled属性,它会尝试调用enabled(),getEnabled()或isEnabled().最后将返回结果与query中指定的值进行比较。

有用的技巧

除了以上的查询语句,还有其它有用的技巧

marked

此关键字通过名称来标识一个View,用来比较id, text或contentDescription属性。

qeuery("button marked:'login_button'")

返回属性

可指定query中的*args参数来控制返回的结果。

返回id

irb(main):005:0> query("android.widget.ImageView", :id)
[
    [ 0] "icv_up_indicator",
    [ 1] "img_up_icon",
    [ 2] "icv",
    [ 3] "icv",
    [ 4] "icv_overflow",
    [ 5] "icv_comment",
    [ 6] "icv_comment",
    [ 7] "icv_comment",
    [ 8] "icv_comment",
    [ 9] "icv_comment",
    [10] "icv_comment",
    [11] "icv_comment",
    [12] "icv_comment",
    [13] "icv_comment"
]

可以将属性过滤与指定的属性配合使用,如

irb(main):006:0> query("android.widget.ImageView enabled:true", :id)

属性也可以写成如下形式

 query("android.widget.ImageView enabled:true", "id")

链式调用:

query(android.widget.ImageView", "text", "toLowerCase")

先对放回的对象调用getText(),然后再调用toLowerCase函数。

设置元素的值:(暂未调试)

irb(main):034:0> query("edittext index:1", :setText => "1234")

暂时介绍到这儿,后续会继续更新。

时间: 2024-08-28 10:03:58

calabash-android 命令行使用的相关文章

Android系列之Android 命令行手动编译打包详解

http://www.qdmm.com/BookReader/1222701,54263720.aspxhttp://www.qdmm.com/BookReader/1222701,54263869.aspxhttp://www.qdmm.com/BookReader/1222701,54263871.aspxhttp://www.qdmm.com/BookReader/1222701,54263876.aspxhttp://www.qdmm.com/BookReader/1222701,542

Android命令行工具

Android命令行工具 查看apk相关信息,例如versionCode,versionName等     $SDK_ROOT/build-tools/21.1.2/aapt.exe aapt dump badging xxxx.apk

android 命令行安装apk

有两种方式可以在android模拟器或真机上使用命令行安装apk 一种是使用adb install命令,网上通常是这种方式 另一种是通过android提供的命令,pm install. 需要先进入android命令行:adb shell 然后执行对应pm命令. pm命令的说明,转载另一篇博客:Android下pm 命令详解 具体内容摘录如下,以便以后查看: 0. Usage: usage: pm [list|path|install|uninstall] pm list packages [-f

Android命令行下蓝牙使用

注意:此部分只适用于broadcom 系列蓝牙芯片,例如RK903, AP6xxx 系列 通过su 命令切换到root 用户 1.先确认RFKILL 驱动已经加载 ls /sys/class/rfkill/rfkill0/ 如果没有找到rfkill0 这个目录,说明蓝牙驱动有问题. 请检查kernel 中的蓝牙选项是否有勾选了 请查看kernel 的打印信息中以"[BT_RFKILL]"打头的信息. 2.关闭蓝牙: A. 在Settings 界面中关闭蓝牙 B. 给蓝牙设备下电: ec

android 命令行编程

如果你想了解android上开发一个应用程序所需要的所有环节,在命令行上将这个过程操作一遍是最好的方法,下面你可以扔掉哪个慢腾腾的eclipse,然后建立如下一个auto.cmd文件,运行一下auto.cmd,你的电脑d:\work\njh1文件夹下就会产生一个SNjh.apk文件,该文件会自动安装到你手机上,并自动运行,这需要13个步骤,当然所用的环境为JDK1.7,ADT22.3,SDK API-17,d:盘建立文件夹work,当这些环境配好后就可以正常工作了,并且在系统环境中的path变量

Android命令行播放MP3音乐

/*************************************************************************** * Android命令行播放MP3音乐 * 说明: * 有时候我们会遇到Touch不能使用,也没有鼠标,但是我们要在Android上测试 * 声卡情况,所以我们会想到在adb或者debug终端上使用命令行来播放声音. * * 2016-5-26 深圳 南山平山村 曾剑锋 *************************************

android命令行创建并打包项目

命令行创建Android项目 查看sdk信息 将目录更改到 Android SDK 的tools/的路径. 执行: android list targets 这将打印您已经为您的 SDK 下载可用的 Android平台的列表.查找您要对其编译使您应用程序的平台的目标 id 的便笺.我们建议您选择可能的最高版本.您仍可以生成您的应用程序支持较旧的版本,但将生成目标设置为最新版本允许您优化您的应用程序的最新设备. 创建项目 命令 android create project --target <ta

android命令行网络时间同步

一.简介 Android基于Linux平台的开源手机操作系统. 二.原理 既然是Linux,那就应该支持linux的各种命令行,高度的可配置,但实验发现Android是Google的一个高度阉割版的linux,很多命令都不支持,如rdate命令(网络同步时间命令).既然不支持,那我使他支持不就完了. 三.BusyBox BusyBox 是标准 Linux 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 grep.f

Android 命令行编译、打包生成apk文件

一.搭建搭建环境 1. 安装JDK 和 Android SDK 2. 配置环境变量 D:\android-sdk-windows\tools C:\Program Files\Java\jdk1.6.0_20\bin3. 例子信息 项目目录:D:\ProjectDemo SDK目录 :D:\android-sdk-windows\platforms\android-8\ 二.命令行编译Android项目 1. 生成R文件 2. Java代码生成class文件 3. class文件生成dex文件

Android 命令行开发入门教程之1

其实,掌握记事本+命令行的编程方式是非常有效的,无论是电脑硬件配置和性能问题的制约,还是对于了解程序内部运行机制.调试等也有很好的帮助.但是很多人因为惧怕或者讨厌使用这种费力的编写习惯也是能够理解的,但这都是表面的现象,后序的工作一样是简单的.可重用的. 其实,掌握记事本+命令行的编程方式是非常有效的,无论是电脑硬件配置和性能问题的制约,还是对于了解程序内部运行机制.调试等也有很好的帮助.但是很多人因为惧怕或者讨厌使用这种费力的编写习惯也是能够理解的,但这都是表面的现象,后序的工作一样是简单的.