lib/calabash-android/operations.rb in calabash-android-0.4.0.pre6 vs lib/calabash-android/operations.rb in calabash-android-0.4.0.pre7
- old
+ new
@@ -11,11 +11,10 @@
module Calabash module Android
module Operations
-
def log(message)
$stdout.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - #{message}" if (ARGV.include? "-v" or ARGV.include? "--verbose")
end
def take_screenshot
@@ -27,17 +26,22 @@
step(txt)
else
Then(txt)
end
end
+
def default_device
unless @default_device
@default_device = Device.new(self, ENV["ADB_DEVICE_ARG"], ENV["TEST_SERVER_PORT"], ENV["APP_PATH"], ENV["TEST_APP_PATH"])
end
@default_device
end
+ def set_default_device(device)
+ @default_device = device
+ end
+
def performAction(action, *arguments)
default_device.perform_action(action, *arguments)
end
def install_app(app_path)
@@ -95,24 +99,19 @@
end
value
end
def query(uiquery, *args)
- if uiquery.start_with? "webView"
- uiquery.slice!(0, "webView".length)
- if uiquery =~ /(css|xpath):\s*(.*)/
- r = performAction("query", $1, $2)
- JSON.parse(r["message"])
+ converted_args = []
+ args.each do |arg|
+ if arg.is_a?(Hash) and arg.count == 1
+ converted_args << {:method_name => arg.keys.first, :arguments => [ arg.values.first ]}
else
- raise "Invalid query #{uiquery}"
+ converted_args << arg
end
- else
- arguments = [*args]
- operation = {"method_name"=>"query", "arguments" => arguments}
- data = {"query" => uiquery, "operation" => operation}
- JSON.parse(http("/map",data))
end
+ map(uiquery,:query,*converted_args)
end
def ni
raise "Not yet implemented."
end
@@ -132,14 +131,10 @@
puts "(Hooks are stored in features/support)"
end
class Device
- def make_default_device
- @cucumber_world.default_device = self
- end
-
def initialize(cucumber_world, serial, server_port, app_path, test_server_path)
@cucumber_world = cucumber_world
@serial = serial
@server_port = server_port
@app_path = app_path
@@ -318,12 +313,12 @@
if keyguard_enabled?
wake_up
end
- env_options = {:target_package => options[:target_package] || ENV["PACKAGE_NAME"],
- :main_activity => options[:main_activity] || ENV["MAIN_ACTIVITY"],
+ env_options = {:target_package => options[:target_package] || package_name(@app_path),
+ :main_activity => options[:main_activity] || main_activity(@app_path),
:debug => options[:debug] || false,
:class => options[:class] || "sh.calaba.instrumentationbackend.InstrumentationBackend"}
cmd_arr = [adb_command, "shell am instrument"]
@@ -400,21 +395,24 @@
sleep 5
raise(msg)
end
def touch(uiquery,*args)
+ raise "Cannot touch nil" unless uiquery
+
if uiquery.instance_of? String
elements = query(uiquery, *args)
raise "No elements found" if elements.empty?
element = elements.first
else
element = uiquery
+ element = element.first if element.instance_of?(Array)
end
- center_x = element["frame"]["x"] + element["frame"]["width"] / 2
- center_y = element["frame"]["y"] + element["frame"]["height"] / 2
+ center_x = element["rect"]["center_x"]
+ center_y = element["rect"]["center_y"]
performAction("touch_coordinate", center_x, center_y)
end
def http(options, data=nil)
default_device.http(options, data)
@@ -525,11 +523,22 @@
def backdoor(sel, arg)
ni
end
- def map( query, method_name, *method_args )
- ni
+ def map(query, method_name, *method_args)
+ operation_map = {
+ :method_name => method_name,
+ :arguments => method_args
+ }
+ res = http("/map",
+ {:query => query, :operation => operation_map})
+ res = JSON.parse(res)
+ if res['outcome'] != 'SUCCESS'
+ screenshot_and_raise "map #{query}, #{method_name} failed because: #{res['reason']}\n#{res['details']}"
+ end
+
+ res['results']
end
def url_for( verb )
ni
end