Class: Fastlane::Actions::FirebaseTestLabAndroidAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



85
86
87
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 85

def self.authors
  ["Wasabeef"]
end

.available_optionsObject



100
101
102
103
104
105
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 100

def self.available_options
  [FastlaneCore::ConfigItem.new(key: :project_id,
                                env_name: "PROJECT_ID",
                                description: "Your Firebase project id",
                                is_string: true,
                                optional: false),
   FastlaneCore::ConfigItem.new(key: :gcloud_service_key_file,
                                env_name: "GCLOUD_SERVICE_KEY_FILE",
                                description: "File path containing the gcloud auth key. Default: Created from GCLOUD_SERVICE_KEY environment variable",
                                is_string: true,
                                optional: false),
   FastlaneCore::ConfigItem.new(key: :type,
                                env_name: "TYPE",
                                description: "Test type. Default: robo (robo/instrumentation)",
                                is_string: true,
                                optional: true,
                                default_value: "robo",
                                verify_block: proc do |value|
                                  if value != "robo" && value != "instrumentation"
                                    UI.user_error!("Unknown test type.")
                                  end
                                end),
   FastlaneCore::ConfigItem.new(key: :devices,
                                description: "Devices to test the app on",
                                type: Array,
                                default_value: [{
                                                    model: "Nexus6",
                                                    version: "21",
                                                    locale: "en_US",
                                                    orientation: "portrait"
                                                }],
                                verify_block: proc do |value|
                                  if value.empty?
                                    UI.user_error!("Devices cannot be empty")
                                  end
                                  value.each do |current|
                                    if current.class != Hash
                                      UI.user_error!("Each device must be represented by a Hash object, " \
                                         "#{current.class} found")
                                    end
                                    check_has_property(current, :model)
                                    check_has_property(current, :version)
                                    set_default_property(current, :locale, "en_US")
                                    set_default_property(current, :orientation, "portrait")
                                  end
                                end),
   FastlaneCore::ConfigItem.new(key: :timeout,
                                env_name: "TIMEOUT",
                                description: "The max time this test execution can run before it is cancelled. Default: 5m (this value must be greater than or equal to 1m)",
                                type: String,
                                optional: true,
                                default_value: "3m"),
   FastlaneCore::ConfigItem.new(key: :app_apk,
                                env_name: "APP_APK",
                                description: "The path for your android app apk",
                                type: String,
                                optional: false),
   FastlaneCore::ConfigItem.new(key: :app_test_apk,
                                env_name: "APP_TEST_APK",
                                description: "The path for your android test apk. Default: empty string",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :use_orchestrator,
                                env_name: "USE_ORCHESTRATOR",
                                description: "If you use orchestrator when set instrumentation test. Default: false",
                                type: Boolean,
                                optional: true,
                                default_value: false),
   FastlaneCore::ConfigItem.new(key: :gcloud_components_channel,
                                env_name: "gcloud_components_channel",
                                description: "If you use beta or alpha components. Default stable (alpha/beta)",
                                is_string: true,
                                optional: true,
                                default_value: "stable",
                                verify_block: proc do |value|
                                  if value != "stable" && value != "alpha" && value != "beta"
                                    UI.user_error!("Unknown gcloud component channel.")
                                  end
                                end),
   FastlaneCore::ConfigItem.new(key: :console_log_file_name,
                                env_name: "CONSOLE_LOG_FILE_NAME",
                                description: "The filename to save the output results. Default: ./console_output.log",
                                type: String,
                                optional: true,
                                default_value: "./console_output.log"),
   FastlaneCore::ConfigItem.new(key: :extra_options,
                                env_name: "EXTRA_OPTIONS",
                                description: "Extra options that you need to pass to the gcloud command. Default: empty string",
                                type: String,
                                optional: true,
                                default_value: ""),
   FastlaneCore::ConfigItem.new(key: :slack_url,
                                env_name: "SLACK_URL",
                                description: "If Notify to Slack after finishing of the test. Set your slack incoming webhook url",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :firebase_test_lab_results_bucket,
                                env_name: "FIREBASE_TEST_LAB_RESULTS_BUCKET",
                                description: "Name of Firebase Test Lab results bucket",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :firebase_test_lab_results_dir,
                                env_name: "FIREBASE_TEST_LAB_RESULTS_DIR",
                                description: "Name of Firebase Test Lab results directory",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :github_owner,
                                env_name: "GITHUB_OWNER",
                                description: "Owner name",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :github_repository,
                                env_name: "GITHUB_REPOSITORY",
                                description: "Repository name",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :github_pr_number,
                                env_name: "GITHUB_PR_NUMBER",
                                description: "Pull request number",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :github_api_token,
                                env_name: "GITHUB_API_TOKEN",
                                description: "GitHub API Token",
                                type: String,
                                optional: true,
                                default_value: nil),
   FastlaneCore::ConfigItem.new(key: :download_dir,
                                env_name: "DOWNLOAD_DIR",
                                description: "Target directory to download screenshots from firebase",
                                type: String,
                                optional: true,
                                default_value: nil)
  ]
end

.check_has_property(hash_obj, property) ⇒ Object



243
244
245
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 243

def self.check_has_property(hash_obj, property)
  UI.user_error!("Each device must have #{property} property") unless hash_obj.key?(property)
end

.descriptionObject



81
82
83
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 81

def self.description
  "Runs Android tests in Firebase Test Lab."
end

.detailsObject



95
96
97
98
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 95

def self.details
  # Optional:
  "Test your app with Firebase Test Lab with ease using fastlane"
end

.example_codeObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 261

def self.example_code
  ['before_all do
      ENV["SLACK_URL"] = "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
    end

    lane :test do

      # Get Pull request number
      pr_number = ENV["CI_PULL_REQUEST"] != nil ? ENV["CI_PULL_REQUEST"][/(?<=https:\/\/github.com\/cats-oss\/android\/pull\/)(.*)/] : nil

      # Upload to Firebase Test Lab
      firebase_test_lab_android(
        project_id: "cats-firebase",
        gcloud_service_key_file: "fastlane/client-secret.json",
        type: "robo",
        devices: [
          {
            model: "Nexus6P",
            version: "23",
            locale: "ja_JP",
            orientation: "portrait"
          },
          {
            model: "Pixel2",
            version: "28"
          }
        ],
        app_apk: "test.apk",
        # app_test_apk: "androidTest.apk",
        # use_orchestrator: false,
        console_log_file_name: "fastlane/console_output.log",
        timeout: "3m",
        firebase_test_lab_results_bucket: "firebase_cats_test_bucket",
        slack_url: ENV["SLACK_URL"],
        github_owner: "cats-oss",
        github_repository: "fastlane-plugin-firebase_test_lab_android",
        github_pr_number: pr_number,
        github_api_token: ENV["DANGER_GITHUB_API_TOKEN"],
        download_dir: ".results"
      )
    end']
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 253

def self.is_supported?(platform)
  platform == :android
end

.outputObject



257
258
259
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 257

def self.output
  [['console_output.log', 'A console log when running Firebase Test Lab with gcloud']]
end

.return_valueObject



89
90
91
92
93
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 89

def self.return_value
  ["Authenticates with Google Cloud.",
   "Runs tests in Firebase Test Lab.",
   "Fetches the results to a local directory."].join("\n")
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 9

def self.run(params)
  UI.message("Starting...")

  results_bucket = params[:firebase_test_lab_results_bucket] || "#{params[:project_id]}_test_results"
  results_dir = params[:firebase_test_lab_results_dir] || "firebase_test_result_#{DateTime.now.strftime('%Y-%m-%d-%H:%M:%S')}"

  # Set target project
  Helper.config(params[:project_id])
  # Activate service account
  Helper.authenticate(params[:gcloud_service_key_file])
  # Run Firebase Test Lab
  Helper.run_tests(params[:gcloud_components_channel], "--type #{params[:type]} "\
            "--app #{params[:app_apk]} "\
            "#{"--test #{params[:app_test_apk]} " unless params[:app_test_apk].nil?}"\
            "#{"--use-orchestrator " if params[:type] == "instrumentation" && params[:use_orchestrator]}"\
            "#{params[:devices].map { |d| "--device model=#{d[:model]},version=#{d[:version]},locale=#{d[:locale]},orientation=#{d[:orientation]} " }.join}"\
            "--timeout #{params[:timeout]} "\
            "--results-bucket #{results_bucket} "\
            "--results-dir #{results_dir} "\
            "#{params[:extra_options]} "\
            "--format=json 1>#{Helper.if_need_dir(params[:console_log_file_name])}"
  )

  # Sample data
  # [
  #   {
  #     "axis_value": "Nexus6P-23-ja_JP-portrait",
  #     "outcome": "Passed",
  #     "test_details": "--"
  #   },
  #   {
  #     "axis_value": "Pixel2-28-en_US-portrait",
  #     "outcome": "Passed",
  #     "test_details": "--"
  #   }
  # ]
  json = JSON.parse(File.read(params[:console_log_file_name]))
  UI.message("Test status: #{json}")

  # Fetch results
  download_dir = params[:download_dir]
  if download_dir
    UI.message("Fetch results from Firebase Test Lab results bucket")
    json.each do |status|
      axis = status["axis_value"]
      Helper.if_need_dir("#{download_dir}/#{axis}")
      Helper.copy_from_gcs("#{results_bucket}/#{results_dir}/#{axis}", download_dir)
      Helper.set_public("#{results_bucket}/#{results_dir}/#{axis}")
    end
  end

  # Notify to Slack
  if params[:slack_url]
    success, body = Helper.make_slack_text(json)
    SlackNotifier.notify(params[:slack_url], body, success)
  end

  # Notify to Github
  owner = params[:github_owner]
  repository = params[:github_repository]
  pr_number = params[:github_pr_number]
  api_token = params[:github_api_token]
  if owner && repository && pr_number && api_token
    prefix, comment = Helper.make_github_text(json, params[:project_id], results_bucket, results_dir, params[:type])
    # Delete past comments
    GitHubNotifier.delete_comments(owner, repository, pr_number, prefix, api_token)
    GitHubNotifier.put_comment(owner, repository, pr_number, comment, api_token)
  end

  UI.message("Finishing...")
end

.set_default_property(hash_obj, property, default) ⇒ Object



247
248
249
250
251
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 247

def self.set_default_property(hash_obj, property, default)
  unless hash_obj.key?(property)
    hash_obj[property] = default
  end
end