Calabash iOS: how to get value using query command

2019-04-02 11:39发布

I'm trying to get the value of every class using query command. Below is the sample UI component that I get:

[0] {
          "class" => "UITabBarSwappableImageView",
             "id" => "imageView-34",
           "rect" => {
        "center_x" => 288,
               "y" => 522,
           "width" => 48,
               "x" => 264,
        "center_y" => 538,
          "height" => 32
    },
          "frame" => {
             "y" => 2,
         "width" => 48,
             "x" => 6,
        "height" => 32
    },
          "label" => nil,
    "description" => "<UITabBarSwappableImageVie....>"

On Android, I can just use this to list all the values of class components:

query("*", :class)

However, I can't seem to use the same command on iOS. I get this as the result:

irb(main):135:0> query "*", :class
[
    [ 0] nil,
    [ 1] nil,
    [ 2] nil,
    [ 3] nil
]

I know that with the labels, I can use :accessibilityLabel to do the job, but not when I try to get value from class/id/etc.

Could someone please shed some light?

1条回答
我想做一个坏孩纸
2楼-- · 2019-04-02 12:22

Short answer: 'class' is not a selector on UIView instances

Long answer:

# query( < look for some views >, < selector on the views found > )

# look for buttons marked 'big button' and call 'isSelected' selector on them
query("button marked:'big button'", :isSelected")

# look for labels marked 'title' and call 'text' selector on them
query("label marked:'title'", :text)

# look for all views and call 'class' selector on them
# whoops!  'class' is not a selector on UIView instances
query "*", :class

Taking a step back, I think I know what you are trying to do - get a comprehensive list of views that are visible.

Have a look at https://github.com/jmoody/briar/blob/master/lib/briar/irbrc.rb

Example output is here: https://gist.github.com/jmoody/8031917

Instead of calling 'class' in the query, iterate over the results returned by query and look for the value of the 'class' key.

query('*').map { |result| result['class'] }
查看更多
登录 后发表回答