Skip to content

Instantly share code, notes, and snippets.

@TeresaP
Last active March 11, 2016 17:13
Show Gist options
  • Save TeresaP/b7373a907ebb31acac39 to your computer and use it in GitHub Desktop.
Save TeresaP/b7373a907ebb31acac39 to your computer and use it in GitHub Desktop.
Allows users to interact with native alerts/dialogs using the Calabash test framework
#From https://github.com/jmoody/briar/blob/develop/lib/briar/alerts_and_sheets/alert_view.rb
#From https://github.com/jmoody/briar/blob/405aacf4e39cecaeca3da31c2503c62d4ef8bc8e/lib/briar/briar_core.rb
# Lets us interact with alerts/dialogs. Even native ones.
class Alert
extend Calabash::Cucumber::Operations
# Check to see if the alert is open
# @param [String] alert_id (Optional) Alert title/text to use in verification
# @return [Boolean] True if the alert is showing
def self.open?(alert_id=nil)
res = uia('uia.alert() != null')
if alert_id.nil?
return res['value']
else
return self.alert_text_exists?(alert_id)
end
end
# Check to see if the alert button is showing
# @param [String] button_label Name of the button
# @return [Boolean] True if the alert button is showing
def self.alert_button_exists?(button_label)
self.wait_for_alert_open
return self.alert_text_exists?(button_label)
end
# Check to see if the alert text is showing
# @param [String] text Text string to look for
# @return [Boolean] True if the alert text is showing
def self.alert_text_exists?(text)
self.wait_for_alert_open
return !uia_query(:view, {:marked => "#{text}"}).empty?
end
# Taps the alert button and verifies the alert disappears afterwards
# @param [String] button_label Name of the button to press
def self.dismiss_alert_with_button(button_label)
self.tap_alert_button(button_label)
wait_for(:timeout => 5) { !self.alert_button_exists?(button_label) }
end
# Taps the default alert button (usually the confirmation button, not the cancel button)
def self.tap_default_alert_button
uia('target.frontMostApp().alert().defaultButton().tap();')
sleep(STEP_PAUSE)
end
# Taps the specified alert button
# @param [String] button_label The name of the button
def self.tap_alert_button(button_label)
self.wait_for_alert_open
uia_tap(:view, {:marked => "#{button_label}"})
sleep(STEP_PAUSE)
end
# Taps the specified alert field
# @param [String] field_id The name of the field to tap
def self.tap_alert_field(field_id)
self.tap_alert_button(field_id)
end
# Wait for the specified alert to close
# @param [String] alert_title The title of the alert
def self.wait_for_alert_closed(alert_title = nil)
msg = "Waited for the alert called '#{alert_title}' to disappear but it is still visible."
options = {:timeout_message => msg, :retry_frequency => 0.1}
wait_for(options) do
!self.open?(alert_title)
end
end
# Wait for the specified alert button to be visible
# @param [String] button_label The label on the button
def self.wait_for_alert_button_visible(button_label)
unless self.alert_button_exists?(button_label)
log_failure("Could not find alert view with button '#{button_label}'.")
end
end
# Wait for the specified alert to be visible
# @param [String] alert_title The alert title
def self.wait_for_alert_open(alert_title = nil)
alert_title.nil? ? msg = ('Should see the alert view.') : msg = ("Should see the alert view marked '#{alert_title}'.")
options = {:timeout_message => msg, :retry_frequency => 0.1}
wait_for(options) do
self.open?(alert_title)
end
end
# Directly enter the text in the specified field
# @param [String] text Text to enter in the field
def self.enter_text_in_field(text)
self.wait_for_alert_open
uia_set_responder_value(text)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment