def sync_plugin_platform_spec(app_name, app_type, log_result, platform, device, hmr=True): # Setting the application Identifier depending on app_type except for TS if app_type is not AppType.TS: app_name = app_name + str(app_type) # Edit platform specific file and verify change is applied on device if platform == platform.ANDROID: platform_change_set = Changes.DateTimePicker.ANDROID_TS else: platform_change_set = Changes.DateTimePicker.IOS_TS File.replace( path=platform_change_set.file_path, old_string=platform_change_set.old_value, new_string=platform_change_set.new_value, fail_safe=True) strings = TnsLogs.run_messages( app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, hmr=hmr, app_type=app_type) TnsLogs.wait_for_log( log_file=log_result.log_file, string_list=strings, timeout=60) device.click(text="DatePickerField") device.wait_for_text("select date") device.click(text="select date") device.click(text="OK") date = datetime.date.today().replace(year=2010) date = date.strftime("%b %-d, %Y") device.wait_for_text(str(date))
def sync_plugin_common(app_name, app_type, platform, device, log_result, hmr=True): # Setting the application Identifier depending on app_type except for TS if app_type is not AppType.TS: app_name = app_name + str(app_type) # Edit common file in SRC change_set = Changes.DateTimePicker.COMMON_TS File.replace( path=change_set.file_path, old_string=change_set.old_value, new_string=change_set.new_value, fail_safe=True) strings = TnsLogs.run_messages( app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, hmr=hmr, app_type=app_type) TnsLogs.wait_for_log( log_file=log_result.log_file, string_list=strings, timeout=60) # Click on datepicker field and verify new value of picker is applied device.click(text="DatePickerField") today = datetime.date.today().strftime("%b %-d, %Y") device.wait_for_text(today)
def test_240_tns_preview_android_verify_plugin_warnings(self): """ Test if correct messages are shown if plugin is missing or versions differ in Preview App. """ # Add some plugins Tns.plugin_add("nativescript-barcodescanner", path=self.app_name) Tns.plugin_add("[email protected]", path=self.app_name) 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) # Verify warnings for plugins strings = [ 'Plugin nativescript-barcodescanner is not included in preview app', # 'Local plugin nativescript-geolocation differs in major version from plugin in preview app', # 'Some features might not work as expected' # TODO: Uncomment line above after we release preview app with version of nativescript-geolocation > 5.1.0 # Notes: # Preview command will fail bacause CLI will detect project needs update, see: # https://github.com/NativeScript/nativescript-cli/ # blob/b5f88a45fbde0ef5559dc02e8cee5fb95cefe882/lib/controllers/migrate-controller.ts#L58 ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
def test_392_tns_run_ios_console_dir(self): # NOTE: This test depends on creation of app in test_391_tns_run_ios_console_time # Replace app.component.ts to use console.time() and console.timeEnd() File.copy( os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'ios', 'files', 'ios-runtime-875', 'items.component.ts'), os.path.join(APP_PATH, 'src', 'app', 'item', 'items.component.ts'), True) # `tns run ios` and wait until app is deployed result = Tns.run_ios(app_name=APP_NAME, emulator=True, wait=False, verify=False) # Verify sync and initial state of the app strings = [ 'name: Ter Stegen', 'role: Goalkeeper', 'object dump end', self.sim.id ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=90, check_interval=10)
def run_app(app_name, platform, device, bundle=True, hmr=True, instrumented=False, click_open_alert=False): result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr) # 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=device) # When you run preview on ios simulator on first run confirmation dialog is shown. if device.type == DeviceType.SIM: if click_open_alert is True: if SimAuto.find(device_info=device, text="Open"): time.sleep(5) device.click("Open") # Verify logs strings = TnsLogs.preview_initial_messages(platform=platform, hmr=hmr, bundle=bundle, instrumented=instrumented) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=100) return result
def test(self, template_name, template_info): TnsRunTest.setUp(self) # Create app app_name = template_info.name.replace('template-', '') Tns.create(app_name=app_name, template='tns-' + template_name, update=False) if Settings.ENV != EnvironmentType.LIVE and Settings.ENV != EnvironmentType.PR: App.update(app_name=app_name) # Run Android result = Tns.run_android(app_name=app_name, device=self.emu.id) strings = TnsLogs.run_messages(app_name=app_name, run_type=RunType.UNKNOWN, platform=Platform.ANDROID) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) if template_info.texts is not None: for text in template_info.texts: self.emu.wait_for_text(text=text, timeout=60) else: self.emu.wait_for_main_color(color=Colors.WHITE, timeout=60) # Run iOS if Settings.HOST_OS is OSType.OSX: Simctl.uninstall_all(simulator_info=self.sim) result = Tns.run_ios(app_name=app_name, device=self.sim.id) strings = TnsLogs.run_messages(app_name=app_name, run_type=RunType.UNKNOWN, platform=Platform.IOS) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) if template_info.texts is not None: for text in template_info.texts: self.sim.wait_for_text(text=text, timeout=60) else: self.sim.wait_for_main_color(color=Colors.WHITE, timeout=60) # Cleanup TnsRunTest.tearDown(self)
def run_hello_world_js_ts(app_name, platform, device, bundle=True, hmr=True, uglify=False, release=False, aot=False, snapshot=False, instrumented=False, sync_all_files=False, just_launch=False, default_andr_sdk='29', timeout=240): # Execute `tns run` and wait until logs are OK result = Tns.run(app_name=app_name, platform=platform, emulator=True, wait=False, bundle=bundle, hmr=hmr, release=release, uglify=uglify, aot=aot, snapshot=snapshot, sync_all_files=sync_all_files, just_launch=just_launch) strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.UNKNOWN, bundle=bundle, hmr=hmr, instrumented=instrumented, device=device, release=release, snapshot=snapshot) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=timeout) # Verify it looks properly device.wait_for_text(text=Changes.JSHelloWord.JS.old_text) device.wait_for_text(text=Changes.JSHelloWord.XML.old_text) blue_count = device.get_pixels_by_color(color=Colors.LIGHT_BLUE) assert blue_count > 100, 'Failed to find blue color on {0}'.format(device.name) initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name, 'initial_state.png') device.get_screen(path=initial_state) if platform == Platform.ANDROID: # Verify android sdk the app is built with if release: apk_path = TnsPaths.get_apk_path(app_name=app_name, release=True) else: apk_path = TnsPaths.get_apk_path(app_name=app_name, release=False) TnsAssert.string_in_android_manifest(apk_path, 'compileSdkVersion="{0}"'.format(default_andr_sdk)) if snapshot and Settings.HOST_OS != OSType.WINDOWS: TnsAssert.snapshot_build(TnsPaths.get_apk_path(app_name=app_name, release=True), Settings.TEST_OUT_TEMP) return result
def test_100_run_on_all_devices(self): result = Tns.run(app_name=self.app_name, platform=Platform.NONE) # Wait for logs strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, run_type=RunType.FULL) for device in DeviceManager.get_devices(device_type=any): strings.append(device.id) strings.append(self.emu.id) if Settings.HOST_OS is OSType.OSX: strings.append(self.sim.id) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=360) # Verify it looks properly for device in DeviceManager.get_devices(device_type=any): device.wait_for_text(text=Changes.JSHelloWord.JS.old_text) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.old_text) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Edit JS file and verify changes are applied Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.JS) # Verify logs file_name = os.path.basename(Changes.JSHelloWord.JS.file_path) strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, run_type=RunType.INCREMENTAL, file_name=file_name) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Verify change is applied on all devices for device in DeviceManager.get_devices(device_type=any): device.wait_for_text(text=Changes.JSHelloWord.JS.new_text) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.new_text) self.sim.wait_for_text(text=Changes.JSHelloWord.JS.new_text)
def test_120_tns_run_ios_just_launch(self): """ This test verify following things: 1. `--justlaunch` option release the console. 2. Full rebuild and prepare are not trigerred if no changes are done. 3. Incremental prepare is triggered if js, xml and css files are changed. """ # Run app with --justlaunch and verify on device result = run_hello_world_js_ts(self.app_name, Platform.IOS, self.sim, just_launch=True) # On some machines it takes time for thr process to die time.sleep(5) assert not Process.is_running_by_commandline(Settings.Executables.TNS) # Execute run with --justlaunch again and verify no rebuild is triggered result = Tns.run_ios(app_name=self.app_name, emulator=True, just_launch=True) strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.IOS, run_type=RunType.JUST_LAUNCH, device=self.sim, just_launch=True) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) # Make changes in js, css and xml files Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.JS) Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.XML) Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.CSS) # Execute run with --justlaunch again and verify prepare is triggered result = Tns.run_ios(app_name=self.app_name, emulator=True, just_launch=True) strings = ['Project successfully prepared', 'Webpack compilation complete'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.sim.wait_for_text(text=Changes.JSHelloWord.XML.new_text)
def preview(app_name, bundle=True, hmr=True, log_trace=True, verify=True, timeout=120): """ Execute `tns preview` command. :param app_name: Pass --path <app_name>. :param bundle: If true pass --bundle. :param hmr: If true pass --hmr. :param log_trace: If true pass --log trace. :param verify: If true verify some logs. :param timeout: Timeout in seconds. :return: Result of `tns preview` command. """ result = Tns.exec_command(command='preview', path=app_name, bundle=bundle, hmr=hmr, wait=False, log_trace=log_trace, timeout=timeout) if verify: strings = [ 'Use NativeScript Playground app and scan the QR code above to preview the application on your device' ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) return result
def test_110_tns_run_android_release(self): # Run app and verify on device result = Tns.run_android(app_name=self.app_name, release=True, verify=True, emulator=True) strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, run_type=RunType.FIRST_TIME, device=self.emu) strings.remove('Restarting application on device') strings.remove('Successfully synced application org.nativescript.TestApp on device') # Verify https://github.com/NativeScript/android-runtime/issues/1024 not_existing_strings = ['JS:'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, not_existing_string_list=not_existing_strings, timeout=120) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.old_text) self.emu.wait_for_text(text=Changes.JSHelloWord.XML.old_text) blue_count = self.emu.get_pixels_by_color(color=Colors.LIGHT_BLUE) assert blue_count > 100, 'Failed to find blue color on {0}'.format(self.emu.name) Tns.kill() # Make changes in js, css and xml files Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.JS) Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.XML) Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.CSS) # Run with --release again and verify changes are deployed on device result = Tns.run_android(app_name=self.app_name, release=True, verify=True, emulator=True) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.new_text) self.emu.wait_for_text(text=Changes.JSHelloWord.XML.new_text) self.emu.wait_for_color(color=Colors.LIGHT_BLUE, pixel_count=blue_count * 2, delta=25)
def __debug_sources(self, platform, device): # Run debug, to network page and open network tab in CDT result = Tns.debug(app_name=self.app_name, platform=platform, emulator=True) device.click(text=HOME_NETWORK_BUTTON) self.dev_tools = ChromeDevTools(self.chrome, platform=platform) self.dev_tools.open_tab(tab=ChromeDevToolsTabs.SOURCES, verify=False) # Place breakpoint and verify it is hit self.dev_tools.load_source_file('network-page.ts') self.dev_tools.breakpoint(line=12) device.click(text=NET_GET_WITHOUT_BODY) self.dev_tools.wait_element_by_text(text='Paused on breakpoint', timeout=10) assert 'httpbin.org' not in File.read(result.log_file) self.dev_tools.continue_debug() TnsLogs.wait_for_log(log_file=result.log_file, string_list=['httpbin.org']) # Add one more breakpoint and hit it self.dev_tools.load_source_file('network-page.ts') self.dev_tools.breakpoint(line=30) device.click(text=NET_GET_WITH_BODY) self.dev_tools.wait_element_by_text(text='Paused on breakpoint', timeout=60) assert 'My custom Arbitrary Header value' not in File.read( result.log_file) self.dev_tools.continue_debug() TnsLogs.wait_for_log(log_file=result.log_file, string_list=['My custom Arbitrary Header value'])
def test_201_test_init_mocha_js_stacktrace(self): # https://github.com/NativeScript/ios-runtime/issues/565 Npm.install(package='mocha', folder=APP_PATH) Tns.exec_command("test init --framework", cwd=APP_PATH, platform='mocha') File.copy( os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'ios', 'files', 'ios-runtime-565', 'example.js'), os.path.join(APP_PATH, 'app', 'tests'), True) result = File.read(os.path.join(APP_PATH, 'app', 'tests', 'example.js')) assert "Mocha test" in result assert "Test" in result assert "Array" not in result result = Tns.exec_command("test ios", cwd=APP_PATH, emulator=True, wait=False) # TODO: Bundle: Add path to stack trace assert, (e.g. @file:///app/tests/example.js:5:25') # https://github.com/NativeScript/nativescript-cli/issues/4524 strings = [ 'JavaScript stack trace', 'JS ERROR AssertionError: expected -1 to equal 1' ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=90)
def test_385_methods_with_same_name_and_different_parameters(self): """ https://github.com/NativeScript/ios-runtime/issues/877 PR https://github.com/NativeScript/ios-runtime/pull/1013 """ # Replace main-page.js to call methods with the same name but different parameters count File.copy( os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'ios', 'files', 'ios-runtime-877', 'main-page.js'), os.path.join(APP_PATH, 'app', 'main-page.js'), True) result = Tns.run_ios(app_name=APP_NAME, emulator=True, wait=False, verify=False) strings = [ 'Successfully synced application', 'SayName no param!', 'SayName with 1 param!', 'SayName with 2 params!' ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=150, check_interval=10) # Verify app is running on device Device.wait_for_text(self.sim, text=TAP_THE_BUTTON)
def test_384_check_for_native_and_js_callstacks(self): """ https://github.com/NativeScript/ios-runtime/pull/1144 """ # Replace main-page.js so there is an error File.copy( os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'ios', 'files', 'ios-pr-1144', 'main-page.js'), os.path.join(APP_PATH, 'app', 'main-page.js'), True) result = Tns.run_ios(app_name=APP_NAME, emulator=True, wait=False, verify=False) strings = [ 'Native Stack:', 'sig_handler(int)', 'JS Stack:', '1 @[native code]', '2 onNavigatingTo@file:///app/main-page.js:34:0' ] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=150, check_interval=10) # Verify app is NOT running on device test_result = Wait.until(lambda: Simctl.is_process_running( self.sim, 'org.nativescript.' + APP_NAME) is False, timeout=120, period=5) assert test_result, "It seems that " + APP_NAME + " is still running when it should not!"
def run_demo_app(app_name, app_type, plugin_name, platform, hmr=True): """ Change plugin in src and verify demo is updated. :param app_name: The name of the App. for example: demo, demo-angular, demo-vue :param app_type: Application type: js,ts,ng,vue. :param plugin_name: The name of the plugin. for example: nativescript-datetimepicker :param platform: The platform type Platform.IOS or Platform.ANDROID :param hmr: HMR flag boolean. """ # Navigate to demo folder and run the demo app app_folder = 'demo' if app_type == AppType.NG: app_folder = 'demo-angular' app_name = app_name + 'ng' elif app_type == AppType.VUE: app_folder = 'demo-vue' app_name = app_name + 'vue' app_path = os.path.join(Settings.TEST_SUT_HOME, plugin_name, app_folder) if Settings.HOST_OS is OSType.OSX: Tns.platform_add_ios(app_name=app_path, framework_path=Settings.IOS.FRAMEWORK_PATH) else: Tns.platform_add_android(app_name=app_path, framework_path=Settings.Android.FRAMEWORK_PATH) App.update(app_path) result = Tns.run(app_name=app_path, platform=platform, emulator=True, wait=False, hmr=hmr) strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.FULL, hmr=hmr, app_type=app_type) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180) return result
def preview(app_name, bundle=True, hmr=True, log_trace=False, verify=True, timeout=120, options='--link'): """ Execute `tns preview` command. :param app_name: Pass --path <app_name>. :param bundle: If true pass --bundle. :param hmr: If true pass --hmr. :param log_trace: If true pass --log trace. :param verify: If true verify some logs. :param timeout: Timeout in seconds. :return: Result of `tns preview` command. """ result = Tns.exec_command(command='preview', path=app_name, bundle=bundle, hmr=hmr, wait=False, log_trace=log_trace, timeout=timeout, options=options) if verify: strings = ['Generating qrcode for url'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) return result
def test_300_deploy_list_and_run_applications(self): # Deploy test application app_id = TnsPaths.get_bundle_id(app_name=APP_NAME) result = Tns.deploy(app_name=APP_NAME, platform=Platform.ANDROID, just_launch=True, wait=True) for device in self.ANDROID_DEVICES: assert device.id in result.output if Settings.HOST_OS == OSType.OSX: result = Tns.deploy(app_name=APP_NAME, platform=Platform.IOS, just_launch=True, wait=True) for device in self.IOS_DEVICES: assert device.id in result.output # Verify list-applications command list default android apps and the app we've just deployed for device in self.ANDROID_DEVICES: result = Tns.exec_command( 'device list-applications --device {0}'.format(device.id)) assert 'com.android' in result.output assert app_id in result.output # Verify `device run <bundle-id>` will start the app device = self.ANDROID_DEVICES[0] Adb.stop_application(device_id=device.id, app_id=app_id) assert not device.is_text_visible( text=Changes.JSHelloWord.JS.old_value), 'Failed to stop the app.' Tns.exec_command(command='device run {0}'.format(app_id), device=device.id, wait=True, just_launch=True) device.wait_for_text(text=Changes.JSHelloWord.JS.old_value) # Get Android device logs result = Tns.exec_command(command='device log', device=device.id, wait=False) TnsLogs.wait_for_log(log_file=result.log_file, string_list=['beginning of'], timeout=120) assert 'I' or 'D' or 'W' in File.read( result.log_file ), 'Log does not contain INFO, DEBUG or WARN messages.' # Get iOS device logs if Settings.HOST_OS == OSType.OSX: device = self.IOS_DEVICES[0] result = Tns.exec_command(command='device log', device=device.id, wait=False) TnsLogs.wait_for_log(log_file=result.log_file, string_list=['>:'], timeout=120) assert "<Notice>:" or "<Error>:" in File.read( result.log_file), 'tns device log fails to get ios logs.'
def debug(app_name, platform, start=False, debug_brk=False, emulator=False, device=None, release=False, provision=Settings.IOS.PROVISIONING, for_device=False, bundle=True, hmr=True, aot=False, uglify=False, wait=False, log_trace=False, verify=True): command = 'debug' if start: command += ' --start' if debug_brk: command += ' --debug-brk' result = Tns.exec_command(command=command, path=app_name, platform=platform, emulator=emulator, device=device, release=release, provision=provision, for_device=for_device, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify, wait=wait, log_trace=log_trace) if verify: strings = [ 'To start debugging, open the following URL in Chrome:', 'chrome-devtools://devtools/bundled/inspector.html?experiments=true&ws=localhost:' ] if not start: strings.append('Successfully synced application') else: if platform == Platform.ANDROID: strings.append('ActivityManager: Start proc') if hmr and platform == Platform.ANDROID: strings.append('HMR: Hot Module Replacement Enabled.') TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) logs = File.read(result.log_file) assert 'closed' not in logs assert 'detached' not in logs assert "did not start in time" not in logs return result
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_310_tns_run_ios_sync_changes_in_node_modules(self): """ Verify changes in node_modules are synced during run command """ # Run the project result = run_hello_world_js_ts(self.app_name, Platform.IOS, self.sim, sync_all_files=True) # Make code changes in tns-core-modules verify livesync is triggered Sync.replace(self.app_name, Changes.NodeModules.TNS_MODULES) strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, run_type=RunType.INCREMENTAL, device=self.emu, file_name='application-common.js') TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings) self.emu.wait_for_text(text=Changes.JSHelloWord.JS.old_text)
def run_hello_world_ng(app_name, platform, device, bundle=True, uglify=False, aot=False, hmr=True, instrumented=True, release=False, snapshot=False): # Define if it should be executed on device or emulator emulator = True device_id = None if device.type == DeviceType.ANDROID or device.type == DeviceType.IOS: emulator = False device_id = device.id # Execute tns run command result = Tns.run(app_name=app_name, platform=platform, emulator=emulator, bundle=bundle, aot=aot, uglify=uglify, hmr=hmr, release=release, snapshot=snapshot, device=device_id) # Check logs strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.UNKNOWN, bundle=bundle, release=release, hmr=hmr, instrumented=instrumented, app_type=AppType.NG, device=device, snapshot=snapshot) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) # Verify it looks properly device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text, timeout=180) device.wait_for_main_color(color=Colors.WHITE) initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name, 'initial_state.png') device.get_screen(path=initial_state) return result
def test_300_tns_run_on_specific_device(self): Adb.open_home(device_id=self.emu.id) Adb.open_home(device_id=self.android_device.id) result = Tns.run(app_name=self.app_name, platform=Platform.ANDROID, device=self.android_device.id) # Wait for logs strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.ANDROID, run_type=RunType.UNKNOWN) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) # Verify it looks properly on device self.android_device.wait_for_text(text=Changes.JSHelloWord.JS.old_text) # Verify not working on emulator assert Changes.JSHelloWord.JS.old_text not in self.emu.get_text() assert self.emu.id not in File.read(result.log_file)
def test_200_error_activity_shown_on_error(self): result = Tns.run_android(app_name=APP_NAME, emulator=True, wait=False) self.emulator.wait_for_text('TAP', timeout=180, retry_delay=10) # Break the app to test error activity # add workaround with for-cycle for https://github.com/NativeScript/nativescript-cli/issues/3812 wait_code = 'var e = new Date().getTime() + 3000; while (new Date().getTime() <= e) {} ' exception_code = 'throw new Error("Kill the app!");' file_path = os.path.join(Settings.TEST_RUN_HOME, APP_NAME, 'app', 'app.js') old_value = 'application.run({ moduleName: "app-root" });' new_value = wait_code + exception_code File.replace(path=file_path, old_string=old_value, new_string=new_value, backup_files=True) # Verify logs and screen TnsLogs.wait_for_log( log_file=result.log_file, string_list=['StackTrace:', 'Error: Kill the app!']) if self.emulator.version < 10.0: regex_to_check = r"""System\.err: Error: Kill the app! System\.err: File: \(file: app\/app\.js:\d+:\d+\) System\.err: StackTrace: System\.err:.+\(file: app\/app.js:\d+:\d+\) System\.err:.+at \.\/app\.js\(file:\/\/\/data\/data\/org\.nativescript\.TestApp\/files\/app\/bundle\.js:\d+:\d+\) System\.err:.+at __webpack_require__\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at checkDeferredModules\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at webpackJsonpCallback\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at \(file:\/\/\/data\/data\/org\.nativescript\.TestApp\/files\/app\/bundle\.js:\d+:\d+\) System\.err:.+at require\(:\d+:\d+\)""" # noqa: E501, E261, W291 else: regex_to_check = r"""System\.err: Error: Kill the app! System\.err: File: \(file: app\/app\.js:\d+:\d+\) System\.err:.+ System\.err: StackTrace:.+ System\.err:.+\(file: app\/app\.js:\d+:\d+\) System\.err:.+at \.\/app\.js\(file:\/\/\/data\/data\/org\.nativescript\.TestApp\/files\/app\/bundle\.js:\d+:\d+\) System\.err:.+at __webpack_require__\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at checkDeferredModules\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at webpackJsonpCallback\(file: app\/webpack\/bootstrap:\d+:\d+\) System\.err:.+at \(file:\/\/\/data\/data\/org\.nativescript\.TestApp\/files\/app\/bundle\.js:\d+:\d+\) System\.err:.+at require\(:\d+:\d+\)""" # noqa: E501, E261, W291 Assert.assert_with_regex(File.read(result.log_file), regex_to_check) self.emulator.wait_for_text('Exception') self.emulator.wait_for_text('Logcat') self.emulator.wait_for_text('Error: Kill the app!')
def test_325_tns_run_android_should_start_emulator(self): """ `tns run android` should start emulator if device is not connected. """ # Run the test only if there are no connected devices conected_devices = Adb.get_ids() if conected_devices.__len__() == 0: DeviceManager.Emulator.stop() result = Tns.run_android(self.app_name) strings = ['Starting Android emulator with image'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=120) DeviceManager.Emulator.stop() DeviceManager.Emulator.ensure_available(Settings.Emulators.DEFAULT) else: raise nose.SkipTest('This test is not valid when devices are connected.')
def test_325_tns_run_ios_should_start_simulator(self): """ `tns run android` should start emulator if device is not connected. """ # Run the test only if there are no connected devices conected_devices = DeviceManager.get_devices(device_type=DeviceType.IOS) if conected_devices.__len__() == 0: DeviceManager.Simulator.stop() result = Tns.run_ios(self.app_name) strings = TnsLogs.run_messages(app_name=self.app_name, platform=Platform.IOS, run_type=RunType.FULL, device=self.sim) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=120) DeviceManager.Simulator.stop() DeviceManager.Simulator.ensure_available(Settings.Simulators.DEFAULT) else: raise nose.SkipTest('This test is not valid when devices are connected.')
def workflow(app_name, device, platform, shared): # Create an app app_path = TnsPaths.get_app_path(app_name=app_name) Folder.clean(app_path) NG.new(collection=NS_SCHEMATICS, project=app_name, shared=shared) TnsAssert.created(app_name=app_name, app_data=None) # Run app initially text = 'TAP' if shared: text = 'Welcome to' result = Tns.run(app_name=app_name, platform=platform, emulator=True, hmr=True) strings = TnsLogs.run_messages(app_name=app_name, platform=platform, bundle=True, hmr=True, app_type=AppType.NG) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300) device.wait_for_text(text=text) # Generate module and component NG.exec_command(command='g m module-test', cwd=app_path) NG.exec_command(command='g c module-test/component-test', cwd=app_path) # Update app.modules.ts app_module_name = 'app.module.ts' app_module_path = os.path.join(app_path, 'app', app_module_name) if shared: app_module_name = 'app.module.tns.ts' app_module_path = os.path.join(app_path, 'src', 'app', app_module_name) old_string = "import { HomeComponent } from './home/home.component';" new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';" File.replace(path=app_module_path, old_string=old_string, new_string=new_string) File.replace(path=app_module_path, old_string='HomeComponent,', new_string='ComponentTestComponent,') # Update app-routing.module.ts app_routing_module_name = 'app-routing.module.ts' app_routing_module_path = os.path.join(app_path, 'app', app_routing_module_name) if shared: app_routing_module_name = 'app.routes.ts' app_routing_module_path = os.path.join(app_path, 'src', 'app', app_routing_module_name) old_string = "import { HomeComponent } from './home/home.component';" new_string = "import { ComponentTestComponent } from './module-test/component-test/component-test.component';" File.replace(path=app_routing_module_path, old_string=old_string, new_string=new_string) File.replace(path=app_routing_module_path, old_string='HomeComponent', new_string='ComponentTestComponent') # Verify app is updated logs = [app_module_name.replace('.tns', ''), app_routing_module_name.replace('.tns', ''), 'Successfully synced application'] TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs, timeout=120) device.wait_for_text(text='component-test works!')
def test(app_name, platform, emulator=True, device=None, just_launch=True, verify=True, wait=True): """ Execute `tns test <platform>` command. :param app_name: App name (passed as --path <App name>) :param platform: PlatformType enum value. :param emulator: If true pass `--emulator` to the command. :param device: Pass `--device <value>` to command. :param just_launch: If true pass `--just_launch` to the command. :param verify: Verify command was executed successfully. :param wait: Wait command to complete it `true`. :return: Result of `tns test` command. """ cmd = 'test {0}'.format(str(platform)) result = Tns.exec_command(command=cmd, path=app_name, emulator=emulator, device=device, just_launch=just_launch, wait=wait) if verify: if wait: assert 'server started at' in result.output assert 'Launching browser' in result.output assert 'Starting browser' in result.output assert 'Connected on socket' in result.output assert 'Executed 1 of 1' in result.output assert 'TOTAL: 1 SUCCESS' in result.output \ or 'Executed 1 of 1 SUCCESS' or 'Executed 1 of 1[32m SUCCESS' in result.output else: strings = [ 'server started at', 'Launching browser', 'Starting browser', 'Connected on socket', 'Executed 1 of 1' ] assert 'TOTAL: 1 SUCCESS' in result.output \ or 'Executed 1 of 1 SUCCESS' or 'Executed 1 of 1[32m SUCCESS' in result.output TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=120) return result
def run_hello_world_js_ts(app_name, platform, device, bundle=True, hmr=True, uglify=False, aot=False, snapshot=False, instrumented=False, sync_all_files=False, just_launch=False): # Execute `tns run` and wait until logs are OK result = Tns.run(app_name=app_name, platform=platform, emulator=True, wait=False, bundle=bundle, hmr=hmr, uglify=uglify, aot=aot, snapshot=snapshot, sync_all_files=sync_all_files, just_launch=just_launch) __verify_snapshot_skipped(snapshot, result) strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.UNKNOWN, bundle=bundle, hmr=hmr, instrumented=instrumented, device=device) TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=240) # Verify it looks properly device.wait_for_text(text=Changes.JSHelloWord.JS.old_text) device.wait_for_text(text=Changes.JSHelloWord.XML.old_text) blue_count = device.get_pixels_by_color(color=Colors.LIGHT_BLUE) assert blue_count > 100, 'Failed to find blue color on {0}'.format( device.name) initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name, 'initial_state.png') device.get_screen(path=initial_state) return result
def test_320_tns_run_android_should_warn_if_package_ids_dont_match(self): """ If bundle identifiers in package.json and app.gradle do not match CLI should warn the user. """ # Change app id in app.gradle file app_gradle = os.path.join(Settings.TEST_RUN_HOME, self.app_name, 'app', 'App_Resources', 'Android', 'app.gradle') File.replace(app_gradle, old_string='generatedDensities = []', new_string='applicationId = "org.nativescript.MyApp"') # Run the app on device and verify the warnings result = Tns.run_android(app_name=self.app_name, just_launch=False) strings = ["WARNING: The Application identifier is different from the one inside \"package.json\" file.", "NativeScript CLI might not work properly.", "Project successfully built"] TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)