Top Level Namespace

Includes:
REXML

Defined Under Namespace

Modules: Calabash Classes: Env, JavaKeystore

Constant Summary collapse

WAIT_TIMEOUT =
(ENV['WAIT_TIMEOUT'] || 30).to_f
STEP_PAUSE =
(ENV['STEP_PAUSE'] || 0.5).to_f

Instance Method Summary collapse

Instance Method Details

#build_test_server_if_needed(app_path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/calabash-android/helpers.rb', line 63

def build_test_server_if_needed(app_path)
  unless File.exist?(test_server_path(app_path))
    if ARGV.include? "--no-build"
      puts "No test server found for this combination of app and calabash version. Exiting!"
      exit 1
    else
      puts "No test server found for this combination of app and calabash version. Recreating test server."
      calabash_build(app_path)
    end
  end
end

#checksum(file_path) ⇒ Object



45
46
47
48
# File 'lib/calabash-android/helpers.rb', line 45

def checksum(file_path)
  require 'digest/md5'
  Digest::MD5.file(file_path).hexdigest
end

#extract_md5_fingerprint(fingerprints) ⇒ Object



116
117
118
119
120
# File 'lib/calabash-android/helpers.rb', line 116

def extract_md5_fingerprint(fingerprints)
  m = fingerprints.scan(/MD5.*((?:[a-fA-F\d]{2}:){15}[a-fA-F\d]{2})/).flatten
  raise "No MD5 fingerprint found:\n #{fingerprints}" if m.empty?
  m.first
end

#fingerprint_from_apk(app_path) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/calabash-android/helpers.rb', line 92

def fingerprint_from_apk(app_path)
  app_path = File.expand_path(app_path)
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir) do
      FileUtils.cp(app_path, "app.apk")
      FileUtils.mkdir("META-INF")
      Zip::ZipFile.foreach("app.apk") do |z|
        z.extract if /^META-INF\/\w+.(RSA|rsa)/ =~ z.name
      end
      rsa_files = Dir["#{tmp_dir}/META-INF/*"]

      raise "No RSA file found in META-INF. Cannot proceed." if rsa_files.empty?
      raise "More than one RSA file found in META-INF. Cannot proceed." if rsa_files.length > 1

      cmd = "#{Env.keytool_path} -v -printcert -file \"#{rsa_files.first}\""
      log cmd
      fingerprints = `#{cmd}`
      md5_fingerprint = extract_md5_fingerprint(fingerprints)
      log "MD5 fingerprint for signing cert (#{app_path}): #{md5_fingerprint}"
      md5_fingerprint
    end
  end
end

#framework_path(apk_file_path) ⇒ Object



58
59
60
# File 'lib/calabash-android/helpers.rb', line 58

def framework_path(apk_file_path)
  "test_servers/apktool-#{checksum(apk_file_path)}_#{Calabash::Android::VERSION}"
end

#log(message, error = false) ⇒ Object



122
123
124
# File 'lib/calabash-android/helpers.rb', line 122

def log(message, error = false)
  $stdout.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - #{message}" if (error or ARGV.include? "-v" or ARGV.include? "--verbose")
end

#main_activity(app) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calabash-android/helpers.rb', line 20

def main_activity(app)
  manifest = Document.new(manifest(app))
  main_activity = manifest.elements["//action[@name='android.intent.action.MAIN']/../.."].attributes['name']
  #Handle situation where main activity is on the form '.class_name'
  if main_activity.start_with? "."
    main_activity = package_name(app) + main_activity
  elsif not main_activity.include? "." #This is undocumentet behaviour but Android seems to accept shorthand naming that does not start with '.'
    main_activity = "#{package_name(app)}.#{main_activity}"
  end
  main_activity
end

#manifest(app) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/calabash-android/helpers.rb', line 32

def manifest(app)
  out_path = manifest_path(app)
  manifest_file = File.join(out_path, 'AndroidManifest.xml')
  unless File.size?(manifest_file)
    manifest_extractor = File.join(File.expand_path(File.dirname(__FILE__)),'lib', 'apktool-cli-1.5.3-SNAPSHOT.jar')
    output = `#{Env.java_path} -jar "#{manifest_extractor}" d -s --frame-path "#{framework_path(app)}" -f "#{app}" #{out_path} 2>&1`
    raise "Unable to extract manifest: #{output}" unless File.size?(manifest_file)
    # Tidy up a bit. It would be nice if apktool could just dump the manifest alone.
    FileUtils.rm_rf(%w{res assets classes.dex}.map {|f| File.join(out_path, f) })
  end
  File.read(manifest_file)
end

#manifest_path(apk_file_path) ⇒ Object



54
55
56
# File 'lib/calabash-android/helpers.rb', line 54

def manifest_path(apk_file_path)
  "test_servers/#{checksum(apk_file_path)}_#{Calabash::Android::VERSION}.res"
end

#package_name(app) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/calabash-android/helpers.rb', line 12

def package_name(app)
  require 'rexml/document'
  require 'rexml/xpath'

  manifest = Document.new(manifest(app))
  manifest.root.attributes['package']
end

#resign_apk(app_path) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/calabash-android/helpers.rb', line 75

def resign_apk(app_path)
  Dir.mktmpdir do |tmp_dir|
    log "Resign apk"
    unsigned_path = File.join(tmp_dir, 'unsigned.apk')
    FileUtils.cp(app_path, unsigned_path)

    `#{Env.java_path} -jar "#{File.dirname(__FILE__)}/lib/unsign.jar" "#{unsigned_path}"`

    sign_apk(unsigned_path, app_path)
  end
end

#sign_apk(app_path, dest_path) ⇒ Object



87
88
89
90
# File 'lib/calabash-android/helpers.rb', line 87

def sign_apk(app_path, dest_path)
  java_keystore = JavaKeystore.get_keystores.first
  java_keystore.sign_apk(app_path, dest_path)
end

#test_server_path(apk_file_path) ⇒ Object



50
51
52
# File 'lib/calabash-android/helpers.rb', line 50

def test_server_path(apk_file_path)
  "test_servers/#{checksum(apk_file_path)}_#{Calabash::Android::VERSION}.apk"
end