2018-11-07 12:26:32 +00:00
|
|
|
import asyncio
|
2018-08-15 12:51:52 +00:00
|
|
|
import logging
|
2017-10-05 19:41:17 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
2018-11-07 12:26:32 +00:00
|
|
|
import sys
|
|
|
|
from abc import ABCMeta, abstractmethod
|
2018-01-26 11:07:09 +00:00
|
|
|
from os import environ
|
2018-11-07 12:26:32 +00:00
|
|
|
|
|
|
|
import pytest
|
2018-01-26 11:07:09 +00:00
|
|
|
from appium import webdriver
|
2018-08-10 10:09:19 +00:00
|
|
|
from appium.webdriver.common.mobileby import MobileBy
|
|
|
|
from selenium.common.exceptions import NoSuchElementException
|
2018-11-07 12:26:32 +00:00
|
|
|
from selenium.common.exceptions import WebDriverException
|
|
|
|
|
|
|
|
from support.api.network_api import NetworkApi
|
2018-08-10 10:09:19 +00:00
|
|
|
from support.github_report import GithubHtmlReport
|
2019-11-29 11:48:07 +00:00
|
|
|
from tests import test_suite_data, start_threads, appium_container, pytest_config_global
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
class AbstractTestCase:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sauce_username(self):
|
|
|
|
return environ.get('SAUCE_USERNAME')
|
|
|
|
|
2018-01-26 11:07:09 +00:00
|
|
|
@property
|
|
|
|
def sauce_access_key(self):
|
|
|
|
return environ.get('SAUCE_ACCESS_KEY')
|
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
@property
|
|
|
|
def executor_sauce_lab(self):
|
|
|
|
return 'http://%s:%s@ondemand.saucelabs.com:80/wd/hub' % (self.sauce_username, self.sauce_access_key)
|
|
|
|
|
|
|
|
@property
|
2017-10-05 19:41:17 +00:00
|
|
|
def executor_local(self):
|
|
|
|
return 'http://localhost:4723/wd/hub'
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
def print_sauce_lab_info(self, driver):
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print("SauceOnDemandSessionID=%s job-name=%s" % (driver.session_id,
|
2019-11-29 11:48:07 +00:00
|
|
|
pytest_config_global['build']))
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
def add_local_devices_to_capabilities(self):
|
|
|
|
updated_capabilities = list()
|
|
|
|
raw_out = re.split(r'[\r\\n]+', str(subprocess.check_output(['adb', 'devices'])).rstrip())
|
|
|
|
for line in raw_out[1:]:
|
2018-04-26 06:22:11 +00:00
|
|
|
serial = re.findall(r"(([\d.\d:]*\d+)|\bemulator-\d+)", line)
|
2017-10-05 19:41:17 +00:00
|
|
|
if serial:
|
|
|
|
capabilities = self.capabilities_local
|
2018-04-26 06:22:11 +00:00
|
|
|
capabilities['udid'] = serial[0][0]
|
2017-10-05 19:41:17 +00:00
|
|
|
updated_capabilities.append(capabilities)
|
|
|
|
return updated_capabilities
|
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
@property
|
2017-10-05 19:41:17 +00:00
|
|
|
def capabilities_sauce_lab(self):
|
|
|
|
desired_caps = dict()
|
2018-02-26 23:38:56 +00:00
|
|
|
desired_caps['app'] = 'sauce-storage:' + test_suite_data.apk_name
|
2017-11-13 10:49:45 +00:00
|
|
|
|
2019-11-29 11:48:07 +00:00
|
|
|
desired_caps['build'] = pytest_config_global['build']
|
2018-02-26 23:38:56 +00:00
|
|
|
desired_caps['name'] = test_suite_data.current_test.name
|
2017-10-05 19:41:17 +00:00
|
|
|
desired_caps['platformName'] = 'Android'
|
2020-10-14 12:09:01 +00:00
|
|
|
desired_caps['appiumVersion'] = '1.18.1'
|
2020-03-17 17:27:10 +00:00
|
|
|
desired_caps['platformVersion'] = '10.0'
|
2017-10-05 19:41:17 +00:00
|
|
|
desired_caps['deviceName'] = 'Android GoogleAPI Emulator'
|
|
|
|
desired_caps['deviceOrientation'] = "portrait"
|
|
|
|
desired_caps['commandTimeout'] = 600
|
|
|
|
desired_caps['idleTimeout'] = 1000
|
2018-02-14 13:48:18 +00:00
|
|
|
desired_caps['unicodeKeyboard'] = True
|
2018-03-28 10:21:39 +00:00
|
|
|
desired_caps['automationName'] = 'UiAutomator2'
|
|
|
|
desired_caps['setWebContentDebuggingEnabled'] = True
|
|
|
|
desired_caps['ignoreUnimportantViews'] = False
|
2018-06-14 10:57:04 +00:00
|
|
|
desired_caps['enableNotificationListener'] = True
|
2019-01-15 14:53:05 +00:00
|
|
|
desired_caps['maxDuration'] = 1800
|
2017-10-05 19:41:17 +00:00
|
|
|
return desired_caps
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2018-03-31 20:05:11 +00:00
|
|
|
def update_capabilities_sauce_lab(self, new_capabilities: dict):
|
2018-05-16 19:59:36 +00:00
|
|
|
caps = self.capabilities_sauce_lab.copy()
|
2018-03-31 20:05:11 +00:00
|
|
|
caps.update(new_capabilities)
|
2018-05-16 19:59:36 +00:00
|
|
|
return caps
|
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
@property
|
|
|
|
def capabilities_local(self):
|
|
|
|
desired_caps = dict()
|
2019-11-29 11:48:07 +00:00
|
|
|
if pytest_config_global['docker']:
|
2018-11-07 12:26:32 +00:00
|
|
|
# apk is in shared volume directory
|
2019-11-29 11:48:07 +00:00
|
|
|
apk = '/root/shared_volume/%s' % pytest_config_global['apk']
|
2018-11-07 12:26:32 +00:00
|
|
|
else:
|
2019-11-29 11:48:07 +00:00
|
|
|
apk = pytest_config_global['apk']
|
2018-11-07 12:26:32 +00:00
|
|
|
desired_caps['app'] = apk
|
2017-09-21 17:01:04 +00:00
|
|
|
desired_caps['deviceName'] = 'nexus_5'
|
2017-08-28 10:02:20 +00:00
|
|
|
desired_caps['platformName'] = 'Android'
|
2018-11-30 11:42:48 +00:00
|
|
|
desired_caps['appiumVersion'] = '1.9.1'
|
2019-11-29 11:48:07 +00:00
|
|
|
desired_caps['platformVersion'] = pytest_config_global['platform_version']
|
2017-10-11 20:10:57 +00:00
|
|
|
desired_caps['newCommandTimeout'] = 600
|
2018-02-19 11:51:53 +00:00
|
|
|
desired_caps['fullReset'] = False
|
2018-02-14 13:48:18 +00:00
|
|
|
desired_caps['unicodeKeyboard'] = True
|
2018-03-28 10:21:39 +00:00
|
|
|
desired_caps['automationName'] = 'UiAutomator2'
|
|
|
|
desired_caps['setWebContentDebuggingEnabled'] = True
|
2017-08-28 10:02:20 +00:00
|
|
|
return desired_caps
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def setup_method(self, method):
|
|
|
|
raise NotImplementedError('Should be overridden from a child class')
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def teardown_method(self, method):
|
|
|
|
raise NotImplementedError('Should be overridden from a child class')
|
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
@property
|
|
|
|
def environment(self):
|
2019-11-29 11:48:07 +00:00
|
|
|
return pytest_config_global['env']
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2018-01-03 09:34:40 +00:00
|
|
|
@property
|
|
|
|
def implicitly_wait(self):
|
2018-09-14 10:26:01 +00:00
|
|
|
return 5
|
2018-01-03 09:34:40 +00:00
|
|
|
|
2018-05-25 17:29:07 +00:00
|
|
|
network_api = NetworkApi()
|
2018-10-26 13:32:05 +00:00
|
|
|
github_report = GithubHtmlReport()
|
2018-06-29 17:27:30 +00:00
|
|
|
|
2018-08-10 10:09:19 +00:00
|
|
|
def is_alert_present(self, driver):
|
|
|
|
try:
|
|
|
|
return driver.find_element(MobileBy.ID, 'android:id/message')
|
|
|
|
except NoSuchElementException:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_alert_text(self, driver):
|
|
|
|
return driver.find_element(MobileBy.ID, 'android:id/message').text
|
|
|
|
|
|
|
|
def add_alert_text_to_report(self, driver):
|
|
|
|
if self.is_alert_present(driver):
|
2019-06-03 19:44:06 +00:00
|
|
|
test_suite_data.current_test.testruns[-1].error += "; also Unexpected Alert is shown: '%s'" \
|
2018-08-10 10:09:19 +00:00
|
|
|
% self.get_alert_text(driver)
|
|
|
|
|
2018-01-26 11:07:09 +00:00
|
|
|
|
2018-08-15 12:51:52 +00:00
|
|
|
class Driver(webdriver.Remote):
|
|
|
|
|
|
|
|
@property
|
|
|
|
def number(self):
|
|
|
|
return test_suite_data.current_test.testruns[-1].jobs[self.session_id]
|
|
|
|
|
|
|
|
def info(self, text: str):
|
|
|
|
if "Base" not in text:
|
|
|
|
text = 'Device %s: %s' % (self.number, text)
|
|
|
|
logging.info(text)
|
|
|
|
test_suite_data.current_test.testruns[-1].steps.append(text)
|
|
|
|
|
|
|
|
def fail(self, text: str):
|
|
|
|
pytest.fail('Device %s: %s' % (self.number, text))
|
|
|
|
|
|
|
|
|
2019-10-29 08:25:57 +00:00
|
|
|
class Errors(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.errors = list()
|
|
|
|
|
|
|
|
def append(self, text=str()):
|
|
|
|
self.errors.append(text)
|
|
|
|
|
|
|
|
def verify_no_errors(self):
|
|
|
|
if self.errors:
|
|
|
|
pytest.fail('\n '.join([self.errors.pop(0) for _ in range(len(self.errors))]))
|
|
|
|
|
|
|
|
|
2018-01-26 11:07:09 +00:00
|
|
|
class SingleDeviceTestCase(AbstractTestCase):
|
|
|
|
|
2019-01-15 14:53:05 +00:00
|
|
|
def setup_method(self, method, **kwargs):
|
2019-11-29 11:48:07 +00:00
|
|
|
if pytest_config_global['docker']:
|
|
|
|
appium_container.start_appium_container(pytest_config_global['docker_shared_volume'])
|
|
|
|
appium_container.connect_device(pytest_config_global['device_ip'])
|
2018-09-03 21:18:18 +00:00
|
|
|
|
|
|
|
(executor, capabilities) = (self.executor_sauce_lab, self.capabilities_sauce_lab) if \
|
|
|
|
self.environment == 'sauce' else (self.executor_local, self.capabilities_local)
|
2019-01-15 14:53:05 +00:00
|
|
|
for key, value in kwargs.items():
|
|
|
|
capabilities[key] = value
|
2018-09-03 21:18:18 +00:00
|
|
|
self.driver = Driver(executor, capabilities)
|
2018-08-15 12:51:52 +00:00
|
|
|
test_suite_data.current_test.testruns[-1].jobs[self.driver.session_id] = 1
|
2018-08-10 10:09:19 +00:00
|
|
|
self.driver.implicitly_wait(self.implicitly_wait)
|
2019-10-29 08:25:57 +00:00
|
|
|
self.errors = Errors()
|
2018-01-26 11:07:09 +00:00
|
|
|
|
2019-11-29 11:48:07 +00:00
|
|
|
if pytest_config_global['docker']:
|
2018-11-07 12:26:32 +00:00
|
|
|
appium_container.reset_battery_stats()
|
|
|
|
|
2018-01-26 11:07:09 +00:00
|
|
|
def teardown_method(self, method):
|
|
|
|
if self.environment == 'sauce':
|
|
|
|
self.print_sauce_lab_info(self.driver)
|
|
|
|
try:
|
2018-08-10 10:09:19 +00:00
|
|
|
self.add_alert_text_to_report(self.driver)
|
2018-01-26 11:07:09 +00:00
|
|
|
self.driver.quit()
|
2019-11-29 11:48:07 +00:00
|
|
|
if pytest_config_global['docker']:
|
2018-11-07 12:26:32 +00:00
|
|
|
appium_container.stop_container()
|
2018-02-19 11:51:53 +00:00
|
|
|
except (WebDriverException, AttributeError):
|
2018-01-26 11:07:09 +00:00
|
|
|
pass
|
2018-08-10 10:09:19 +00:00
|
|
|
finally:
|
|
|
|
self.github_report.save_test(test_suite_data.current_test)
|
2018-01-26 11:07:09 +00:00
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
|
2018-01-03 09:34:40 +00:00
|
|
|
class LocalMultipleDeviceTestCase(AbstractTestCase):
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
def setup_method(self, method):
|
2018-01-26 11:07:09 +00:00
|
|
|
self.drivers = dict()
|
2019-10-29 08:25:57 +00:00
|
|
|
self.errors = Errors()
|
2018-01-26 11:07:09 +00:00
|
|
|
|
|
|
|
def create_drivers(self, quantity):
|
2017-10-05 19:41:17 +00:00
|
|
|
capabilities = self.add_local_devices_to_capabilities()
|
2018-01-26 11:07:09 +00:00
|
|
|
for driver in range(quantity):
|
2018-08-15 12:51:52 +00:00
|
|
|
self.drivers[driver] = Driver(self.executor_local, capabilities[driver])
|
|
|
|
test_suite_data.current_test.testruns[-1].jobs[self.drivers[driver].session_id] = driver + 1
|
2018-01-26 11:07:09 +00:00
|
|
|
self.drivers[driver].implicitly_wait(self.implicitly_wait)
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
2018-01-26 11:07:09 +00:00
|
|
|
for driver in self.drivers:
|
2017-10-05 19:41:17 +00:00
|
|
|
try:
|
2018-08-10 10:09:19 +00:00
|
|
|
self.add_alert_text_to_report(driver)
|
2018-01-26 11:07:09 +00:00
|
|
|
self.drivers[driver].quit()
|
2017-10-05 19:41:17 +00:00
|
|
|
except WebDriverException:
|
|
|
|
pass
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
|
2018-01-03 09:34:40 +00:00
|
|
|
class SauceMultipleDeviceTestCase(AbstractTestCase):
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-09-13 14:34:42 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
2018-02-09 15:16:07 +00:00
|
|
|
cls.loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(cls.loop)
|
2017-09-13 14:34:42 +00:00
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
def setup_method(self, method):
|
2018-01-26 11:07:09 +00:00
|
|
|
self.drivers = dict()
|
2019-10-29 08:25:57 +00:00
|
|
|
self.errors = Errors()
|
2018-01-26 11:07:09 +00:00
|
|
|
|
2019-07-29 00:35:36 +00:00
|
|
|
def create_drivers(self, quantity=2, max_duration=1800, custom_implicitly_wait=None):
|
2018-03-31 20:05:11 +00:00
|
|
|
capabilities = {'maxDuration': max_duration}
|
|
|
|
self.drivers = self.loop.run_until_complete(start_threads(quantity,
|
2018-08-15 12:51:52 +00:00
|
|
|
Driver,
|
2018-03-31 20:05:11 +00:00
|
|
|
self.drivers,
|
|
|
|
self.executor_sauce_lab,
|
|
|
|
self.update_capabilities_sauce_lab(capabilities)))
|
2018-01-26 11:07:09 +00:00
|
|
|
for driver in range(quantity):
|
2018-08-15 12:51:52 +00:00
|
|
|
test_suite_data.current_test.testruns[-1].jobs[self.drivers[driver].session_id] = driver + 1
|
2018-03-31 20:05:11 +00:00
|
|
|
self.drivers[driver].implicitly_wait(
|
|
|
|
custom_implicitly_wait if custom_implicitly_wait else self.implicitly_wait)
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
2018-01-26 11:07:09 +00:00
|
|
|
for driver in self.drivers:
|
2017-10-05 19:41:17 +00:00
|
|
|
try:
|
2018-02-19 11:51:53 +00:00
|
|
|
self.print_sauce_lab_info(self.drivers[driver])
|
2018-08-10 10:09:19 +00:00
|
|
|
self.add_alert_text_to_report(self.drivers[driver])
|
2018-01-26 11:07:09 +00:00
|
|
|
self.drivers[driver].quit()
|
2018-02-19 11:51:53 +00:00
|
|
|
except (WebDriverException, AttributeError):
|
2017-10-05 19:41:17 +00:00
|
|
|
pass
|
2018-08-10 10:09:19 +00:00
|
|
|
finally:
|
|
|
|
self.github_report.save_test(test_suite_data.current_test)
|
2017-09-13 14:34:42 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardown_class(cls):
|
|
|
|
cls.loop.close()
|
2017-10-05 19:41:17 +00:00
|
|
|
|
|
|
|
|
2019-11-29 11:48:07 +00:00
|
|
|
if pytest_config_global['env'] == 'local':
|
2019-10-21 07:15:34 +00:00
|
|
|
MultipleDeviceTestCase = LocalMultipleDeviceTestCase
|
|
|
|
else:
|
|
|
|
MultipleDeviceTestCase = SauceMultipleDeviceTestCase
|
2019-08-07 21:37:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NoDeviceTestCase(AbstractTestCase):
|
|
|
|
|
|
|
|
def setup_method(self, method, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
self.github_report.save_test(test_suite_data.current_test)
|