diff --git a/.gitignore b/.gitignore index e83a8f06..2e419386 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ sample-Groceries master-detail* hello-world* tab-navigation* +.vscode # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/.pylintrc b/.pylintrc index 6d793a10..93ed982e 100644 --- a/.pylintrc +++ b/.pylintrc @@ -519,7 +519,7 @@ max-attributes=7 max-bool-expr=5 # Maximum number of branch for function / method body. -max-branches=12 +max-branches=20 # Maximum number of locals for function / method body. max-locals=15 diff --git a/core/utils/file_utils.py b/core/utils/file_utils.py index 7f1a9287..bb6756cb 100644 --- a/core/utils/file_utils.py +++ b/core/utils/file_utils.py @@ -107,6 +107,11 @@ def write(path, text): with open(path, 'w+') as text_file: text_file.write(text) + @staticmethod + def append(path, text): + with open(path, 'a') as text_file: + text_file.write(text) + @staticmethod def replace(path, old_string, new_string): content = File.read(path=path) @@ -160,3 +165,13 @@ def find_by_extension(folder, extension): Log.debug('File with {0} extension found: {1}'.format(extension, os.path.abspath(f))) matches.append(os.path.join(root, f)) return matches + + @staticmethod + def extract_part_of_text(text, key_word): + """ + That method will extract text from last occurance of key word + to the end of the file + """ + index = text.rfind(key_word) + text = text[index:] + return text diff --git a/core_tests/products/tns_tests.py b/core_tests/products/tns_tests.py index a0bb140c..e3a9d71b 100644 --- a/core_tests/products/tns_tests.py +++ b/core_tests/products/tns_tests.py @@ -6,9 +6,8 @@ from core.enums.os_type import OSType from core.settings import Settings from core.utils.device.device_manager import DeviceManager -from core.utils.file_utils import File -from core.utils.wait import Wait from products.nativescript.tns import Tns +from products.nativescript.tns_helpers import TnsHelpers class TnsTests(TnsTest): @@ -47,9 +46,7 @@ def test_001_tns_run_android(self): # Wait until app is build and installed. texts = ['Project successfully built', 'Successfully installed'] - for text in texts: - found = Wait.until(lambda: text in File.read(result.log_file), timeout=180, period=10) - assert found, '"{0}" not found in logs.'.format(text) + TnsHelpers.wait_for_log(result.log_file, texts) @timed(300) def test_002_tns_run_android_with_justlaunch(self): diff --git a/products/nativescript/tns_helpers.py b/products/nativescript/tns_helpers.py index f3e58691..5b247f79 100644 --- a/products/nativescript/tns_helpers.py +++ b/products/nativescript/tns_helpers.py @@ -1,8 +1,11 @@ # pylint: disable=unused-argument # TODO: Implement it! import os +import time +from core.log.log import Log from core.settings import Settings +from core.utils.file_utils import File # noinspection PyUnusedLocal @@ -24,3 +27,62 @@ def get_ipa(app_name): @staticmethod def get_app(app_name): return '' + + @staticmethod + def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=45, check_interval=3): + """ + Wait until log file contains list of string. + :param log_file: Path to log file. + :param string_list: List of strings. + :param not_existing_string_list: List of string that should not be in logs. + :param timeout: Timeout. + :param check_interval: Check interval. + """ + end_time = time.time() + timeout + all_items_found = False + not_found_list = [] + log = "" + verified_flag = '[VERIFIED]' + while time.time() < end_time: + not_found_list = [] + log = File.read(log_file) + # Extract the part of the log that hasn't been previously verified + if verified_flag in log: + log = File.extract_part_of_text(log, verified_flag) + for item in string_list: + if item in log: + Log.info("'{0}' found.".format(item)) + else: + not_found_list.append(item) + if not_found_list == []: + all_items_found = True + Log.info("Log contains: {0}".format(string_list)) + break + else: + Log.info("'{0}' NOT found. Wait...".format(not_found_list)) + time.sleep(check_interval) + if 'BUILD FAILED' in log: + Log.error('BUILD FAILED. No need to wait more time!') + break + if 'Unable to sync files' in log: + Log.error('Sync process failed. No need to wait more time!') + break + if 'errors were thrown' in log: + Log.error('Multiple errors were thrown. No need to wait more time!') + break + + # Mark that part of the log as verified by appending a flag at the end. + # The second time we read the file we will verify only the text after that flag + File.append(log_file, verified_flag) + + if all_items_found: + if not_existing_string_list is None: + pass + else: + for item in not_existing_string_list: + assert item not in log, "{0} found! It should not be in logs.\nLog:\n{1}".format(item, log) + else: + Log.debug('##### OUTPUT BEGIN #####\n') + Log.debug(log) + Log.debug('##### OUTPUT END #####\n') + assert False, "Output does not contain {0}".format(not_found_list)