0

Hy! I am trying to write predefined steps in calabash ios for finding a button with a certain title. My question is how do I find a certain UIButton using its title label in calabash? I've tried the following:

Then (/^I see button with title "([^\"]*)" disabled$/) do |buttonTitle|
 buttons = query("UIButton").compact
  buttons.each do |button|
      if query(button, :titleLabel, :text) == buttonTitle
        fail(msg="Button found")
        return
      end
  end
end
Dalma Racz
  • 149
  • 1
  • 7

2 Answers2

1

Why not something like

Then (/^I should not see button with title "([^\"]*)"$/) do |button_title|
   button = query("view marked:'#{button_title}'")
   unless button.empty?
      screenshot_and_raise "Error: #{button_title} is visible."
   end
end
irradio
  • 53
  • 7
1

irradio's answer is correct, but I want to add some comments to help you understand what you are doing wrong.

# Returns an Array of query results for buttons, #compact
# is not necessary because all the values will be non-nil.
buttons = query("UIButton")

# This is incorrect; you should not pass a query result back to query.
buttons.each do |button|
  query(button, ...)
end

# If you wanted to extract titles of all the buttons
query("button descendant label", :text)
=> an Array of titles

# [button titleForState:UIControlStateNormal]
query("button", {:titleForState => 0})
=> an Array of titles for UIControlStateNormal

# [[button titleLabel] text]
query("button", :titleLabel, :text)
=> an Array of titles
jmoody
  • 2,480
  • 1
  • 16
  • 22