4

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

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 :(

David Karlsson
  • 9,396
  • 9
  • 58
  • 103

4 Answers4

4

I tested this code and its working fine

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

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 console and

query like 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
  • What do you mean custom alerts? I cant get this working as you wrote it. – David Karlsson May 10 '13 at 09:50
  • custom alerts means the one you make like this in side your code `UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", "") otherButtonTitles:nil]; [alert show];` – Chathura Palihakkara May 10 '13 at 10:09
  • @David : Did you follow the steps correctly can you show me your code, Any error messages you get ? you need to replace the message text "Email cannot be empty." with your one.and you need to give wait time few seconds to appear the alert before you execute this code part. or else there is a function to wait until the screen you defined to appear and then execute your code. – Chathura Palihakkara May 10 '13 at 10:14
  • Im sorry, then i guess im using a custom alertView. The message in my case is actually set from a NSString (I updated my question), so this approach wont work? – David Karlsson May 10 '13 at 10:34
  • hm.. then you need to investigate you alert view and its components using `$ calabash-ios console` and then you can try to query correct identifier like here [link]https://github.com/calabash/calabash-ios/wiki/05-Query-syntax if you successfully get your text from alert to this console then write a ruby function call it from gherkin. – Chathura Palihakkara May 10 '13 at 16:55
  • The problems seems to appear due to newline "\n" in the string – David Karlsson May 10 '13 at 22:14
  • @David even-though my answer is not worked for your special case it works on normal alerts fine. so can you please marked it as correct answer for other users benefit. and when you find the answer to your special case publish your answer with special conditions. Thanks. – Chathura Palihakkara May 11 '13 at 19:21
  • @david you can read your message using `$ calabash-ios console` and query like `query("view:'UIAlertView'",:message)` then you can use it. – Chathura Palihakkara May 14 '13 at 05:28
4

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

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

Works for me.

Chahal
  • 995
  • 2
  • 14
  • 24
  • OK! Works, for me as well, make sure the message you are asserting is exactly the message that is shown not a substring of it! – Denis Mar 06 '14 at 18:50
3

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:

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
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
1

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

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")

Then I see an alert with text:
"""
Latitude:59
Longitude:17
"""
jmoody
  • 2,480
  • 1
  • 16
  • 22