2016-03-14 58 views
0

Hy!我正试图在葫芦ios中编写预定义的步骤来查找带有特定标题的按钮。我的问题是如何在葫芦中使用标题标签找到某个UIButton?我已经试过如下:如何通过标题标签在葫芦ios中查找UIButton?

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 

回答

1

为何不像

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 
1

irradio的答案是正确的,但我想补充一些意见,以帮助你了解你在做什么错。

# 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