def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=60, 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) string_list = not_found_list if not not_found_list: all_items_found = True Log.info("All items found") break else: Log.debug("'{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 if Settings.HOST_OS != OSType.WINDOWS: 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.info("NOT FOUND: {0}".format(not_found_list)) Log.info('##### ACTUAL LOG #####\n') Log.info(log) Log.info('######################\n') assert False, "Output does not contain {0}".format(not_found_list)
def test_115_tns_run_android_add_remove_files_and_folders(self): """ Add/delete files and folders should be synced properly """ # Run app and verify on device result = run_hello_world_js_ts(self.app_name, Platform.ANDROID, self.emu) # Add new file # To verify that file is synced on device we have to refer some function # from it and verify it is executed. We will use console.log app_folder = os.path.join(self.source_project_dir, 'app') new_file = os.path.join(app_folder, 'test.js') renamed_file = os.path.join(app_folder, 'test_2.js') app_js_file = os.path.join(app_folder, 'app.js') File.write(new_file, "console.log('test.js synced!!!');") File.append(app_js_file, "require('./test.js');") strings = ["JS: test.js synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Rename file os.rename(new_file, renamed_file) File.replace(renamed_file, 'test.js', 'renamed file') time.sleep(1) File.replace(app_js_file, 'test.js', 'test_2.js') strings = ["JS: renamed file synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Delete file File.delete(renamed_file) strings = ["Module build failed", "Error: ENOENT: no such file or directory", 'Successfully synced application'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.emu.wait_for_text(text='Exception') File.replace(app_js_file, "require('./test_2.js');", ' ') strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, device=self.emu, run_type=RunType.UNKNOWN) not_existing_strings = ['12345'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, not_existing_string_list=not_existing_strings) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Add folder folder_name = os.path.join(app_folder, 'test_folder') new_file = os.path.join(folder_name, 'test_in_folder.js') Folder.create(folder_name) File.write(new_file, "console.log('test_in_folder.js synced!!!');") File.append(app_js_file, "require('./test_folder/test_in_folder.js');") strings = ["JS: test_in_folder.js synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Delete folder Folder.clean(folder_name) strings = ["Module build failed", "Error: ENOENT: no such file or directory"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.emu.wait_for_text(text='Exception')
def test_210_tns_preview_on_simulator_and_emulator_livesync(self): """ Preview app on simulator and emulator. Verify livesync. """ # Preview on emulator result = Tns.preview(app_name=self.app_name) # Read the log and extract the url to load the app on emulator log = File.read(result.log_file) url = Preview.get_url(log) Preview.run_url(url=url, device=self.emu) strings = TnsLogs.preview_initial_messages(platform=Platform.ANDROID) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Click on TAP button on emulator Adb.click_element_by_text(self.emu.id, 'TAP', case_sensitive=True) # Preview on simulator Preview.run_url(url=url, device=self.sim) strings = TnsLogs.preview_initial_messages(platform=Platform.IOS) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Verify emulator is not refreshed, state of app is preserved self.emu.wait_for_text(text='41 taps left', timeout=30) # Edit JS file and verify changes are applied on both emulators Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.JS) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.new_text) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.new_text) # Check changes are not synced more than once per platform # Extract the last part of the log log = File.read(result.log_file) log = File.extract_part_of_text(log, '[VERIFIED]') # Verify files are synced once TnsAssert.file_is_synced_once(log, Platform.ANDROID, 'main-view-model.js') TnsAssert.file_is_synced_once(log, Platform.IOS, 'main-view-model.js') # Mark that part of the log as verified before next sync File.append(result.log_file, '[VERIFIED]') # Edit XML file and verify changes are applied on both emulators Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.XML) self.emu.wait_for_text(text=Changes.JSHelloWord.XML.new_text) self.sim.wait_for_text(text=Changes.JSHelloWord.XML.new_text) # Check changes are not synced more than once per platform # Extract the last part of the log log = File.read(result.log_file) log = File.extract_part_of_text(log, '[VERIFIED]') # Verify files are synced once TnsAssert.file_is_synced_once(log, Platform.ANDROID, 'main-page.xml') TnsAssert.file_is_synced_once(log, Platform.IOS, 'main-page.xml')
def test_115_tns_run_ios_add_remove_files_and_folders(self): """ Add/delete files and folders should be synced properly """ # Run app and verify on device result = run_hello_world_js_ts(self.app_name, Platform.IOS, self.sim) # Add new file # To verify that file is synced on device we have to refer some function # from it and verify it is executed. We will use console.log app_folder = os.path.join(self.source_project_dir, 'app') new_file = os.path.join(app_folder, 'test.js') renamed_file = os.path.join(app_folder, 'test_2.js') app_js_file = os.path.join(app_folder, 'main-view-model.js') File.write(new_file, "console.log('test.js synced!!!');") File.append(app_js_file, "require('./test.js');") strings = ["test.js synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Rename file os.rename(new_file, renamed_file) File.replace(renamed_file, 'test.js', 'renamed file') time.sleep(1) File.replace(app_js_file, 'test.js', 'test_2.js') strings = ["renamed file synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Delete file File.delete(renamed_file) strings = ["Module build failed: Error: ENOENT", "NativeScript debugger detached"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) File.replace(app_js_file, "require('./test_2.js');", ' ') strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.IOS, device=self.sim, run_type=RunType.UNKNOWN) not_existing_strings = ['123'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, not_existing_string_list=not_existing_strings) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Add folder folder_name = os.path.join(app_folder, 'test_folder') new_file = os.path.join(folder_name, 'test_in_folder.js') Folder.create(folder_name) File.write(new_file, "console.log('test_in_folder.js synced!!!');") File.append(app_js_file, "require('./test_folder/test_in_folder.js');") strings = ["test_in_folder.js synced!!!"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Delete folder Folder.clean(folder_name) strings = ["Module build failed: Error: ENOENT"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
def test_210_plugin_add_static_lib_universal(self): plugin_path = os.path.join(Settings.TEST_RUN_HOME, 'assets', 'plugins', 'hello-plugin.tgz') result = Tns.plugin_add(plugin_path, path=Settings.AppName.DEFAULT, verify=False) assert "Successfully installed plugin hello." in result.output assert File.exists( os.path.join(TnsPaths.get_app_node_modules_path(self.app_name), 'hello', 'package.json')) assert File.exists( os.path.join(TnsPaths.get_app_node_modules_path(self.app_name), 'hello', 'hello-plugin.ios.js')) assert File.exists( os.path.join(TnsPaths.get_app_node_modules_path(self.app_name), 'hello', 'platforms', 'ios', 'HelloLib.a')) assert File.exists( os.path.join(TnsPaths.get_app_node_modules_path(self.app_name), 'hello', 'platforms', 'ios', 'include', 'HelloLib', 'Bye.h')) assert File.exists( os.path.join(TnsPaths.get_app_node_modules_path(self.app_name), 'hello', 'platforms', 'ios', 'include', 'HelloLib', 'Hello.h')) output = File.read(os.path.join(self.app_name, 'package.json')) assert "plugins/hello-plugin" in output # Require the plugin so webpack can pick it up main_js_file = os.path.join(Settings.TEST_RUN_HOME, self.app_name, 'app', 'main-page.js') File.append(main_js_file, 'const hello = require("hello");') Tns.prepare_ios(self.app_name) bundle_js = File.read( os.path.join( TnsPaths.get_platforms_ios_app_path(self.app_name), 'bundle.js')) vendor_js = File.read( os.path.join( TnsPaths.get_platforms_ios_app_path(self.app_name), 'vendor.js')) assert '__webpack_require__("../node_modules/hello/hello-plugin.js")' in bundle_js assert 'hello = Hello.alloc().init();' in vendor_js result = run("cat " + os.path.join( TnsPaths.get_platforms_ios_folder(self.app_name), 'TestApp.xcodeproj', 'project.pbxproj | grep \"HelloLib.a\"')) assert "HelloLib.a in Frameworks" in result.output