我如何从葫芦中的警报(ios)中读取文本

时间:2013-05-03 04:52:02

标签: ios xcode cucumber calabash

如何在 calabash / cucumber 测试中访问iOS上的alertview文本?

NSString *msgString = [NSString stringWithFormat:@"No: %@\n Latitude: %f\n Longitude: %f", wrapper.no, wrapper.latitude, wrapper.longitude];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"msgString" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
// optional - add more buttons:
[alert addButtonWithTitle:@"Yes"];
[alert show];

我想声明警报具有预期的内容:

Feature: Running a test
  As a user using a phone connected to the internet
  I want to have correct sample data retrieved from cache or net
  So I can read the values of the tableview

   Scenario: Testing retrieved data

  Given the app is running
  Then I press "Refresh"
  Then I should see "Some value"
  Then I press "Some value"
  Then I should /*See an alert with "myMessage"*/
  Then I press "OK"

  And take picture

因此,如果我将字符串更改为“No:”并丢弃字符串中的所有其他内容,它实际上似乎有效,但我无法使用更复杂的字符串运行它:(

4 个答案:

答案 0 :(得分:4)

我测试了这段代码并且工作正常

内部步骤定义文件(ProjectName / features / step_definitions / my_first_steps.rb)添加

Then /^I see an alert with "([^\"]*)" text$/ do |message|
    result = query("view:'UIAlertView' label text:'#{message}'").empty?
    if result
        screenshot_and_raise "could not find text field with AlertView with text '#{message}'"
    end
    sleep(STEP_PAUSE)
end

并在功能文件中

Then I see an alert with "Email cannot be empty." text

如果文字与消息不匹配则会截取屏幕截图并导致测试失败

但这适用于您的自定义提醒而非系统提醒.. !!

如果您需要阅读来自提醒的消息

,这将对您有所帮助

打开$ calabash-ios console

查询,如query("view:'UIAlertView'",:message)

添加更多....

或者您可以使用类似

的内容
Then /^I wait until alert with text "([^\"]*)" and press "([^\"]*)" button$/ do |message, button|

  wait_for_elements_exist(["alertView child label marked:'#{message}'"], :timeout => 30,  :retry_frequency => 0.3,:timeout_message => "Timed out waiting..",:screenshot_on_error => true )
    if element_exists("alertView child label marked:'#{message}'")
      touch("button marked:'#{button}'")
      sleep(STEP_PAUSE)
    else
      screenshot_and_raise "Alert Element not found"
    end
end

答案 1 :(得分:4)

对于iOS 7及更高版本:以下calabash代码可以正常工作。

Then I should see "your text here"
And I should see "Call XXX"
And I should see "Cancel"

适合我。

答案 2 :(得分:3)

添加换行符支持的解决方案是从函数中的字符串中取出变量,以便可以通过ruby代码添加换行符:

Then I see a popup with latitude 10 and longitude 20

呼叫:

Then /^I see a popup with latitude (\d+) and longitude (\d+)$/ do |lat, lon|
  msg = "Latitude:#{lat}\nLongitude:#{lon}"
  should_see_alert_with_text msg
end

使用:

def should_see_alert_with_text (text)
  wait_poll(:until_exists => 'alertView', :timeout => 5) do
    actual = query('alertView child label', :text).first
    unless actual.eql? text
      screenshot_and_raise "should see alert view with message '#{text}' but found '#{actual}'"
    end
  end
end

答案 3 :(得分:1)

calabash-ios问题的链接隐藏在评论中。

https://github.com/calabash/calabash-ios/issues/149

在该问题中,我提供了一个如何使用换行符处理文本搜索的示例。

Karl还建议用多行字符串(“pystrings”)

编写步骤
Then I see an alert with text:
"""
Latitude:59
Longitude:17
"""