Road to identifying elements using Calabash's query command

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-android console {ApkfileName} 
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:
query("*")
You should see all element details are listed in console
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
example:
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'")
example:
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"
    }
]
5. Using element’s id with text
query("* id:'login_btn' text:'LOGIN'")
query("* id:'login_btn' marked:'LOGIN'")
example:
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"
    }
]
6. Using element’s class:
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"
    }
]
7. For webview using css:
query("webView css:'*'")
query("webView css:'input[name=\"btn\"]'")
query("webView css:'#idName'")
query("webView css:'.calssname")
example:
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"
    }
]

8 comments:

  1. How can we locate dynamic changing elements using xpath in calabash

    ReplyDelete
  2. I want to get center_x value. How do I get it?

    ReplyDelete
    Replies
    1. query("AppCompatTextView")[1]["rect"]["center_x"]
      540
      irb(main):051:0> query("AppCompatTextView")[1]["rect"]["center_y"]
      336

      Delete
    2. query("AppCompatTextView")[1]["rect"]["center_x"]
      540
      irb(main):051:0> query("AppCompatTextView")[1]["rect"]["center_y"]
      336

      Delete
  3. how can i check if the text box is enabled or not
    query("android.widget.EditText":enabled=>'true') this query is giving error

    ReplyDelete
    Replies
    1. that query is invalid.
      if 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']

      Delete
  4. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
    When 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.

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution