In this post, I will show you the way, how to identify application elements. Before going below, make sure you resigned and created test server of your application.
Now open commands prompt, go to directory application and run below command:
Calabash console should be opened. Following are the some basic way to find element details using query commands.
1. To find all element locator run command:
You should see all element details are listed in console
2. To find element details using layout properties:
3. Using index number :
example:
4. Using element id:
example:
5. Using element’s id with text
example:
6. Using element’s class:
example:
7. For webview using css:
example:
Now open commands prompt, go to directory application and run below command:
calabash-android console {ApkfileName}
1. To find all element locator run command:
query("*")
2. To find element details using layout properties:
query("button")
query("ImageView")
query("TextView")
query("TabWidget")
query("FrameLayout")
example:
irb(main):020:0>
query("button")
[
[0] {
"id" =>
"help_btn",
"enabled" =>
true,
"contentDescription" =>
"help",
"class" =>
"android.widget.Button",
"text" =>
"?",
"rect" => {
"center_y" => 173.0,
"center_x" => 420.0,
"height" => 54,
"y" => 146,
"width" => 30,
"x" => 405
},
"description" =>
"android.widget.Button@412cee70"
},
[1] {
"id" =>
"login_btn",
"enabled" =>
true,
"contentDescription" =>
nil,
"class" =>
"android.widget.Button",
"text" =>
"LOGIN",
"rect" => {
"center_y" => 429.0,
"center_x" => 344.5,
"height" => 72,
"y" => 393,
"width" => 181,
"x" => 254
},
"description" =>
"android.widget.Button@412d6be8"
}
]
3. Using index number :
query("*
id:’idName’ index:1")
query("button
index:1") //with layout property
irb(main):050:0>
query("button index:1")
[
[0] {
"id" =>
"login_btn",
"enabled" => true,
"contentDescription" =>
nil,
"class" =>
"android.widget.Button",
"text" =>
"LOGIN",
"rect" => {
"center_y" => 429.0,
"center_x" => 344.5,
"height" => 72,
"y" => 393,
"width" => 181,
"x" => 254
},
"description" =>
"android.widget.Button@412d6be8"
}
4. Using element id:
query("*
id:'help_btn'")
irb(main):022:0>
query("* id:'help_btn'")
[
[0] {
"id" =>
"help_btn",
"enabled" =>
true,
"contentDescription" =>
"help",
"class" =>
"android.widget.Button",
"text" =>
"?",
"rect" => {
"center_y" => 173.0,
"center_x" => 420.0,
"height" => 54,
"y" => 146,
"width" => 30,
"x" => 405
},
"description" =>
"android.widget.Button@412cee70"
}
]
query("*
id:'login_btn' text:'LOGIN'")
query("*
id:'login_btn' marked:'LOGIN'")
irb(main):052:0>
query("* id:'login_btn' marked:'LOGIN'")
[
[0] {
"id" =>
"login_btn",
"enabled" =>
true,
"contentDescription" =>
nil,
"class" =>
"android.widget.Button",
"text" =>
"LOGIN",
"rect" => {
"center_y" => 429.0,
"center_x" => 344.5,
"height" => 72,
"y" => 393,
"width" => 181,
"x" => 254
},
"description" =>
"android.widget.Button@412d6be8"
}
]
query("android.widget.Button”)
example:
irb(main):053:0>
query("android.widget.Button")
[
[0] {
"id" =>
"help_btn",
"enabled" => true,
"contentDescription" =>
"help",
"class" =>
"android.widget.Button",
"text" =>
"?",
"rect" => {
"center_y" => 173.0,
"center_x" => 420.0,
"height" => 54,
"y" => 146,
"width" => 30,
"x" => 405
},
"description" =>
"android.widget.Button@412cee70"
},
[1] {
"id" =>
"login_btn",
"enabled" =>
true,
"contentDescription" =>
nil,
"class" =>
"android.widget.Button",
"text" =>
"LOGIN",
"rect" => {
"center_y" => 429.0,
"center_x" => 344.5,
"height" => 72,
"y" => 393,
"width" => 181,
"x" => 254
},
"description" =>
"android.widget.Button@412d6be8"
}
]
query("webView
css:'*'")
query("webView
css:'input[name=\"btn\"]'")
query("webView
css:'#idName'")
query("webView css:'.calssname")
irb(main):011:0>
query("webView css:'input[name=\"btn\"]'")
[
[0] {
"id" =>
"",
"textContent" =>
"",
"nodeName" =>
"INPUT",
"class" =>
"",
"html" => \"input
type=\"button\" name=\"btn\" value=\"GO!\" onclick=\"go()\">",
"rect" => {
"center_y" => 443.0,
"center_x" => 55.5,
"height" => 39,
"width" => 54,
"bottom" => 226,
"left" => 10,
"right" => 64,
"y" => 280.5,
"x" => 15.0,
"top" => 187
},
"nodeType" =>
"ELEMENT_NODE",
"webView" =>
"webview"
}
]
Very nice post,, lot of info here
ReplyDeleteHow can we locate dynamic changing elements using xpath in calabash
ReplyDeleteI want to get center_x value. How do I get it?
ReplyDeletequery("AppCompatTextView")[1]["rect"]["center_x"]
Delete540
irb(main):051:0> query("AppCompatTextView")[1]["rect"]["center_y"]
336
query("AppCompatTextView")[1]["rect"]["center_x"]
Delete540
irb(main):051:0> query("AppCompatTextView")[1]["rect"]["center_y"]
336
how can i check if the text box is enabled or not
ReplyDeletequery("android.widget.EditText":enabled=>'true') this query is giving error
that query is invalid.
Deleteif you want to query for only edittexts that are enabled:
query("android.widget.EditText isEnabled:1")
if you want to get the if the edittext is enabled or not, query for it and check the "enabled" key:
query("android.widget.EditText").first['enabled']
Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
ReplyDeleteWhen I started learning then I understood it has got really cool stuff.
I can vouch webdriver has proved the best feature in Selenium framework. Thanks a lot for taking a time to share a wonderful article.
Very useful content, thank you.
ReplyDeleteCan you please help on how to write parent child nodes in query syntax?
I mean the way we write in selenium
example: //*[@id='some_id']/required_node