Skip to content

Commit 3ec49b9

Browse files
authored
Merge pull request #53 from NativeScript/dtopuzov/refactor_device
Update device and work on verified plugin scripts
2 parents 9cc046a + fb1e5a1 commit 3ec49b9

28 files changed

+694
-410
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ nosetests.xml
2020
Payload
2121
/out/*
2222
/workspace/*
23+
verified_plugins/data/*.csv
2324
nativescript-*

core/device/adb.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import os
2+
3+
from core.osutils.command import run
4+
from core.osutils.command_log_level import CommandLogLevel
5+
from core.osutils.file import File
6+
from core.osutils.os_type import OSType
7+
from core.settings.settings import CURRENT_OS
8+
9+
ANDROID_HOME = os.environ.get('ANDROID_HOME')
10+
ADB_PATH = os.path.join(ANDROID_HOME, 'platform-tools', 'adb')
11+
12+
13+
class Adb(object):
14+
@staticmethod
15+
def __find_aapt():
16+
"""
17+
Find aapt tool under $ANDRODI_HOME/build-tools
18+
:return: Path to appt.
19+
"""
20+
aapt_executable = 'aapt'
21+
if CURRENT_OS is OSType.WINDOWS:
22+
aapt_executable += '.exe'
23+
base_path = os.path.join(ANDROID_HOME, 'build-tools')
24+
return File.find(base_path=base_path, file_name=aapt_executable, exact_match=True)
25+
26+
@staticmethod
27+
def __get_pacakge_id(apk_file):
28+
"""
29+
Get package id from apk file.
30+
:param apk_file: Path to apk file.
31+
:return: Package identifier.
32+
"""
33+
app_id = None
34+
aapt = Adb.__find_aapt()
35+
command = aapt + ' dump badging ' + apk_file
36+
output = run(command=command, log_level=CommandLogLevel.COMMAND_ONLY)
37+
for line in output.split('\n'):
38+
if 'package:' in line:
39+
app_id = line.split('\'')[1]
40+
return app_id
41+
42+
@staticmethod
43+
def run(command, device_id, timeout=60, log_level=CommandLogLevel.COMMAND_ONLY):
44+
"""
45+
Run adb command.
46+
:param command: Command to run (without adb in front).
47+
:param device_id: Device id.
48+
:param timeout: Timeout.
49+
:param log_level: Log level.
50+
:return: Output of executed command.
51+
"""
52+
return run(ADB_PATH + ' -s ' + device_id + ' ' + command, timeout=timeout, log_level=log_level)
53+
54+
@staticmethod
55+
def uninstall_all_apps(device_id):
56+
"""
57+
Uninstall all 3rd party applications.
58+
:param device_id: Device id.
59+
"""
60+
apps = Adb.run(command='shell pm list packages -3', device_id=device_id)
61+
for line in apps:
62+
if 'package:' in line:
63+
app = line.replace('package:', '')
64+
Adb.uninstall(app_id=app, device_id=device_id)
65+
66+
@staticmethod
67+
def install(apk_file, device_id):
68+
"""
69+
Install application.
70+
:param apk_file: Application under test.
71+
:param device_id: Device id.
72+
"""
73+
output = Adb.run(command='install -r ' + apk_file, device_id=device_id)
74+
assert 'Success' in output, 'Failed to install {0}. \n Log: \n {1}'.format(apk_file, output)
75+
print '{0} installed successfully on {1}'.format(apk_file, device_id)
76+
77+
@staticmethod
78+
def uninstall(app_id, device_id):
79+
"""
80+
Uninstall application.
81+
:param app_id: Package identifier (for example org.nativescript.testapp).
82+
:param device_id: Device id.
83+
"""
84+
output = Adb.run(command='shell pm uninstall ' + app_id, device_id=device_id)
85+
assert 'Success' in output, 'Failed to uninstall {0}. \n Log: \n {1}'.format(app_id, output)
86+
87+
@staticmethod
88+
def monkey(apk_file, device_id):
89+
"""
90+
Perform monkey testing.
91+
:param apk_file: Application under test.
92+
:param device_id: Device id.
93+
"""
94+
Adb.__monkey_kill(device_id)
95+
app_id = Adb.__get_pacakge_id(apk_file)
96+
print 'Start monkey testing...'
97+
output = Adb.run(command='shell monkey -p ' + app_id + ' --throttle 100 -v 100 -s 120', device_id=device_id)
98+
assert 'No activities found' not in output, '{0} is not available on {1}'.format(app_id, device_id)
99+
assert 'Monkey aborted due to error' not in output, '{0} crashed! \n Log: \n {1}'.format(app_id, output)
100+
assert 'Monkey finished' in output, 'Unknown error occurred! \n Log: \n {0}'.format(output)
101+
print 'Monkey test passed!'
102+
103+
@staticmethod
104+
def __monkey_kill(device_id):
105+
"""
106+
Kill running adb monkey instances.
107+
:param device_id: device id.
108+
"""
109+
kill_command = "shell ps | awk '/com\.android\.commands\.monkey/ { system(\"adb shell kill \" $2) }'"
110+
Adb.run(command=kill_command, device_id=device_id, log_level=CommandLogLevel.SILENT)

core/device/device.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from PIL import Image
88

9+
from core.device.adb import Adb, ADB_PATH
910
from core.device.device_type import DeviceType
1011
from core.device.emulator import Emulator
1112
from core.osutils.command import run
@@ -14,8 +15,6 @@
1415
from core.osutils.folder import Folder
1516
from core.settings.settings import TNS_PATH
1617

17-
ADB_PATH = os.path.join(os.environ.get('ANDROID_HOME'), 'platform-tools', 'adb')
18-
1918

2019
class Device(object):
2120
@staticmethod
@@ -28,33 +27,25 @@ def __get_screen(device_type, device_id, file_name):
2827
File.remove(file_path)
2928
if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
3029
# Cleanup sdcard
31-
output = run(command="{0} -s {1} shell rm /sdcard/*.png".format(ADB_PATH, device_id, file_name),
32-
log_level=CommandLogLevel.FULL)
30+
output = Adb.run(command="shell rm /sdcard/*.png", device_id=device_id)
3331
if "Read-only file system" in output:
3432
Emulator.unlock_sdcard()
35-
output = run(command="{0} -s {1} shell rm /sdcard/*.png".format(ADB_PATH, device_id, file_name),
36-
log_level=CommandLogLevel.FULL)
33+
output = Adb.run(command="shell rm /sdcard/*.png", device_id=device_id)
3734
assert "error" not in output.lower(), "Screencap failed with: " + output
3835

3936
# Get current screen of mobile device
40-
output = run(command="{0} -s {1} shell screencap -p /sdcard/{2}.png".format(ADB_PATH, device_id, file_name),
41-
log_level=CommandLogLevel.FULL)
37+
output = Adb.run(command="shell screencap -p /sdcard/{0}.png".format(file_name), device_id=device_id)
4238
if "Read-only file system" in output:
4339
Emulator.unlock_sdcard()
44-
output = run(
45-
command="{0} -s {1} shell screencap -p /sdcard/{2}.png".format(ADB_PATH, device_id, file_name),
46-
log_level=CommandLogLevel.FULL)
40+
output = Adb.run(command="shell screencap -p /sdcard/{0}.png".format(file_name), device_id=device_id)
4741
assert "error" not in output.lower(), "Screencap failed with: " + output
4842

4943
# Transfer image from device to localhost
50-
output = run(
51-
command="{0} -s {1} pull /sdcard/{2}.png {3}".format(ADB_PATH, device_id, file_name, file_path),
52-
log_level=CommandLogLevel.FULL)
44+
output = Adb.run(command="pull /sdcard/{0}.png {1}".format(file_name, file_path), device_id=device_id)
5345
assert "100%" in output, "Failed to get {0}. Log: {1}".format(file_name, output)
5446

5547
# Cleanup sdcard
56-
run(command="{0} -s {1} shell rm /sdcard/{2}.png".format(ADB_PATH, device_id, file_name),
57-
log_level=CommandLogLevel.SILENT)
48+
Adb.run(command="shell rm /sdcard/{0}.png".format(file_name), device_id=device_id)
5849

5950
if device_type == DeviceType.SIMULATOR:
6051
run(command="xcrun simctl io booted screenshot {0}".format(file_path),

core/device/devide_type.py

-11
This file was deleted.

0 commit comments

Comments
 (0)