Calabash-android command line usage
This article describes how to use the calabash-android command line. Through examples, you can familiarize yourself with the ruby APIs and common Console Commands called by calabash-android, such as query, touch, and wait .,. Familiarity with these Apis helps us write custom feature.
Step 1: Download the app
The app used in this article is the hacker headlines. Click here to download it. Shows the view elements in the app:
Step 2: Open the command line and enter the app download directory. Enter the following command:
calabash-android console HackerNews.apk
If everything runs normally, the calabash command line is displayed, and the prompt is changed:irb(main):001:0>
Enter the following command in the command line:
Irb (main): 001: 0> reinstall_appsirb (main): 002: 0> start_test_server_in_background the first command is used to install the tested app and test_server. The next day, the command starts test_server, in this case, you can enter the main interface of the program.
Each Android Application consists of a series of views, such as. Button, ImageView, and chekbox. Each View has a corresponding type, and the program interface can be seen as a set of views.
Query usage
query(uiquery, *args)
This function returns an array. Each array item contains View information, such as id, coordinates, and type. Enter
irb(main):003:0> query("ActionBarButton")
Because there are two actionbarbuttons in the ControllerBar, the output result is as follows:
[[0] {"id" => nil, "enabled" => true, "contentDescription" => nil, "class" => "com. airlocksoftware. holo. actionbar. actionBarButton "," 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. actionBarButton "," rect "=> {" center_y "=> 98," center_x "=> 576," height "=> 96," y "=> 50, "width" => 96, "x" => 528}, "tag" => nil, "description" => "ActionBarButton: \" Refresh content \ ""}]
Since the returned result is a ruby array, we can use the array method, such as getting the size
irb(main):004:0> query("ActionBarButton").size
The returned result is 2.
Query 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")
The parameters in the query can be wildcards, such
irb(main):006:0> query("*")
Returns all elements on the screen.
Filter elements by Class Name
Simple Class Name
The method for querying elements is to use a simple class name and is case-insensitive. For example
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}" }]
And
irb(main):010:0> query("ImageVIew")
The results are the same.
Class Name with package Scope
If you enter the following query command,
irb(main):011:0> query("android.widget.ImageView")
The result is as follows:
[ [ 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{41f41cc8 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{4241f038 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{424aa628 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{41f6a6b0 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{42075eb8 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{4240c4d0 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{42368ad0 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{41c8a720 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{423685d8 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{424c0658 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{423b43f8 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{420c0dd8 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{423ab810 V.ED.... ........ 50,89-76,122 #7f060120 app:id/icv_comment}" }]
The returned result contains 14 elements.
We can see that:
1. When only a simple class name is provided, only the objects corresponding to the class name are queried.
2. When querying a class name containing the package name, the class object and the class subclass object are given.
Filter operation
You can filter the results by id, text, or class.
Specified id:
irb(main):018:0> query("android.widget.ImageView id:'icv_comment'")
Single quotation marks behind the primary id
Specify text:
query("* text:'Login'")
This operation returns the View containing the "Login" text on the screen.
Other attributes
query("* width:200")query("* enabled:false")
Note that no single quotation marks are provided here.
You can specify multiple attributes at the same time, for example:
Query ("* enabled: true height: 50 ")
Query Principle
The syntax of the calabash filter attribute is as follows:
prop:value
Prop is the property name and value is a string, Boolean value or integer.
In the preceding example, the attributes we use include id, text, enabled, and width. In addition, more attributes can be used.
Each View object has a series of methods, such as the View has an isEnabled method. When calabash is going to parse the attributes passed in the query, it will call the View methods in the following order: prop (), getProp () and isProp ().
For the enabled attribute, it will attempt to call enabled (), getEnabled () or isEnabled (). Finally, it will compare the returned result with the value specified in the query.
Useful Techniques
In addition to the above query statements, there are other useful techniques
Marked
This keyword identifies a View by name to compare the attributes of id, text, or contentDescription.
qeuery("button marked:'login_button'")
Returned Properties
You can specify the * args parameter in the query to control the returned results.
Return 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"]
Attribute filtering can be used with the specified attribute, as shown in figure
irb(main):006:0> query("android.widget.ImageView enabled:true", :id)
Attributes can also be written as follows:
query("android.widget.ImageView enabled:true", "id")
Chain call:
query(android.widget.ImageView", "text", "toLowerCase")
Call getText () for the put back object first, and then call the toLowerCase function.
Set the element value: (not debugged yet)
irb(main):034:0> query("edittext index:1", :setText => "1234")
We will introduce it here for the time being and update it later.