Class: KrakenMobile::DevicesHelper::AdbHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/kraken-mobile/helpers/devices_helper/adb_helper.rb

Instance Method Summary collapse

Instance Method Details

#adb_devices_lObject

ADB command that returns all phones and emulators connected to the computer.



8
9
10
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 8

def adb_devices_l
	`adb devices -l`
end

#connected_devicesObject

Returns an array with all the devices and emulators connected to the computer.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 49

def connected_devices
	begin
		devices = []
		list =
			adb_devices_l.split("\n").each do |line|
				line_id = extract_device_id(line)
				line_model = extract_device_model(line)
				if line_id && line_model
					device = Models::Device.new(line_id, line_model, devices.size + 1)
					devices << device
				end
			end
		devices
	rescue
		[]
	end
end

#create_file(file_name, device_id) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 87

def create_file file_name, device_id
  begin
    raise "Device #{device_id} not found" unless is_device_connected(device_id)
    create_file_in_device("#{file_name}.txt", device_id)
    true
  rescue
    false
  end
end

#create_file_in_device(file_name, device_id) ⇒ Object



20
21
22
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 20

def create_file_in_device file_name, device_id
  `adb -s #{device_id} shell "> /sdcard/#{file_name}"`
end

#delete_file(file_name, device_id) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 97

def delete_file file_name, device_id
  begin
    raise "Device #{device_id} not found" unless is_device_connected(device_id)
    delete_file_in_device("#{file_name}.txt", device_id)
    true
  rescue
    false
  end
end

#delete_file_in_device(file_name, device_id) ⇒ Object



24
25
26
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 24

def delete_file_in_device file_name, device_id
  `adb -s #{device_id} shell "rm -rf /sdcard/#{file_name}"`
end

#device_orientation(device_id) ⇒ Object



36
37
38
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 36

def device_orientation device_id
  `adb -s #{device_id} shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }'`
end

#device_screen_size(device_id) ⇒ Object



28
29
30
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 28

def device_screen_size device_id
  `adb -s #{device_id} shell wm size`
end

#device_sdk_version(device_id) ⇒ Object



32
33
34
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 32

def device_sdk_version device_id
  `adb -s #{device_id} shell getprop ro.build.version.sdk`
end

#extract_device_id(line) ⇒ Object

Parses the device id from the ADB devices command.



143
144
145
146
147
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 143

def extract_device_id line
	if line.match(/device(?!s)/)
		line.split(" ").first
	end
end

#extract_device_model(line) ⇒ Object

Parses the device model from the ADB devices command.



150
151
152
153
154
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 150

def extract_device_model line
	if line.match(/device(?!s)/)
		line.scan(/model:(.*) device/).flatten.first
	end
end

#file_content(file_name, device_id) ⇒ Object



12
13
14
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 12

def file_content file_name, device_id
  `adb -s #{device_id} shell "cat /sdcard/#{file_name} 2> /dev/null"`
end

#is_device_connected(device_id) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 40

def is_device_connected device_id
  begin
   adb_devices_l.include?(device_id)
  rescue
    false
  end
end

#orientation(device_id) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 133

def orientation device_id
  begin
    adb_orientation = device_orientation(device_id).strip!
    return adb_orientation.to_i
  rescue
    return KrakenMobile::Constants::PORTRAIT
  end
end

#read_file_content(file_name, device_id) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 67

def read_file_content file_name, device_id
  begin
    raise "Device #{device_id} not found" unless is_device_connected(device_id)
    content = file_content("#{file_name}.txt", device_id)
    content.strip
  rescue
    ""
  end
end

#screen_size(device_id) ⇒ Object

Returns height, width



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 108

def screen_size device_id
  begin
    adb_size = device_screen_size device_id
    parts = adb_size.strip!.split(" ")
    size = parts[parts.count-1]
    return 0,0 if !size.include?("x")
    size_parts = size.split("x")
    if orientation(device_id) == KrakenMobile::Constants::PORTRAIT
      return size_parts[1].to_i, size_parts[0].to_i
    else
      return size_parts[0].to_i, size_parts[1].to_i
    end
  rescue
    return 0,0
  end
end

#sdk_version(device_id) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 125

def sdk_version device_id
  begin
    return device_sdk_version device_id
  rescue
    return "N/A"
  end
end

#write_content_to_device(content, file_name, device_id) ⇒ Object



16
17
18
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 16

def write_content_to_device content, file_name, device_id
  `adb -s #{device_id} shell "echo "#{content}" > /sdcard/#{file_name}"`
end

#write_content_to_file(content, file_name, device_id) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/kraken-mobile/helpers/devices_helper/adb_helper.rb', line 77

def write_content_to_file content, file_name, device_id
  begin
    raise "Device #{device_id} not found" unless is_device_connected(device_id)
    write_content_to_device(content, "#{file_name}.txt", device_id)
    true
  rescue
    false
  end
end