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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16351791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:19:19  来源:igfitidea点击:

How do i read text from an Alert(ios) in calabash

iosxcodecucumbercalabash

提问by David Karlsson

How can I access the text of an alertview on iOS in my calabash/cucumbertests?

如何在我的葫芦/黄瓜测试中访问 iOS 上的警报视图文本?

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];

I want to assert that the alert has the expected content:

我想断言警报具有预期的内容:

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

So if i change the string to simply "No:" and discard everything else from the string, it does actually seem to work, but i cant get it running with my more complex string :(

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

采纳答案by David Karlsson

A solution to add newline support is to take the variables out of the string in the features so that the newline can be added by the ruby code:

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

Then I see a popup with latitude 10 and longitude 20

Calls:

调用:

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

Using:

使用:

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

回答by Chathura Palihakkara

I tested this code and its working fine

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

inside step definition file (ProjectName/features/step_definitions/my_first_steps.rb) add

在步骤定义文件 (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

and in feature file

并在功能文件中

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

if text doesn't match with the message it will take a screenshot and fails the test

如果文本与消息不匹配,它将截取屏幕截图并通过测试

But this is working for your custom alerts not on system alerts..!!

但这适用于您的自定义警报,而不适用于系统警报..!!

this will help you if you need to read the message from alert

如果您需要阅读警报中的消息,这将对您有所帮助

open $ calabash-ios consoleand

打开$ calabash-ios console

query like query("view:'UIAlertView'",:message)

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

add more....

添加更多....

Or You can use something like

或者你可以使用类似的东西

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

回答by Chahal

For iOS 7 and above: following calabash code will work fine.

对于 iOS 7 及更高版本:以下葫芦代码将正常工作。

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

Works for me.

对我来说有效。

回答by jmoody

The link to the calabash-ios issue was buried in the comments.

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

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

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

In that issue, I provide an example of how to handle search for text with newlines.

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

Karl also suggests writing the step with multi-line strings ("pystrings")

Karl 还建议使用多行字符串(“pystrings”)编写步骤

Then I see an alert with text:
"""
Latitude:59
Longitude:17
"""