Using new usage process of new SDK

This commit is contained in:
butomo1989 2017-11-09 11:25:21 +01:00
parent d28c4dc5e0
commit 567a494ccb
3 changed files with 50 additions and 53 deletions

View file

@ -4,22 +4,24 @@ import json
import logging import logging
import os import os
import subprocess import subprocess
import errno
from src import CONFIG_FILE, ROOT, CHROME_DRIVER from src import CHROME_DRIVER, CONFIG_FILE, ROOT
from src import log from src import log
log.init() log.init()
logger = logging.getLogger('app') logger = logging.getLogger('app')
def symlink_force(target, link_name): def symlink_force(target, link_name):
try: try:
os.symlink(target, link_name) os.symlink(target, link_name)
except OSError as e: except OSError as e:
import errno
if e.errno == errno.EEXIST: if e.errno == errno.EEXIST:
os.remove(link_name) os.remove(link_name)
os.symlink(target, link_name) os.symlink(target, link_name)
def get_or_raise(env: str) -> str: def get_or_raise(env: str) -> str:
""" """
Check if needed environment variables are given. Check if needed environment variables are given.
@ -69,22 +71,9 @@ def prepare_avd(device: str, avd_name: str):
:param device: Device name :param device: Device name
:param avd_name: Name of android virtual device / emulator :param avd_name: Name of android virtual device / emulator
""" """
cmd = 'echo no | android create avd -f -n {name} -t android-{api} -b {img_type}{sys_img}'.format(
name=avd_name, api=API_LEVEL, img_type='google_apis/' if IMG_TYPE == 'google_apis' else '',
sys_img=SYS_IMG)
# Link emulator skins
skin_rsc_path = os.path.join(ROOT, 'devices', 'skins')
logger.info('Skin ressource path: {rsc}'.format(rsc=skin_rsc_path))
skin_dst_path = os.path.join(ANDROID_HOME, 'platforms', 'android-{api}'.format(api=API_LEVEL), 'skins')
logger.info('Skin destination path: {dst}'.format(dst=skin_dst_path))
for s in os.listdir(skin_rsc_path):
symlink_force(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s))
# Hardware and its skin
device_name_bash = device.replace(' ', '\ ') device_name_bash = device.replace(' ', '\ ')
skin_name = device.replace(' ', '_').lower() skin_name = device.replace(' ', '_').lower()
logger.info('Device name in bash: {db}, Skin name: {skin}'.format(db=device_name_bash, skin=skin_name))
# For custom hardware profile # For custom hardware profile
profile_dst_path = os.path.join(ROOT, '.android', 'devices.xml') profile_dst_path = os.path.join(ROOT, '.android', 'devices.xml')
@ -95,10 +84,19 @@ def prepare_avd(device: str, avd_name: str):
logger.info('Hardware profile destination path: {dst}'.format(dst=profile_dst_path)) logger.info('Hardware profile destination path: {dst}'.format(dst=profile_dst_path))
symlink_force(profile_src_path, profile_dst_path) symlink_force(profile_src_path, profile_dst_path)
# Append command avd_path = '/'.join([ANDROID_HOME, 'android_emulator'])
cmd += ' -d {device} -s {skin}'.format(device=device_name_bash, skin=skin_name) creation_cmd = 'avdmanager create avd -f -n {name} -b {img_type}/{sys_img} -k "system-images;android-{api_lvl};' \
logger.info('AVD creation command: {cmd}'.format(cmd=cmd)) '{img_type};{sys_img}" -d {device} -p {path}'.format(name=avd_name, img_type=IMG_TYPE, sys_img=SYS_IMG,
subprocess.check_call(cmd, shell=True) api_lvl=API_LEVEL, device=device_name_bash,
path=avd_path)
logger.info('Command to create avd: {command}'.format(command=creation_cmd))
subprocess.check_call(creation_cmd, shell=True)
skin_path = '/'.join([ANDROID_HOME, 'devices', 'skins', skin_name])
config_path = '/'.join([avd_path, 'config.ini'])
with open(config_path, 'a') as file:
file.write('skin.path={sp}'.format(sp=skin_path))
logger.info('Skin was added in config.ini')
def appium_run(avd_name: str): def appium_run(avd_name: str):
@ -184,8 +182,9 @@ def run():
logger.info('Preparing emulator...') logger.info('Preparing emulator...')
prepare_avd(device, avd_name) prepare_avd(device, avd_name)
logger.info('Run emulator...') logger.info('Run emulator...')
cmd = 'emulator -avd {name}'.format(name=avd_name) cmd = 'emulator -avd {name} -gpu off'.format(name=avd_name)
subprocess.Popen(cmd.split()) subprocess.Popen(cmd.split())
appium = convert_str_to_bool(str(os.getenv('APPIUM', False))) appium = convert_str_to_bool(str(os.getenv('APPIUM', False)))

View file

@ -10,22 +10,24 @@ from src import app
class TestApp(TestCase): class TestApp(TestCase):
"""Unit test class to test other methods in the app.""" """Unit test class to test other methods in the app."""
#create symlink
@classmethod @classmethod
def test_symlink_correct(self): def test_symlink(self):
os.mknod(os.path.join("./","testFile1.txt")) res = os.path.join('testFile1.txt')
app.symlink_force(os.path.join("./","testFile1.txt"),os.path.join("./","link_testFile1.txt")) dest = os.path.join('link_testFile1.txt')
os.remove(os.path.join("./","testFile1.txt")) open(res, 'a').close()
os.remove(os.path.join("./","link_testFile1.txt")) app.symlink_force(res, dest)
os.remove(res)
os.remove(dest)
#link already exist
@classmethod @classmethod
def test_symlink_already_exist(self): def test_symlink_already_exist(self):
os.mknod(os.path.join("./","testFile2.txt")) res = os.path.join('testFile1.txt')
os.mknod(os.path.join("./","link_testFile2.txt")) dest = os.path.join('link_testFile1.txt')
app.symlink_force(os.path.join("./","testFile2.txt"),os.path.join("./","link_testFile2.txt")) open(res, 'a').close()
os.remove(os.path.join("./","testFile2.txt")) open(dest, 'a').close()
os.remove(os.path.join("./","link_testFile2.txt")) app.symlink_force(res, dest)
os.remove(res)
os.remove(dest)
def test_valid_env(self): def test_valid_env(self):
key = 'ENV_1' key = 'ENV_1'

View file

@ -8,34 +8,30 @@ from src import app
@mock.patch('subprocess.check_call') @mock.patch('subprocess.check_call')
@mock.patch('os.symlink')
class TestAvd(TestCase): class TestAvd(TestCase):
"""Unit test class to test method create_avd.""" """Unit test class to test method create_avd."""
def setUp(self): def setUp(self):
self.avd_name = 'test_avd' self.avd_name = 'test_avd'
def test_nexus_avd_as_default(self, mocked_suprocess, mocked_sys_link): @mock.patch("builtins.open", create=True)
with mock.patch('os.listdir') as mocked_list_dir: def test_nexus_avd_as_default(self, mocked_suprocess, mocked_open):
mocked_list_dir.return_value = ['file1', 'file2'] self.assertFalse(mocked_suprocess.called)
self.assertFalse(mocked_list_dir.called) self.assertFalse(mocked_open.called)
self.assertFalse(mocked_sys_link.called) app.prepare_avd('Nexus 5', self.avd_name)
self.assertFalse(mocked_suprocess.called) self.assertTrue(mocked_suprocess.called)
app.prepare_avd('Nexus 5', self.avd_name) self.assertTrue(mocked_open.called)
self.assertTrue(mocked_list_dir.called)
self.assertTrue(mocked_sys_link.called)
self.assertTrue(mocked_suprocess.called)
def test_samsung_avd(self, mocked_suprocess, mocked_sys_link): @mock.patch('os.symlink')
with mock.patch('os.listdir') as mocked_list_dir: @mock.patch("builtins.open", create=True)
mocked_list_dir.return_value = ['file1', 'file2'] def test_samsung_avd(self, mocked_suprocess, mocked_sys_link, mocked_open):
self.assertFalse(mocked_list_dir.called) self.assertFalse(mocked_sys_link.called)
self.assertFalse(mocked_sys_link.called) self.assertFalse(mocked_suprocess.called)
self.assertFalse(mocked_suprocess.called) self.assertFalse(mocked_open.called)
app.prepare_avd('Samsung Galaxy S6', self.avd_name) app.prepare_avd('Samsung Galaxy S6', self.avd_name)
self.assertTrue(mocked_list_dir.called) self.assertTrue(mocked_sys_link.called)
self.assertTrue(mocked_sys_link.called) self.assertTrue(mocked_suprocess.called)
self.assertTrue(mocked_suprocess.called) self.assertTrue(mocked_open.called)
def tearDown(self): def tearDown(self):
if os.getenv('DEVICE'): if os.getenv('DEVICE'):