First commit

This commit is contained in:
butomo1989
2016-12-22 14:29:57 +01:00
commit a5e009607e
18 changed files with 419 additions and 0 deletions
View File
+110
View File
@@ -0,0 +1,110 @@
import logging
import os
import re
import subprocess
logging.basicConfig()
logger = logging.getLogger('android_appium')
def run():
"""
Run Android emulator and Appium server.
"""
android_version = os.getenv('ANDROID_VERSION', '4.2.2')
create_android_emulator(android_version)
emulator_name = 'emulator_{version}'.format(version=android_version)
logger.info('android emulator name: {name} '.format(name=emulator_name))
# TODO: check android emulator is ready to use
cmd_run = 'emulator -avd {name} -no-audio -no-window & appium'.format(name=emulator_name)
subprocess.check_call(cmd_run, shell=True)
def get_available_sdk_packages():
"""
Get list of available sdk packages.
:return: List of available packages.
:rtype: bytearray
"""
logger.info('List of Android SDK: ')
cmd = ['android', 'list', 'sdk']
output_str = subprocess.check_output(cmd)
logger.info(output_str)
return [output.strip() for output in output_str.split('\n')] if output_str else None
def get_item_position(keyword, items):
"""
Get position of item in array by given keyword.
:return: Item position.
:rtype: int
"""
pos = 0
for p, v in enumerate(items):
if keyword in v:
pos = p
break # Get the first item that match with keyword
return pos
def create_android_emulator(android_version):
"""
Create android emulator based on given android version.
It include installation of sdk package and its armeabi v7a.
To see list of available targets: android list targets
To see list to avd: android list avd
:param android_version: android version
:type android_version: str
"""
try:
packages = get_available_sdk_packages()
if packages:
item_pos = get_item_position(android_version, packages)
logger.info('item position: {pos}'.format(pos=item_pos))
item = packages[item_pos]
item_info = item.split('-')
package_number = item_info[0]
api_version = re.search('%s(.*)%s' % ('API', ','), item_info[1]).group(1).strip()
logger.info(
'Package number: {number}, API version: {version}'.format(number=package_number, version=api_version))
# Install SDK package
logger.info('Installing SDK package...')
cmd_sdk = 'echo y | android update sdk --no-ui --filter {number}'.format(number=package_number)
subprocess.check_call(cmd_sdk, shell=True)
logger.info('Installation completed')
# Install armeabi v7a
logger.info('Installing its armeabi...')
cmd_arm = 'echo y | android update sdk --no-ui -a --filter sys-img-armeabi-v7a-android-{api}'.format(
api=api_version)
subprocess.check_call(cmd_arm, shell=True)
logger.info('Installation completed')
# Create android emulator
logger.info('Creating android emulator...')
cmd_emu = 'echo no | android create avd -f -n emulator_{version} -t android-{api} --abi armeabi-v7a'.format(
version=android_version, api=api_version)
subprocess.check_call(cmd_emu, shell=True)
logger.info('Android emulator is created')
else:
raise RuntimeError('Packages is empty!')
except IndexError as i_err:
logger.error(i_err)
if __name__ == '__main__':
logger.setLevel(logging.INFO)
run()
+19
View File
@@ -0,0 +1,19 @@
"""Unit test for start.py."""
from unittest import TestCase
import mock
from service import start
class TestService(TestCase):
"""Unit test class to test method run."""
@mock.patch('service.start.create_android_emulator')
@mock.patch('subprocess.check_call')
def test_service(self, mocked_creation, mocked_subprocess):
self.assertFalse(mocked_creation.called)
self.assertFalse(mocked_subprocess.called)
start.run()
self.assertTrue(mocked_creation.called)
self.assertTrue(mocked_subprocess.called)
+34
View File
@@ -0,0 +1,34 @@
"""Unit test for start.py."""
from unittest import TestCase
import mock
from service import start
@mock.patch('service.start.get_available_sdk_packages')
class TestRunService(TestCase):
"""Unit test class to test method create_android_emulator."""
def test_create_emulator(self, mocked_packages):
mocked_packages.return_value = ['9- SDK Platform Android 4.4.2, API 19, revision 4',
'10- SDK Platform Android 4.3.1, API 18, revision 3',
'11- SDK Platform Android 4.2.2, API 17, revision 3']
with mock.patch('subprocess.check_call') as mocked_subprocess:
self.assertFalse(mocked_subprocess.called)
android_version = '4.2.2'
start.create_android_emulator(android_version)
self.assertTrue(mocked_subprocess.called)
def test_empty_packages(self, mocked_packages):
mocked_packages.return_value = None
with self.assertRaises(RuntimeError):
start.create_android_emulator('4.2.2')
def test_index_error(self, mocked_packages):
mocked_packages.return_value = ['9 SDK Platform Android 4.4.2, API 19, revision 4',
'10 SDK Platform Android 4.3.1, API 18, revision 3',
'11 SDK Platform Android 4.2.2, API 17, revision 3']
android_version = '4.2.2'
start.create_android_emulator(android_version)
self.assertRaises(IndexError)
+28
View File
@@ -0,0 +1,28 @@
"""Unit test for start.py."""
from unittest import TestCase
import mock
from service import start
class TestAvailablePackages(TestCase):
"""Unit test class to test method get_available_sdk_packages."""
@mock.patch('subprocess.check_output')
def test_valid_output(self, mocked_output):
mocked_output.return_value = 'package 1 \n package 2'
output = start.get_available_sdk_packages()
self.assertEqual(['package 1', 'package 2'], output)
@mock.patch('subprocess.check_output')
def test_without_line_break(self, mocked_output):
mocked_output.return_value = 'package 1, package 2'
output = start.get_available_sdk_packages()
self.assertEqual(['package 1, package 2'], output)
@mock.patch('subprocess.check_output')
def test_empty_string(self, mocked_output):
mocked_output.return_value = None
output = start.get_available_sdk_packages()
self.assertEqual(None, output)
+27
View File
@@ -0,0 +1,27 @@
"""Unit test for start.py."""
from unittest import TestCase
from service import start
class TestItemPosition(TestCase):
"""Unit test class to test method get_item_position."""
def setUp(self):
self.items = ['android 4.1', 'android 4.2.2', 'android 4.3', 'android 4.4', 'android 4.4.2']
def test_valid_params(self):
keyword = '4.2'
output = start.get_item_position(keyword, self.items)
self.assertEqual(1, output)
def test_invalid_keyword(self):
keyword = 'fake'
output = start.get_item_position(keyword, self.items)
self.assertEqual(0, output)
def test_empty_array(self):
items = []
keyword = '4.2'
output = start.get_item_position(keyword, items)
self.assertEqual(0, output)