Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 544
David Karlsson Avatar asked Dec 16 '22 11:12

David Karlsson


1 Answers

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
like image 73
Chathura Palihakkara Avatar answered Dec 29 '22 15:12

Chathura Palihakkara