Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#appium_server_start(**options) ⇒ Object



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
# File 'lib/Appium.rb', line 14

def appium_server_start(**options)
  command = 'appium'
  command << " --nodeconfig #{options[:config]}" if options.key?(:config)
  command << " -p #{options[:port]}" if options.key?(:port)
  command << " -bp #{options[:bp]}" if options.key?(:bp)
  command << " --udid #{options[:udid]}" if options.key?(:udid)
  command << " --automation-name #{options[:automationName]}" if options.key?(:automationName)
  command << " --selendroid-port #{options[:selendroidPort]}" if options.key?(:selendroidPort)
  command << " --webkit-debug-proxy-port #{options[:webkitPort]}" if options.key?(:webkitPort)

  platform = get_platform()
  if platform == :windows
    command << " --log #{options[:config_dir]}/output/#{options[:log]}" if options.key?(:log)
  else
    command << " --log #{options[:config_dir]}/output/#{options[:log]}" if options.key?(:log)
  end

  command << " --tmp /tmp/#{options[:tmp]}" if options.key?(:tmp)
  command << " --chromedriver-port #{options[:cp]}" if options.key?(:cp)
  command << " --command-timeout 180"
  puts command
  Dir.chdir('.') {
    if Gem::Platform.local.os == 'linux'
      pid = system('x-terminal-emulator -e ' + command + '&')
    elsif Gem::Platform.local.os == 'darwin'
      `osascript -e 'tell app "Terminal" to do script "#{command}"'`
      `osascript -e 'tell app "Terminal" to do script "ios_webkit_debug_proxy -c #{options[:udid]}:#{options[:webkitPort]} -d"'`
    else
      pid = system('start ' + command)
    end

    puts 'Waiting for Appium to start up...'
    sleep 5

    if pid.nil?
      puts 'Appium server did not start :('
    end
  }
end

#create_dir(name) ⇒ Object



47
48
49
# File 'lib/FileSystemHelpers.rb', line 47

def create_dir(name)
  FileUtils::mkdir_p name
end

#generate_node_config(nodeDir, file_name, udid, appium_port, ip, hubIp, platform, browser) ⇒ Object



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
# File 'lib/FileSystemHelpers.rb', line 12

def generate_node_config(nodeDir, file_name, udid, appium_port, ip, hubIp, platform, browser)
  f = File.new(nodeDir + "/node_configs/#{file_name}", "w")

  f.write( JSON.generate({ capabilities: [
                                          { udid: udid,
                                            browserName: udid,
                                            maxInstances: 1,
                                            platform: platform,
                                            deviceName: udid,
                                            applicationName: udid
                                          },

                                          { browserName: browser,
                                            maxInstances: 1,
                                            deviceName: udid,
                                            udid: udid,
                                            seleniumProtocol: 'WebDriver',
                                            platform: platform ,
                                            applicationName: udid}],

                           configuration: { cleanUpCycle: 2000,
                                            timeout: 299000,
                                            registerCycle: 5000,
                                            proxy: "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
                                            url: "http://#{ip}:#{appium_port}/wd/hub",
                                            host: ip,
                                            port: appium_port,
                                            maxSession: 1,
                                            register: true,
                                            hubPort: 4444,
                                            hubHost: hubIp } } ) )
  f.close
end

#get_android_devicesObject



1
2
3
# File 'lib/Android.rb', line 1

def get_android_devices
  ENV["DEVICES"] = JSON.generate((`adb devices`).lines.select { |line| line.match(/\tdevice$/) }.map.each_with_index { |line, index| { udid: line.split("\t")[0], thread: index + 1 } })
end

#get_device_osv(udid) ⇒ Object



5
6
7
8
# File 'lib/Android.rb', line 5

def get_device_osv udid
  command = "adb  -s #{udid} shell getprop ro.build.version.sdk"
  `#{command}`
end

#get_ios_devicesObject



1
2
3
# File 'lib/iOS.rb', line 1

def get_ios_devices
  ENV["IOS_DEVICES"] = JSON.generate((`system_profiler SPUSBDataType | sed -n -E -e '/(iPhone|iPad)/,/Serial/s/ *Serial Number: *(.+)/\\1/p'`).lines.map.each_with_index { |line, index| { udid: line.gsub(/\n/,""), thread: index + 1 } })
end

#get_platformObject



2
3
4
5
6
7
8
9
10
# File 'lib/FileSystemHelpers.rb', line 2

def get_platform()
  if Gem::Platform.local.os == 'darwin'
    return :mac
  elsif Gem::Platform.local.os == 'linux'
    return :linux
  else
    return :windows
  end
end

#launch_hub_and_nodes(ip, hubIp, nodeDir) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/Appium.rb', line 56

def launch_hub_and_nodes(ip, hubIp, nodeDir)


  if Gem::Platform.local.os == 'darwin'

    ios_devices = JSON.parse(get_ios_devices)

    ios_devices.size.times do |index|
      port = 4100 + index
      webkitPort = 27753 + index
      config_name = "#{ios_devices[index]["udid"]}.json"
      generate_node_config nodeDir, config_name, ios_devices[index]["udid"], port, ip, hubIp, 'MAC', 'safari'
      node_config = nodeDir + '/node_configs/' +"#{config_name}"
      appium_server_start config: node_config, port: port, udid: ios_devices[index]["udid"], log: "appium-#{ios_devices[index]["udid"]}.log", tmp: ios_devices[index]["udid"], webkitPort: webkitPort, config_dir: nodeDir
    end

  else

    devices = JSON.parse(get_android_devices)

    devices.size.times do |index|
      port = 4000 + index
      bp = 2250 + index
      sdp = 5000 + index
      cp = 6000 + index
      sdkv = get_device_osv(devices[index]['udid']).strip.to_i
      config_name = "#{devices[index]["udid"]}.json"
      generate_node_config nodeDir, config_name, devices[index]["udid"], port, ip, hubIp, 'android', 'chrome'
      node_config = nodeDir + '/node_configs/' +"#{config_name}"
      if sdkv === 16 || sdkv === 17
        appium_server_start config: node_config, port: port, bp: bp, udid: devices[index]["udid"], automationName: "selendroid", selendroidPort: sdp, log: "appium-#{devices[index]["udid"]}.log", tmp: devices[index]["udid"], cp: cp, config_dir: nodeDir
      else
        appium_server_start config: node_config, port: port, bp: bp, udid: devices[index]["udid"], log: "appium-#{devices[index]["udid"]}.log", tmp: devices[index]["udid"], cp: cp, config_dir: nodeDir
      end
    end
  end
end

#restart_devicesObject



10
11
12
13
14
15
16
# File 'lib/Android.rb', line 10

def restart_devices
  devices = JSON.parse(get_android_devices)

  devices.each do |device|
    `adb -s #{device['udid']} reboot`
  end
end

#shortname(long_name) ⇒ Object



7
8
9
10
11
12
# File 'lib/Appium.rb', line 7

def shortname long_name
  max_path = 1024
  short_name = " " * max_path
  lfn_size = Win32API.new("kernel32", "GetShortPathName", ['P','P','L'],'L').call(long_name, short_name, max_path)
  return short_name[0..lfn_size-1]
end