Skip to content

feat: wait for log method #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sample-Groceries
master-detail*
hello-world*
tab-navigation*
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions core/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
7 changes: 2 additions & 5 deletions core_tests/products/tns_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
62 changes: 62 additions & 0 deletions products/nativescript/tns_helpers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)