From 9db091e201d99dafe147e72e9bda53b8e976de74 Mon Sep 17 00:00:00 2001 From: Maria Endarova Date: Wed, 23 Jan 2019 11:05:37 +0200 Subject: [PATCH 1/4] Add wait for log method. Add methods for append and extract text. Add vscode files to gitignore --- .gitignore | 1 + core/utils/file_utils.py | 15 +++++++ core_tests/products/tns_tests.py | 5 +-- products/nativescript/tns_helpers.py | 62 ++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) 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/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..ffa02f27 100644 --- a/core_tests/products/tns_tests.py +++ b/core_tests/products/tns_tests.py @@ -9,6 +9,7 @@ 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 +48,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..6292df60 100644 --- a/products/nativescript/tns_helpers.py +++ b/products/nativescript/tns_helpers.py @@ -1,8 +1,10 @@ # pylint: disable=unused-argument # TODO: Implement it! import os +import time from core.settings import Settings +from core.utils.file_utils import File # noinspection PyUnusedLocal @@ -24,3 +26,63 @@ 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: + print "'{0}' found.".format(item) + else: + not_found_list.append(item) + if not_found_list == []: + all_items_found = True + print "Log contains: {0}".format(string_list) + break + else: + print "'{0}' NOT found. Wait...".format(not_found_list) + time.sleep(check_interval) + if 'BUILD FAILED' in log: + print 'BUILD FAILED. No need to wait more time!' + break + if 'Unable to sync files' in log: + print 'Sync process failed. No need to wait more time!' + break + if 'errors were thrown' in log: + print '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: + print "##### OUTPUT BEGIN #####\n" + print log + print "##### OUTPUT END #####\n" + print "" + assert False, "Output does not contain {0}".format(not_found_list) From 3c906789c13e0bdecadd4b9d355d1dbae779a470 Mon Sep 17 00:00:00 2001 From: endarova Date: Wed, 23 Jan 2019 12:14:17 +0200 Subject: [PATCH 2/4] fix linter errors --- core_tests/products/tns_tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/core_tests/products/tns_tests.py b/core_tests/products/tns_tests.py index ffa02f27..e3a9d71b 100644 --- a/core_tests/products/tns_tests.py +++ b/core_tests/products/tns_tests.py @@ -6,8 +6,6 @@ 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 From 63a15e9bf704cc0f1204b1c1635481776d3be4ca Mon Sep 17 00:00:00 2001 From: Maria Endarova Date: Wed, 23 Jan 2019 13:56:09 +0200 Subject: [PATCH 3/4] chore: pylint, Increase maximum number of branch for function --- .pylintrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 92097687f350e5756e9c6603a5a4ddd4ce8fbe5c Mon Sep 17 00:00:00 2001 From: Maria Endarova Date: Wed, 23 Jan 2019 14:46:30 +0200 Subject: [PATCH 4/4] Use Log class methods --- products/nativescript/tns_helpers.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/products/nativescript/tns_helpers.py b/products/nativescript/tns_helpers.py index 6292df60..5b247f79 100644 --- a/products/nativescript/tns_helpers.py +++ b/products/nativescript/tns_helpers.py @@ -3,6 +3,7 @@ import os import time +from core.log.log import Log from core.settings import Settings from core.utils.file_utils import File @@ -50,24 +51,24 @@ def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=4 log = File.extract_part_of_text(log, verified_flag) for item in string_list: if item in log: - print "'{0}' found.".format(item) + Log.info("'{0}' found.".format(item)) else: not_found_list.append(item) if not_found_list == []: all_items_found = True - print "Log contains: {0}".format(string_list) + Log.info("Log contains: {0}".format(string_list)) break else: - print "'{0}' NOT found. Wait...".format(not_found_list) + Log.info("'{0}' NOT found. Wait...".format(not_found_list)) time.sleep(check_interval) if 'BUILD FAILED' in log: - print 'BUILD FAILED. No need to wait more time!' + Log.error('BUILD FAILED. No need to wait more time!') break if 'Unable to sync files' in log: - print 'Sync process failed. No need to wait more time!' + Log.error('Sync process failed. No need to wait more time!') break if 'errors were thrown' in log: - print 'Multiple errors were thrown. No need to wait more time!' + 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. @@ -81,8 +82,7 @@ def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=4 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: - print "##### OUTPUT BEGIN #####\n" - print log - print "##### OUTPUT END #####\n" - print "" + Log.debug('##### OUTPUT BEGIN #####\n') + Log.debug(log) + Log.debug('##### OUTPUT END #####\n') assert False, "Output does not contain {0}".format(not_found_list)