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



78
79
80
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 78

def self.authors
  ["Wasabeef"]
end

.available_optionsObject



93
94
95
96
97
98
99
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
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 93

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"),
   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: :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: :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)
  ]
end

.check_has_property(hash_obj, property) ⇒ Object



208
209
210
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 208

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

.descriptionObject



74
75
76
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 74

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

.detailsObject



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

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

.example_codeObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 226

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"]
      )
    end']
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 218

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

.outputObject



222
223
224
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 222

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

.return_valueObject



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

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
# 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] == nil ? "#{params[:project_id]}_test_results" : params[:firebase_test_lab_results_bucket]
  results_dir = "firebase_test_result_#{DateTime.now.strftime('%Y-%m-%d-%H:%M:%S')}"

  # Create the log file
  dirname = File.dirname(params[:console_log_file_name])
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end

  # 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("--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>#{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]))

  # Notify to Slack
  if params[:slack_url] != nil
    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]
  unless owner.nil? || repository.nil? || pr_number.nil? || api_token.nil?
    prefix, comment = Helper.make_github_text(json, params[:project_id], results_bucket, results_dir)
    # 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



212
213
214
215
216
# File 'lib/fastlane/plugin/firebase_test_lab_android/actions/firebase_test_lab_android_action.rb', line 212

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