Class: Fastlane::Actions::AppiumAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/appium.rb

Constant Summary collapse

INVOKE_TIMEOUT =
30
APPIUM_PATH_HOMEBREW =
'/usr/local/bin/appium'
APPIUM_APP_PATH =
'/Applications/Appium.app'
APPIUM_APP_BUNDLE_PATH =
'Contents/Resources/node_modules/.bin/appium'

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, deprecated_notes, details, lane_context, method_missing, other_action, output, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorObject



176
177
178
# File 'fastlane/lib/fastlane/actions/appium.rb', line 176

def self.author
  'yonekawa'
end

.available_optionsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'fastlane/lib/fastlane/actions/appium.rb', line 106

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :platform,
      env_name: 'FL_APPIUM_PLATFORM',
      description: 'Appium platform name',
      is_string: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :spec_path,
      env_name: 'FL_APPIUM_SPEC_PATH',
      description: 'Path to Appium spec directory',
      is_string: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :app_path,
      env_name: 'FL_APPIUM_APP_FILE_PATH',
      description: 'Path to Appium target app file',
      is_string: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :invoke_appium_server,
      env_name: 'FL_APPIUM_INVOKE_APPIUM_SERVER',
      description: 'Use local Appium server with invoke automatically',
      is_string: false,
      default_value: true,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :host,
      env_name: 'FL_APPIUM_HOST',
      description: 'Hostname of Appium server',
      is_string: true,
      default_value: '0.0.0.0',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :port,
      env_name: 'FL_APPIUM_PORT',
      description: 'HTTP port of Appium server',
      is_string: false,
      default_value: 4723,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :appium_path,
      env_name: 'FL_APPIUM_EXECUTABLE_PATH',
      description: 'Path to Appium executable',
      is_string: true,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :caps,
      env_name: 'FL_APPIUM_CAPS',
      description: 'Hash of caps for Appium::Driver',
      is_string: false,
      type: Hash,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :appium_lib,
      env_name: 'FL_APPIUM_LIB',
      description: 'Hash of appium_lib for Appium::Driver',
      is_string: false,
      type: Hash,
      optional: true
    )
  ]
end

.categoryObject



184
185
186
# File 'fastlane/lib/fastlane/actions/appium.rb', line 184

def self.category
  :testing
end

.configure_rspec(params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'fastlane/lib/fastlane/actions/appium.rb', line 77

def self.configure_rspec(params)
  RSpec.configure do |c|
    c.before(:each) do
      caps = params[:caps] || {}
      caps[:platformName] ||= params[:platform]
      caps[:autoAcceptAlerts] ||= true
      caps[:app] = params[:app_path]

      appium_lib = params[:appium_lib] || {}

      @driver = Appium::Driver.new(
        caps: caps,
        server_url: params[:host],
        port: params[:port],
        appium_lib: appium_lib
      ).start_driver
      Appium.promote_appium_methods(RSpec::Core::ExampleGroup)
    end

    c.after(:each) do
      @driver.quit unless @driver.nil?
    end
  end
end

.descriptionObject



102
103
104
# File 'fastlane/lib/fastlane/actions/appium.rb', line 102

def self.description
  'Run UI test by Appium with RSpec'
end

.detect_appium(params) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'fastlane/lib/fastlane/actions/appium.rb', line 43

def self.detect_appium(params)
  appium_path = params[:appium_path] || `which appium`.to_s.strip

  if appium_path.empty?
    if File.exist?(APPIUM_PATH_HOMEBREW)
      appium_path = APPIUM_PATH_HOMEBREW
    elsif File.exist?(APPIUM_APP_PATH)
      appium_path = APPIUM_APP_PATH
    end
  end

  unless File.exist?(appium_path)
    UI.user_error!("You have to install Appium using `npm install -g appium`")
  end

  if appium_path.end_with?('.app')
    appium_path = "#{appium_path}/#{APPIUM_APP_BUNDLE_PATH}"
  end

  UI.message("Appium executable detected: #{appium_path}")
  appium_path
end

.example_codeObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'fastlane/lib/fastlane/actions/appium.rb', line 188

def self.example_code
  [
    'appium(
      app_path:  "appium/apps/TargetApp.app",
      spec_path: "appium/spec",
      platform:  "iOS",
      caps: {
        versionNumber: "9.1",
        deviceName:    "iPhone 6"
      },
      appium_lib: {
        wait: 10
      }
    )'
  ]
end

.invoke_appium_server(params) ⇒ Object



38
39
40
41
# File 'fastlane/lib/fastlane/actions/appium.rb', line 38

def self.invoke_appium_server(params)
  appium = detect_appium(params)
  Process.spawn("#{appium} -a #{params[:host]} -p #{params[:port]}")
end

.is_supported?(platform) ⇒ Boolean

Returns:



180
181
182
# File 'fastlane/lib/fastlane/actions/appium.rb', line 180

def self.is_supported?(platform)
  [:ios, :android].include?(platform)
end

.run(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'fastlane/lib/fastlane/actions/appium.rb', line 9

def self.run(params)
  Actions.verify_gem!('rspec')
  Actions.verify_gem!('appium_lib')

  require 'rspec'
  require 'appium_lib' unless Helper.test?

  FastlaneCore::PrintTable.print_values(
    config: params,
    title: "Summary for Appium Action"
  )

  if params[:invoke_appium_server]
    appium_pid = invoke_appium_server(params)
    wait_for_appium_server(params)
  end

  configure_rspec(params)

  rspec_args = []
  rspec_args << params[:spec_path]
  status = RSpec::Core::Runner.run(rspec_args).to_i
  if status != 0
    UI.user_error!("Failed to run Appium spec. status code: #{status}")
  end
ensure
  Actions.sh("kill #{appium_pid}") if appium_pid
end

.wait_for_appium_server(params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'fastlane/lib/fastlane/actions/appium.rb', line 66

def self.wait_for_appium_server(params)
  loop.with_index do |_, count|
    break if `lsof -i:#{params[:port]}`.to_s.length != 0

    if count * 5 > INVOKE_TIMEOUT
      UI.user_error!("Invoke Appium server timed out")
    end
    sleep(5)
  end
end