How to read text from Alert (ios) in calabash

How can I access the warning text on iOS in calabash / oucumber 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 claim that the warning 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 line to just β€œNo” and discard everything else from the line, it really works, but I cannot get it to work with my more complex string :(

+4
source share
4 answers

The solution to adding newline support is to infer variables from the line in functions so that the new line can be added with ruby ​​code:

 Then I see a popup with latitude 10 and longitude 20 

Challenges:

 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 
+3
source

I tested this code and it works perfectly

internal 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 the properties file

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

If the text does not match the message, it will take a screenshot and not run a test

But this works for your custom alerts, not system alerts .. !!

this will help you if you need to read a message from an alert

open $ calabash-ios console and

query of type 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 
+4
source

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

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

It works for me.

+4
source

A link to the problem with Kalabash-ios was buried in the comments.

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

In this problem I will give an example of how to handle text search with new characters.

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

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

Source: https://habr.com/ru/post/1478909/


All Articles