2017-02-27 13:53:48 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
from src import android, appium, log
|
|
|
|
|
|
|
|
logger = logging.getLogger('service')
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
"""
|
|
|
|
Installation of needed sdk package, creation of android emulator and execution of appium server.
|
|
|
|
|
|
|
|
"""
|
2017-03-16 15:33:26 +00:00
|
|
|
# Android SDK path
|
2017-02-27 13:53:48 +00:00
|
|
|
android_path = os.getenv('ANDROID_HOME', '/root')
|
|
|
|
logger.info('Android path: {path}'.format(path=android_path))
|
2017-03-16 15:33:26 +00:00
|
|
|
|
|
|
|
# Emulator informations
|
|
|
|
emu_type = os.getenv('EMULATOR_TYPE', android.TYPE_ARMEABI).lower()
|
|
|
|
emu_type = android.TYPE_ARMEABI if emu_type not in [android.TYPE_ARMEABI, android.TYPE_X86] else \
|
|
|
|
emu_type
|
|
|
|
logger.info('Emulator type: {type}'.format(type=emu_type))
|
|
|
|
emu_file = 'emulator64-x86' if emu_type == android.TYPE_X86 else 'emulator64-arm'
|
|
|
|
logger.info('Emulator file: {file}'.format(file=emu_file))
|
|
|
|
|
|
|
|
# Device name
|
|
|
|
device = os.getenv('DEVICE', 'Nexus 5')
|
|
|
|
|
|
|
|
# Android version
|
2017-02-27 13:53:48 +00:00
|
|
|
android_version = os.getenv('ANDROID_VERSION', '4.2.2')
|
|
|
|
logger.info('Android version: {version}'.format(version=android_version))
|
2017-03-16 15:33:26 +00:00
|
|
|
|
|
|
|
# Selenium grid connection
|
2017-02-27 13:53:48 +00:00
|
|
|
connect_to_grid = str_to_bool(str(os.getenv('CONNECT_TO_GRID', False)))
|
|
|
|
logger.info('Connect to selenium grid? {input}'.format(input=connect_to_grid))
|
|
|
|
|
2017-03-16 15:33:26 +00:00
|
|
|
# Install android sdk package
|
2017-02-27 13:53:48 +00:00
|
|
|
api_level = android.get_api_level(android_version)
|
2017-03-16 15:33:26 +00:00
|
|
|
# Bug: cannot use skin for system image x86 with android version < 5.0
|
|
|
|
if emu_type == android.TYPE_X86:
|
2017-03-07 15:24:14 +00:00
|
|
|
if int(api_level) < android.API_LEVEL_ANDROID_5:
|
|
|
|
sys_img = android.TYPE_X86
|
2017-03-16 15:33:26 +00:00
|
|
|
device = 'emulator'
|
2017-03-07 15:24:14 +00:00
|
|
|
else:
|
|
|
|
sys_img = android.TYPE_X86_64
|
|
|
|
else:
|
|
|
|
sys_img = '{type}-v7a'.format(type=android.TYPE_ARMEABI)
|
|
|
|
logger.info('System image: {sys_img}'.format(sys_img=sys_img))
|
2017-03-16 15:33:26 +00:00
|
|
|
android.install_package(android_path, emu_file, api_level, sys_img)
|
2017-03-07 15:24:14 +00:00
|
|
|
|
|
|
|
# Create android virtual device
|
2017-03-16 15:33:26 +00:00
|
|
|
logger.info('Device: {device}'.format(device=device))
|
|
|
|
avd_name = '{device}_{version}'.format(device=device.replace(' ', '_').lower(), version=android_version)
|
2017-02-27 13:53:48 +00:00
|
|
|
logger.info('AVD name: {avd}'.format(avd=avd_name))
|
2017-03-16 15:33:26 +00:00
|
|
|
android.create_avd(android_path, device, avd_name, api_level)
|
2017-02-27 13:53:48 +00:00
|
|
|
|
|
|
|
# Run appium server
|
|
|
|
appium.run(connect_to_grid, avd_name, android_version)
|
|
|
|
|
|
|
|
|
|
|
|
def str_to_bool(str):
|
|
|
|
"""
|
|
|
|
Convert string to boolean.
|
|
|
|
|
|
|
|
:param str: given string
|
|
|
|
:type str: str
|
|
|
|
:return: converted string
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
return str.lower() in ('yes', 'true', 't', '1')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
log.init()
|
|
|
|
start()
|