kraken-mobile 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -11
  3. data/bin/kraken-mobile +55 -67
  4. data/bin/kraken_mobile_calabash_android.rb +39 -0
  5. data/bin/kraken_mobile_helpers.rb +91 -0
  6. data/bin/kraken_mobile_setup.rb +134 -0
  7. data/calabash-android-features-skeleton/step_definitions/mobile_steps.rb +1 -0
  8. data/calabash-android-features-skeleton/support/app_installation_hooks.rb +2 -0
  9. data/calabash-android-features-skeleton/support/app_life_cycle_hooks.rb +3 -4
  10. data/calabash-android-features-skeleton/support/env.rb +1 -1
  11. data/calabash-android-features-skeleton/web/step_definitions/web_steps.rb +3 -0
  12. data/calabash-android-features-skeleton/web/support/app_life_cycle_hooks.rb +15 -0
  13. data/lib/kraken-mobile/device_process.rb +94 -0
  14. data/lib/kraken-mobile/helpers/devices_helper/adb_helper.rb +100 -105
  15. data/lib/kraken-mobile/helpers/kraken_faker.rb +107 -0
  16. data/lib/kraken-mobile/hooks/mobile_kraken_hooks.rb +15 -0
  17. data/lib/kraken-mobile/hooks/mobile_operations.rb +36 -0
  18. data/lib/kraken-mobile/hooks/web_operations.rb +33 -0
  19. data/lib/kraken-mobile/mobile/adb.rb +66 -0
  20. data/lib/kraken-mobile/mobile/android_commands.rb +43 -0
  21. data/lib/kraken-mobile/mobile/mobile_process.rb +82 -0
  22. data/lib/kraken-mobile/models/android_device.rb +121 -0
  23. data/lib/kraken-mobile/models/device.rb +113 -31
  24. data/lib/kraken-mobile/models/feature_file.rb +108 -0
  25. data/lib/kraken-mobile/models/feature_scenario.rb +24 -0
  26. data/lib/kraken-mobile/models/web_device.rb +86 -0
  27. data/lib/kraken-mobile/monkeys/mobile/android_monkey.rb +30 -0
  28. data/lib/kraken-mobile/monkeys/mobile/kraken_android_monkey.rb +54 -0
  29. data/lib/kraken-mobile/runners/calabash/android/steps/communication_steps.rb +15 -0
  30. data/lib/kraken-mobile/steps/general_steps.rb +82 -0
  31. data/lib/kraken-mobile/steps/mobile/kraken_steps.rb +72 -0
  32. data/lib/kraken-mobile/steps/web/kraken_steps.rb +81 -0
  33. data/lib/kraken-mobile/test_scenario.rb +193 -0
  34. data/lib/kraken-mobile/utils/feature_reader.rb +17 -0
  35. data/lib/kraken-mobile/utils/k.rb +67 -0
  36. data/lib/kraken-mobile/utils/mobile_cucumber.rb +2 -0
  37. data/lib/kraken-mobile/utils/reporter.rb +453 -0
  38. data/lib/kraken-mobile/version.rb +2 -2
  39. data/lib/kraken-mobile/web/web_process.rb +39 -0
  40. data/lib/kraken_mobile.rb +77 -0
  41. data/reporter/index.html.erb +6 -6
  42. metadata +89 -9
  43. data/bin/kraken-mobile-calabash-android.rb +0 -85
  44. data/bin/kraken-mobile-generate.rb +0 -19
  45. data/bin/kraken-mobile-helpers.rb +0 -48
  46. data/bin/kraken-mobile-setup.rb +0 -50
  47. data/calabash-android-features-skeleton/step_definitions/kraken_steps.rb +0 -1
  48. data/lib/kraken-mobile.rb +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a48ddcdc40dc988342ab4cd94ffa1e34abb22a93587c9c6f2f8ef31e6e85a757
4
- data.tar.gz: c5a11c855e05688d304f3e2c79b2a95c928fd92cfedf035c8891b0d1c746ab6f
3
+ metadata.gz: 8a62f2fa104aef33dbd29eeb781a1af75ebeac4306038023d6fc1abd51235a5c
4
+ data.tar.gz: 9d3b50a9fa14ccb8e411ff760733be78f91b856e0f99805b24f67c837b0e03c4
5
5
  SHA512:
6
- metadata.gz: d86f8f4bb10ebcd523afbfbd567922159596ec67765d2ed17b6b55203013ac88e680c123b4bab68561aca682cedd1c634eee75088aa114d1b077a1b153c0bef5
7
- data.tar.gz: d8e5fb18604ab6778b1c79d0eab28686196b4eb4b86311a3d0682ebb6ab62d3c833d9be964d1ccf4c631cf762c99ac7b108dd74f1d6b0e4053d104c6db63fb31
6
+ metadata.gz: 0ef9c9887cdd96ea1fd702c93c1ca7ebaf98a889328927f8834e76a0f47a6f36a40d6d46617e765db1b7f51f1a38792db8d2796d6dd4a1157539236dbe7e38f9
7
+ data.tar.gz: 0a91cd009596a0adc065b72eafaff018e65797ad79bcdcadf03ba41bc530e871a7de736b00c4572d1cd0f776386e3be1b00a245c44773122d4dfc5a795c8e020
data/README.md CHANGED
@@ -176,7 +176,7 @@ You can use the specified properties using the following sintax.
176
176
 
177
177
  ```Gherkin
178
178
  @user1
179
- Scenario: As a kjkhdkjds
179
+ Scenario: As a user
180
180
  Given I wait
181
181
  Then I see the text "<PASSWORD>"
182
182
  ```
@@ -187,17 +187,51 @@ Scenario: As a kjkhdkjds
187
187
  kraken-mobile run <apk> --properties=<kraken_mobile_properties_path>
188
188
  ```
189
189
 
190
+ # Use fake strings in tests
191
+
192
+ Kraken offers a Fake string generator thanks to the Ruby gem [Faker](https://github.com/faker-ruby/faker), the list of supported faker types are listed as follows:
193
+
194
+ * Name
195
+ * Number
196
+ * Email
197
+ * String
198
+ * String Date
199
+
200
+ ### Use a faker in a test
201
+
202
+ Kraken keeps a record of every Fake string generated, thats why each string will have an id associated. To generate a Faker string you need to follow the structure "$FAKERNAME_ID".
203
+
204
+ ```Gherkin
205
+ @user1
206
+ Scenario: As a user
207
+ Given I wait
208
+ Then I enter text "$name_1" into field with id "view"
209
+ ```
210
+
211
+ ### Reusing a fake string
212
+
213
+ As mentioned before, Kraken keeps record of every string generated with an id given to each string, this gives you the possibility of reusing this string later in your scenario. To reuse a string you can you need to append a $ character to the fake string as follows:
214
+
215
+ ```Gherkin
216
+ @user1
217
+ Scenario: As a user
218
+ Given I wait
219
+ Then I enter text "$name_1" into field with id "view"
220
+ Then I press "add_button"
221
+ Then I should see "$$name_1"
222
+ ```
223
+
190
224
  # Examples
191
225
 
192
226
  | Application | Video | Feature File | Properties file | Settings File | Report Link |
193
227
  |:-------------|:-------------|:------------------|:-------|:-------|:-------|
194
- | Infinite Words | [video](https://www.youtube.com/watch?v=4lX7mO80w-4&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=3&t=0s)|[.feature](/examples/infinite-words/infinite_words.feature) | --- | --- | [report](/examples/infinite-words/report/index.html) |
195
- | QuizUp | [video](https://www.youtube.com/watch?v=2mhZVTK0r6k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=4&t=1s) | [.feature](/examples/quizup/quizup.feature) | --- | --- | [report](/examples/quizup/report/index.html) |
196
- | Spotify/Shazam | [video](https://www.youtube.com/watch?v=7AKsfY1KFX0&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=5&t=0s) | [.feature](/examples/shazam/shazam.feature) | [.json](/examples/shazam/kraken_properties.json) | [.json](/examples/shazam/kraken_mobile_settings.json) | [report](/examples/shazam/report/index.html) |
197
- | Spunky | [video](https://www.youtube.com/watch?v=WOhRWkdFaVk&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=6&t=25s) | [.feature](/examples/spunky/spunky.feature) | --- | --- | [report](/examples/spunky/report/index.html) |
198
- | Picap | [video](https://www.youtube.com/watch?v=RozQrmH_Z5k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=7&t=3s) | [.feature](/examples/picap/picap.feature) | [.json](/examples/picap/kraken_properties.json) | --- | [report](/examples/picap/report/index.html) |
199
- | AskFM | [video](https://www.youtube.com/watch?v=d9Gbdx8kFX8&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=8&t=0s) | [.feature](/examples/askfm/askfm.feature) | --- | --- | [report](/examples/askfm/report/index.html) |
200
- | Stick Men Fight | [video](https://www.youtube.com/watch?v=36OJKNj0nSo&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=9&t=4s) | [.feature](/examples/stick/stick.feature) | --- | --- | [report](/examples/stick/report/index.html) |
201
- | Tic Tac Toe | [video](https://www.youtube.com/watch?v=F9pDJDYsL_w&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=10&t=2s) | [.feature](/examples/tictactoe/tictactoe.feature) | --- | --- | [report](/examples/tictactoe/report/index.html) |
202
- | Tumblr | [video](https://www.youtube.com/watch?v=eqFej2uJz4k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=11&t=3s) | [.feature](/examples/tumblr/tumblr.feature) | [.json](/examples/tumblr/kraken_properties.json) | --- | [report](/examples/tumblr/report/index.html) |
203
- | F3 | [video](https://www.youtube.com/watch?v=vESh6Jyp-so&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=12&t=0s) | [.feature](/examples/f3/f3.feature) | [.json](/examples/f3/kraken_properties.json) | --- | [report](/examples/f3/report/index.html) |
228
+ | Infinite Words | [video](https://www.youtube.com/watch?v=4lX7mO80w-4&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=3&t=0s)|[.feature](../gh-pages/examples/infinite-words/infinite_words.feature) | --- | --- | [report](../gh-pages/examples/infinite-words/report/index.html) |
229
+ | QuizUp | [video](https://www.youtube.com/watch?v=2mhZVTK0r6k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=4&t=1s) | [.feature](../gh-pages/examples/quizup/quizup.feature) | --- | --- | [report](../gh-pages/examples/quizup/report/index.html) |
230
+ | Spotify/Shazam | [video](https://www.youtube.com/watch?v=7AKsfY1KFX0&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=5&t=0s) | [.feature](../gh-pages/examples/shazam/shazam.feature) | [.json](../gh-pages/examples/shazam/kraken_properties.json) | [.json](../gh-pages/examples/shazam/kraken_mobile_settings.json) | [report](../gh-pages/examples/shazam/report/index.html) |
231
+ | Spunky | [video](https://www.youtube.com/watch?v=WOhRWkdFaVk&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=6&t=25s) | [.feature](../gh-pages/examples/spunky/spunky.feature) | --- | --- | [report](../gh-pages/examples/spunky/report/index.html) |
232
+ | Picap | [video](https://www.youtube.com/watch?v=RozQrmH_Z5k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=7&t=3s) | [.feature](../gh-pages/examples/picap/picap.feature) | [.json](../gh-pages/examples/picap/kraken_properties.json) | --- | [report](../gh-pages/examples/picap/report/index.html) |
233
+ | AskFM | [video](https://www.youtube.com/watch?v=d9Gbdx8kFX8&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=8&t=0s) | [.feature](../gh-pages/examples/askfm/askfm.feature) | --- | --- | [report](../gh-pages/examples/askfm/report/index.html) |
234
+ | Stick Men Fight | [video](https://www.youtube.com/watch?v=36OJKNj0nSo&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=9&t=4s) | [.feature](../gh-pages/examples/stick/stick.feature) | --- | --- | [report](../gh-pages/examples/stick/report/index.html) |
235
+ | Tic Tac Toe | [video](https://www.youtube.com/watch?v=F9pDJDYsL_w&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=10&t=2s) | [.feature](../gh-pages/examples/tictactoe/tictactoe.feature) | --- | --- | [report](../gh-pages/examples/tictactoe/report/index.html) |
236
+ | Tumblr | [video](https://www.youtube.com/watch?v=eqFej2uJz4k&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=11&t=3s) | [.feature](../gh-pages/examples/tumblr/tumblr.feature) | [.json](../gh-pages/examples/tumblr/kraken_properties.json) | --- | [report](../gh-pages/examples/tumblr/report/index.html) |
237
+ | F3 | [video](https://www.youtube.com/watch?v=vESh6Jyp-so&list=PLF5U8kfVgRcJ3RCHt7cWmwlqN93brbVW-&index=12&t=0s) | [.feature](../gh-pages/examples/f3/f3.feature) | [.json](../gh-pages/examples/f3/kraken_properties.json) | --- | [report](../gh-pages/examples/f3/report/index.html) |
@@ -1,91 +1,79 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.join(File.dirname(__FILE__), "kraken-mobile-helpers")
4
- require File.join(File.dirname(__FILE__), "kraken-mobile-setup")
5
- require File.join(File.dirname(__FILE__), "kraken-mobile-calabash-android")
6
- $LOAD_PATH << File.expand_path("../../lib", __FILE__)
7
- require 'kraken-mobile'
3
+ require File.join(File.dirname(__FILE__), 'kraken_mobile_helpers')
4
+ require File.join(File.dirname(__FILE__), 'kraken_mobile_setup')
5
+ require File.join(File.dirname(__FILE__), 'kraken_mobile_calabash_android')
6
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
7
+ require 'kraken_mobile'
8
8
  require 'kraken-mobile/constants'
9
9
 
10
10
  #-------------------------------
11
11
  # AGV reader helper
12
12
  #-------------------------------
13
13
 
14
- def read_specified_runner
15
- runner_command = ARGV.select { |arg| arg.include?("--runner=") }.first
16
- if runner_command
17
- formatted_runner_command = runner_command.strip.downcase
18
- formatted_runner_command.slice!("--runner=")
19
- formatted_runner_command
20
- end
21
- end
14
+ def read_configuration
15
+ configuration_command = ARGV.select do |arg|
16
+ arg.include?('--configuration=')
17
+ end.first
18
+ return if configuration_command.nil?
22
19
 
23
- def read_specified_protocol
24
- protocol_command = ARGV.select { |arg| arg.include?("--protocol=") }.first
25
- if protocol_command
26
- formatted_protocol_command = protocol_command.strip.downcase
27
- formatted_protocol_command.slice!("--protocol=")
28
- if KrakenMobile::Constants::SUPPORTED_PROTOCOLS.include?(formatted_protocol_command)
29
- formatted_protocol_command
30
- else # Protocol not supported.
31
- raise "Specified protocol not supported."
32
- end
33
- else # If protocol not specified use file based as default.
34
- KrakenMobile::Constants::FILE_PROTOCOL
35
- end
20
+ configuration_command = configuration_command.strip.downcase
21
+ configuration_command.slice!('--configuration=')
22
+ configuration_command
36
23
  end
37
24
 
38
- def read_configuration
39
- configuration_command = ARGV.select { |arg| arg.include?("--configuration=") }.first
40
- if configuration_command
41
- configuration_command = configuration_command.strip.downcase
42
- configuration_command.slice!("--configuration=")
43
- configuration_command
44
- end
25
+ def read_user_entered_properties_path
26
+ properties_command = ARGV.select { |arg| arg.include?('--properties=') }.first
27
+ return if properties_command.nil?
28
+
29
+ properties_command = properties_command.strip.downcase
30
+ properties_command.slice!('--properties=')
31
+ properties_command
45
32
  end
46
33
 
47
- def read_properties
48
- properties_command = ARGV.select { |arg| arg.include?("--properties=") }.first
49
- if properties_command
50
- properties_command = properties_command.strip.downcase
51
- properties_command.slice!("--properties=")
52
- properties_command
53
- end
34
+ #-------------------------------
35
+ # Helpers
36
+ #-------------------------------
37
+
38
+ def user_entered_apk_path
39
+ return nil if ARGV.first.nil?
40
+
41
+ first_argument = ARGV.first
42
+ return nil if first_argument.start_with?('--') # Is argument parameter not apk
43
+
44
+ File.expand_path(first_argument)
54
45
  end
55
46
 
56
47
  #-------------------------------
57
48
  # Command reader
58
49
  #-------------------------------
59
50
 
60
- if ARGV.length == 0
61
- print_usage
51
+ if ARGV.length.zero?
52
+ print_usage
62
53
  else
63
54
  cmd = ARGV.shift
64
55
  case cmd
65
- when 'version'
66
- require 'kraken-mobile/version'
67
- puts KrakenMobile::VERSION
68
- when 'devices'
69
- require 'kraken-mobile/helpers/devices_helper/manager'
70
- print_devices
71
- when 'setup'
72
- kraken_setup
73
- else
74
- runner_command = read_specified_runner()
75
- protocol = read_specified_protocol()
76
- configuration = read_configuration()
77
- properties = read_properties()
78
- if runner_command
79
- case runner_command
80
- when KrakenMobile::Constants::CALABASH_ANDROID
81
- require File.join(File.dirname(__FILE__), "kraken-mobile-calabash-android")
82
- handle_calabash_android(cmd, protocol, configuration, properties)
83
- else
84
- raise "Specified runner not supported."
85
- end
86
- else # If runner not specified use calabash android as default.
87
- require File.join(File.dirname(__FILE__), "kraken-mobile-calabash-android")
88
- handle_calabash_android(cmd, protocol, configuration, properties)
89
- end
56
+ when 'version'
57
+ require 'kraken-mobile/version'
58
+ puts KrakenMobile::VERSION
59
+ when 'devices'
60
+ require 'kraken-mobile/helpers/devices_helper/manager'
61
+ print_devices
62
+ when 'setup'
63
+ KrakenSetup.new.run
64
+ when 'gen'
65
+ scaffold_folder_structure
66
+ when 'resign'
67
+ require 'calabash-android/helpers'
68
+ puts 'Resigning APK with Calabash-Android'
69
+ ensure_apk_is_specified
70
+ resign_apk(user_entered_apk_path)
71
+ when 'run'
72
+ require File.join(File.dirname(__FILE__), 'kraken_mobile_calabash_android')
73
+ configuration = read_configuration
74
+ user_entered_properties_path = read_user_entered_properties_path
75
+ handle_calabash_android(configuration, user_entered_properties_path)
76
+ else
77
+ print_usage
90
78
  end
91
79
  end
@@ -0,0 +1,39 @@
1
+ @features_dir = File.join(FileUtils.pwd, 'features')
2
+ @support_dir = File.join(@features_dir, 'support')
3
+ @source_dir = File.join(
4
+ File.dirname(__FILE__),
5
+ '..',
6
+ 'calabash-android-features-skeleton'
7
+ )
8
+
9
+ def handle_calabash_android(config_path, properties_path)
10
+ require 'kraken-mobile/constants'
11
+ require 'calabash-android/helpers'
12
+
13
+ kraken = KrakenApp.new(
14
+ apk_path: user_entered_apk_path,
15
+ properties_path: format_properties(properties_path),
16
+ config_path: format_config(config_path)
17
+ )
18
+ kraken.start
19
+ end
20
+
21
+ private
22
+
23
+ def format_properties(properties_path)
24
+ return if properties_path.nil?
25
+
26
+ properties_absolute_path = File.expand_path(properties_path)
27
+ ensure_properties_is_valid(properties_absolute_path)
28
+
29
+ properties_absolute_path
30
+ end
31
+
32
+ def format_config(config_path)
33
+ return if config_path.nil?
34
+
35
+ config_absolute_path = File.expand_path(config_path)
36
+ ensure_configuration_is_valid(config_absolute_path)
37
+
38
+ config_absolute_path
39
+ end
@@ -0,0 +1,91 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+
4
+ #-------------------------------
5
+ # Helpers
6
+ #-------------------------------
7
+
8
+ def print_usage
9
+ puts <<USAGE
10
+ Usage: kraken-mobile <command-name> [parameters] [options]
11
+ <command-name> can be one of
12
+ run <apk>
13
+ runs Cucumber in the current folder with the environment needed.
14
+ version
15
+ prints the gem version.
16
+ devices
17
+ prints the list of devices attached.
18
+ setup
19
+ creates kraken-settings file specifying in what devices the tests are going to be run.
20
+ gen
21
+ generate a features folder structure.
22
+ resign <apk>
23
+ resigns the app with the currently configured keystore.
24
+ USAGE
25
+ end
26
+
27
+ def print_devices
28
+ puts 'List of devices attached'
29
+ ADB.connected_devices.each_with_index do |device, index|
30
+ puts "user#{index + 1} - #{device.to_s.split(K::SEPARATOR).join(' - ')}"
31
+ end
32
+ end
33
+
34
+ def apk_file?(file_path)
35
+ file_path.end_with?('.apk') && File.exist?(file_path)
36
+ end
37
+
38
+ def relative_to_full_path(file_path)
39
+ File.expand_path(file_path)
40
+ end
41
+
42
+ def msg(title, &block)
43
+ puts "\n" + '-' * 10 + title + '-' * 10
44
+ block.call
45
+ puts '-' * 10 + '-------' + '-' * 10 + "\n"
46
+ end
47
+
48
+ def scaffold_folder_structure
49
+ if File.exist?(@features_dir)
50
+ puts 'A features directory already exists. Stopping...'
51
+ exit 1
52
+ end
53
+
54
+ msg('Question') do
55
+ puts 'I\'m about to create a subdirectory called features.'
56
+ puts 'features will contain all your kraken tests.'
57
+ puts 'Please hit return to confirm that\'s what you want.'
58
+ end
59
+ exit 2 unless STDIN.gets.chomp == ''
60
+
61
+ FileUtils.cp_r(@source_dir, @features_dir)
62
+
63
+ msg('Info') do
64
+ puts "features subdirectory created. \n"
65
+ end
66
+ end
67
+
68
+ def ensure_apk_is_specified
69
+ return if !ARGV.empty? && apk_file?(ARGV.first)
70
+
71
+ puts 'The first parameter must be the path to a valid apk file.'
72
+ exit 1
73
+ end
74
+
75
+ def ensure_configuration_is_valid(configuration)
76
+ return if File.exist?(configuration) &&
77
+ File.file?(configuration) &&
78
+ configuration.end_with?('.json')
79
+
80
+ puts 'The path of the configuration file is not valid.'
81
+ exit 1
82
+ end
83
+
84
+ def ensure_properties_is_valid(properties)
85
+ return if File.exist?(properties) &&
86
+ File.file?(properties) &&
87
+ properties.end_with?('.json')
88
+
89
+ puts 'The path of the properties file is not valid.'
90
+ exit 1
91
+ end
@@ -0,0 +1,134 @@
1
+ require 'io/console'
2
+ require 'tty-prompt'
3
+
4
+ class KrakenSetup
5
+ WEB_IDENTIFIER = 'Web'.freeze
6
+ IS_WEB_AVAILABLE_FOR_SELECTION = false
7
+
8
+ attr_accessor :prompt
9
+ attr_accessor :devices_connected_id
10
+ attr_accessor :settings
11
+
12
+ def initialize
13
+ @prompt = TTY::Prompt.new
14
+ @available_devices_ids = available_devices_ids_for_select
15
+ @settings = {}
16
+ end
17
+
18
+ def run
19
+ number_of_devices_required.times do |index|
20
+ user_id = index + 1
21
+ selected_device_id = select_device_for_user_with_id(user_id)
22
+ if selected_device_id == WEB_IDENTIFIER
23
+ handle_web_setup_for_user_with_id(user_id)
24
+ else
25
+ handle_mobile_setup_for_user_with_id(user_id, selected_device_id)
26
+ end
27
+ end
28
+ save_user_settings_to_file
29
+ end
30
+
31
+ private
32
+
33
+ def handle_web_setup_for_user_with_id(user_id)
34
+ save_web_device_settings_for_user_with_id(user_id: user_id)
35
+ end
36
+
37
+ def handle_mobile_setup_for_user_with_id(user_id, device_id)
38
+ selected_apk = select_apk_for_user_with_id(user_id)
39
+ save_mobile_device_settings_for_user_with_id(
40
+ user_id: user_id, apk: selected_apk,
41
+ device_id: device_id
42
+ )
43
+ delete_selected_device_id_from_available_devices(device_id)
44
+ end
45
+
46
+ def save_user_settings_to_file
47
+ open('kraken_mobile_settings.json', 'w') do |f|
48
+ f.puts @settings.to_json
49
+ end
50
+
51
+ puts 'Saved your settings to .kraken_mobile_settings. You can edit '\
52
+ 'the settings manually or run this setup script again'
53
+ end
54
+
55
+ def number_of_devices_required
56
+ number_of_devices = -1
57
+ max_devices = max_number_of_devices
58
+ while number_of_devices <= 0 || number_of_devices > max_number_of_devices
59
+ number_of_devices = @prompt.ask(
60
+ 'How many devices do you want to use?', convert: :int
61
+ )
62
+
63
+ if number_of_devices.zero?
64
+ @prompt.error('Number of devices can\'t be less than 1.')
65
+ end
66
+
67
+ if number_of_devices > max_devices
68
+ @prompt.error(
69
+ "You can run Kraken only in #{max_devices} device" \
70
+ "#{max_devices > 1 ? 's' : ''}"
71
+ )
72
+ end
73
+ end
74
+ number_of_devices
75
+ end
76
+
77
+ def delete_selected_device_id_from_available_devices(device_id)
78
+ @available_devices_ids.delete(device_id)
79
+ end
80
+
81
+ def save_mobile_device_settings_for_user_with_id(
82
+ user_id:, apk:, device_id:
83
+ )
84
+ device = ADB.connected_devices.select { |d| d.id == device_id }.first
85
+ @settings[user_id] = {
86
+ id: device_id,
87
+ model: device.model,
88
+ config: {
89
+ apk_path: apk
90
+ }
91
+ }
92
+ end
93
+
94
+ def save_web_device_settings_for_user_with_id(user_id:)
95
+ device = WebDevice.factory_create
96
+ @settings[user_id] = {
97
+ id: device.id,
98
+ model: device.model,
99
+ config: {}
100
+ }
101
+ end
102
+
103
+ def select_apk_for_user_with_id(id)
104
+ valid_apk = false
105
+
106
+ until valid_apk
107
+ entered_text = @prompt.ask(
108
+ "What APK user #{id} is going to run?", required: true
109
+ )
110
+ entered_apk_path = File.expand_path(entered_text)
111
+ valid_apk = apk_file?(File.expand_path(entered_apk_path))
112
+ @prompt.error('APK path is not valid.') unless valid_apk
113
+ end
114
+ entered_apk_path
115
+ end
116
+
117
+ def select_device_for_user_with_id(id)
118
+ @prompt.select(
119
+ "Choose your user #{id}", @available_devices_ids
120
+ )
121
+ end
122
+
123
+ def available_devices_ids_for_select
124
+ devices_connected_id = ADB.connected_devices.map(&:id)
125
+ devices_connected_id << WEB_IDENTIFIER if IS_WEB_AVAILABLE_FOR_SELECTION
126
+ devices_connected_id
127
+ end
128
+
129
+ def max_number_of_devices
130
+ return 100 if IS_WEB_AVAILABLE_FOR_SELECTION
131
+
132
+ ADB.connected_devices.count
133
+ end
134
+ end