Last active
August 29, 2015 14:05
-
-
Save SeanFelipe/951ea8f3997d641fff16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pages can be defined like so: | |
LoginScreen < ScreenModel | |
element :email, "et_email" | |
element :password, "et_pw" | |
element :login_button, "btn_login" | |
end | |
# and called in step definitions like so | |
Then(/^I enter my email and password and press the login button$/) do | |
LoginScreen.email.type("foo@bar.com") | |
LoginScreen.password.type("foobar") | |
LoginScreen.login_button.press | |
end | |
require 'calabash-android/operations' | |
module ScreenModels | |
# analogs of Android views for running Calabash tests | |
# test view classes implement basic test actions - press, wait_for, is_visible, etc | |
# requires environment var ADB_DEVICE_ARG | |
class View | |
# get the performAction method from Calabash | |
# which does the touchy feely bits | |
include Calabash::Android::Operations | |
def initialize(viewid, array_index=-1) | |
# to support multiple instances of the view on a page --> | |
# if the view will appear more than once, add an array_index; | |
# otherwise, since Ruby will return nil for any uninitialized attribute, | |
# use -1 to indicate there is no indexing for this view | |
@index = array_index | |
@id = viewid | |
end | |
def wait_for | |
proceed = false | |
tries = 0 | |
while not proceed do | |
begin | |
performAction('wait_for_view_by_id', @id) | |
if tries >= 1 # if we've timed out, make a note if we've recovered | |
puts "view found: #{@id}" | |
end | |
proceed = true | |
rescue RuntimeError | |
# Robotium will time out after 10 seconds, then throw an error | |
# catch that error up to three times, then give up | |
tries += 1 | |
puts "timed out waiting for #{@id}, #{tries} of 3..." | |
if tries == 3 | |
raise RuntimeError, "timed out waiting for view #{@id}" | |
proceed = true | |
end | |
end | |
end | |
end | |
def is_visible | |
performAction('assert_view_property', @id, "visibility", "visible") | |
end | |
end | |
class TextView < View | |
def get_text | |
result_hash = performAction("get_text_by_id", @id) | |
return result_hash["message"] | |
end | |
alias_method :text, :get_text | |
def should_equal(s) | |
get_text == s | |
end | |
def number | |
#get only the numericals | |
num = get_text.gsub(/\D/, '') | |
num.to_i | |
end | |
end | |
class EditText < View | |
def type(text) | |
performAction("enter_text_into_id_field", text, @id) | |
end | |
end | |
class Button < View | |
def touch(text=nil) | |
if text | |
performAction('click_on_text', text) | |
else | |
wait_for | |
performAction('click_on_view_by_id', @id) | |
end | |
end | |
alias_method :press, :touch | |
end | |
class ScreenModel | |
extend Calabash::Android::Operations | |
def self.element(element_name, element_view_type, android_id) | |
class_eval <<-CODE | |
@@#{element_name} = #{element_view_type}.new(android_id) | |
def self.#{element_name} | |
@@#{element_name} | |
end | |
CODE | |
end | |
def self.logout | |
# globally accessible logout method | |
sleep 1 | |
performAction('press_menu') | |
performAction('click_on_text', 'Logout') | |
end | |
def self.scroll_down | |
performAction('scroll_down') | |
end | |
def self.scroll_up | |
performAction('scroll_down') | |
end | |
def self.type(text) | |
# a type method outside of Calabash that uses adb shell input text | |
# when you need to force the issue | |
if text.match(" ") != nil | |
raise ArgumentError.new("'adb shell input text' doesn't support whitespace") | |
else | |
`adb -s #{ENV["ADB_DEVICE_ARG"]} shell input text #{text}` | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment